• java学习--day19(Map集合&File类)


    回顾:

    1.匿名内部类的写法
    	针对于抽象类和接口的
    	现在直接new,但是在new  接口的时候  一定重写抽象的方法
    	new A (){
    		public void test () {
    			System.out.println("测试");
    		}
    	}.test();
    	真实开发的时候:
    		一个接口或者抽象类会作为方法的参数。
    		在之前新建一个类去实现一个接口。但是现在学完匿名内部类之后
    		直接在方法中直接new  
    2.成员内部类的写法
    	class Person {
    		
    		class Inner {
    			
    		}
    	}
    3.String类下面的方法
    	重要
    	
    4.List接口下面的方法
    	add
    	remove
    	get
    	size
    	set
    5.比较器在TreeSet中的使用
    Set set = new TreeSet<>(new Comparator () {
    	public int compare (Student o1, Student o2) {
    		return o1.age - o2.age;
    	}
    });
    6.八大基本数据类型所对应得包装类
    int Integer
    byte Byte
    short Short
    long Long
    float Float
    double Double
    char  Character
    boolean Boolean
    7.HashSet和TreeSet区别
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    今天的内容

    1.Map

    2.File类

    1.Map集合

    地图, 通过一个点可以找到一个具体的位置 映射

    map集合也是存数据的

    双边队列

    Interface Map

    k: key 键

    v: value 值

    键值对

    意味着咱们map集合中存的数据是键值对像=形式的数据

    {0001=张三, 002=李四, 003=王五}

    –|HashMap

    –|TreeMap

    1.1Map集合中常用的方法
    增:
    	V put(K key, V vlaue);添加键值对的数据到map集合中
    	void putAll(Map<? extends K> k, Map<? extends V> v);将一个map集合存放到另外一个map集合中
    	
    删:
    	V remove (K key);通过键删除键值对的数据,返回值是值
    改:
    	V  put(K key V value);当键值存在的时候,会将value值覆盖掉的
    查:
    	int size(); 查看集合中元素的个数
    	boolean isEmpty();判断是否为空,如果不为空返回的是false
    	boolean containsKey();是否包含这个键
    	boolean containsValue();是否包含这个值
    	重要的方法:
    	V get(K key);通过键获取值
    	Set<K>  keySet();获取map集合中的键,然后存到set集合中
    	Collection<V> values(); 获取map集合中值,存到了Collection集合中
    	Set<Map.Entry<K,V>> entrySet();将map集合的键值对,存到了set集合
    		Map.Entry这个接口的方法
    			getKey:返回键值对的键
    			getValue:返回键值对的值
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    package com.qfedu.a_map;
    // hashMap 的操作
    // 方法:  put putAll
    
    // hashMap 中的建是唯一的
    // 当键是一样的时候,value值是会被替换的
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    public class Demo1 {
        public static void main(String[] args) {
            Map<String,String> map = new HashMap<>();
    
            // 添加:put(key,value)
            map.put("001","张三");
            map.put("002","李四");
            map.put("003","王五");
            System.out.println(map);
            map.put("001","赵六");
            System.out.println(map);
    
            Map<String,String> map1 = new HashMap<>();
            map1.put("004","周杰伦");
            map1.put("005","林俊杰");
            map1.putAll(map);
            System.out.println(map1);
    
            // 删除:remove(key) 方法  有返回值:value
            System.out.println(map1.remove("001")); //赵六
            System.out.println(map1); //{002=李四, 003=王五, 004=周杰伦, 005=林俊杰}
    
            // 改:put(key,value) 就相当于覆盖
            map1.put("002","蔡徐坤");
            System.out.println(map1); //{002=蔡徐坤, 003=王五, 004=周杰伦, 005=林俊杰}
    
            // 查询:
            System.out.println(map1.size()); //4
            System.out.println(map1.isEmpty()); //false
            System.out.println(map1.containsKey("001")); // false
            System.out.println(map1.containsValue("马保国")); //false
    
            // 获取所有的键 存到set集合中
            Set<String> strings = map.keySet();
            System.out.println(strings);//[001, 002, 003]
    
            // 获取所有的值,存到collection集合中
            Collection<String> values = map.values();
            System.out.println(values);//[赵六, 李四, 王五]
    
            //entrySet: 将map中的键值对存储在set集合中
            Set<Map.Entry<String, String>> entries = map.entrySet();
            System.out.println(entries); //[001=赵六, 002=李四, 003=王五]
    
            // 将数据取出来 用增强for循环(iter快捷键)
            for (Map.Entry<String, String> entry : entries) {
                System.out.println(entry);
                System.out.println(entry.getKey());
                System.out.println(entry.getValue());
                System.out.println("************");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    1.2Map集合中的value 存的是对象
    package com.qfedu.a_map;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    class Student {
        String name;
        int age;
        public Student (String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    public class Demo2 {
        public static void main(String[] args) {
            Map<Integer,Student> map = new HashMap<>();
            // 将student对象存入map的value中
            map.put(1,new Student("马保国",73));
            map.put(2,new Student("蔡徐坤",36));
            System.out.println(map);
    
            // 取键
            Set<Integer> key = map.keySet();
            System.out.println(key);
            // 取值
            Collection<Student> values = map.values();
            System.out.println(values);
            // 取对象的名字
            for (Student value : values) {
                System.out.println(value.name);
            }
    
            System.out.println("-----------");
            // 使用entrySet方法进行取值
            Set<Map.Entry<Integer, Student>> entries = map.entrySet();
            for (Map.Entry<Integer, Student> entry : entries) {
                System.out.println(entry.getValue().name);
            }
    
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    1.3map集合中的值存储一个list狗对象
    package com.qfedu.a_map;
    
    // map> map集合中的值存储一个list学生对象
    
    import java.util.*;
    
    class Dog {
        String name;
        int age;
        public Dog (String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Dog{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    public class Demo3 {
        public static void main(String[] args) {
            // 新建一个list集合存储Dog对象
            List<Dog> list = new ArrayList<>();
            list.add(new Dog("发大财",5));
            list.add(new Dog("挣大钱",6));
            list.add(new Dog("阿黄",4));
    
            // 再建一个list1 存储Dog对象 (可以理解为两个家庭的狗)
            List<Dog> list1 = new ArrayList<>();
            list1.add(new Dog("旺财",9));
            list1.add(new Dog("来福",7));
    
            // 创建map对象并进行存值
            Map<String, List<Dog>> map = new HashMap<>();
            map.put("001",list);
            map.put("002",list1);
    
            // 取值
            System.out.println(map); // 输出{001=[Dog{name='发大财', age=5}, Dog{name='挣大钱', age=6}, Dog{name='阿黄', age=4}], 002=[Dog{name='旺财', age=9}, Dog{name='来福', age=7}]}
            Collection<List<Dog>> values = map.values(); // values 值是list集合(两家的狗)
            for (List<Dog> value : values) {
                System.out.println(value);  // 分别输出两家狗
                System.out.println("-------------");
                for (Dog dog : value) {
                    System.out.println(dog); // 输出每家狗的信息
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    总结:
    	开发中用
    		List==》ArrayList
    		Map===>HashMap
    
    • 1
    • 2
    • 3
    • 4

    2.File类

    文件和文件夹(文件路径)的抽象表示,是专门来出来u磁盘上面的文件或者文件夹的

    之前都是手动创建,现在可以借助Java封装好的类创建文件夹和文件。

    路径问题:

    ​ 相对路径:

    ​ 得有一个参照物

    ​ ./当前工作目录

    ​ …/上一级路径的目录

    ​ …/…/上两级目录

    ​ 绝对路径:

    ​ 从磁盘的根路径一级一级的往下找

    ​ 如: C:\ccc\ddd\1.png

    2.1File 类的构造方法

    File(String pathname)

    通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。

    File(String parent, String child)

    从父路径名字符串和子路径名字符串创建新的 File实例。

    package com.qfedu.b_file;
    
    import java.io.File;
    
    public class Demo1 {
        public static void main(String[] args) {
            //可以将1.txt 抽象成一个Java代码的对象
            File file = new File("c:/aaa/1.txt");
            File file2 = new File("c:\\aaa\\1.txt");
            //Java中   看系统
            /**
             * 1./  在windows系统和linux 都是可以的!!!
             * 2.\  只有在windows系统下面可以的(\\反斜线也可以代表路径) 不推荐
             */
            /**
             * static String	separator(File 的方法)
             * 与系统相关的默认名称 - 分隔符字符,以方便的方式表示为字符串。
             * 当你是window系统的时候 就是\
             * 当你的代码在linux  /
             */
    
            System.out.println(File.separator);
            //为了保证你的代码在任意的系统下面 路径的分割符是都可以使用
            //可以咋写代码?
            File file3 = new File("c:" + File.separator + "aaa" + File.separator + "1.txt");
            System.out.println(file3);
            System.out.println("=====");
            System.out.println(file);
            //File(String parent, String child)
            File file1 = new File("c:/aaa/", "1.txt");
            System.out.println(file1);
    
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    2.2File类下面的方法

    boolean createNewFile();创建一个文件 返回值是布尔类型的数据

    ​ 1.如果文件路径不存在会报错

    ​ 2.如果文件名字已经存在,返回false

    ​ 3.磁盘坏了,创建不了

    boolean mkdir();创建单级路径

    boolean mkdirs();创建多级路径

    package com.qfedu.b_file;
    
    import java.io.File;
    import java.io.IOException;
    
    public class Demo2 {
        public static void main(String[] args) throws IOException {
            File file = new File("c:/aaa/1.txt");
            System.out.println(file.createNewFile());
            File file1 = new File("c:/aaa/bbb");
            System.out.println(file1.mkdir());//单级路径
    
            File file2 = new File("c:/aaa/ccc/ddd");
            System.out.println(file2.mkdirs());//多级路径
            
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    删除文件或者文件夹

    boolean delete();立即删除文件,常用的

    void deleteOnExit();不会立即删除,是程序退出以后才删除的

    package com.qfedu.b_file;
    
    import java.io.File;
    import java.util.Scanner;
    
    public class Demo3 {
        public static void main(String[] args) {
            File file = new File("c:/aaa/1.txt");
            System.out.println(file.delete());  // 删除1.txt文件
            //删除一个非空的文件夹,是删除不了的,咋办?递归
            File file1 = new File("c:/aaa/ccc");
            System.out.println(file1.delete());
    
            File file2 = new File("c:/aaa/bbb");
            System.out.println(file2.delete());
            File file3 = new File("c:/aaa/ccc/ddd");
            file3.deleteOnExit();
            //如果保证程序不退出
            new Scanner(System.in).nextInt();  // 输入内容后退出
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    File对象的判断方法,比较常用的

    boolean isFile(); 是否是文件【常用】(txt、md之类,文件夹除外)

    boolean isDirectory();是否是文件夹【常用】

    boolean isHidden();是否是隐藏文件

    boolean isAbsolute();是否是绝对路径(从磁盘根目录)

    boolean exists();判断文件或者文件夹是否存在【重要】

    package com.qfedu.b_file;
    
    import java.io.File;
    import java.io.IOException;
    
    public class Demo4 {
        public static void main(String[] args) throws IOException {
            File file = new File("c:/aaa");
            System.out.println(file.isFile());//false
            File file1 = new File("c:/aaa/2.gif");
            System.out.println(file1.isFile());//true
            System.out.println(file.isDirectory());//true
            System.out.println(file1.isDirectory());//false
            File file2 = new File("c:/aaa/2.txt");
            System.out.println(file2.isHidden());//true
            System.out.println(file1.isHidden());//false
            System.out.println(file.isAbsolute());//true
    
            File file3 = new File("./");
            System.out.println(file3.isAbsolute());//false
    
            System.out.println(file1.exists());//true
            if (!file1.exists()) {
                file1.createNewFile();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    返回值是String类型的数据的

    String getName(); 获取文件或者文件夹的名字的

    String getPath();获取当前对象的路径的

    String getParent();获取当前文件对象的上一级的目录

    package com.qfedu.b_file;
    
    import java.io.File;
    
    public class Demo5 {
        public static void main(String[] args) {
            File file = new File("c:\\aaa\\2.gif");
            System.out.println(file.getName());//2.gif
            System.out.println(file.getParent());//c:\aaa
            System.out.println(file.getPath());//c:\aaa\2.gif
            System.out.println(file);
           
    
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    返回是long类型数据的

    win10,win11中一个汉字(utf-8)占3字节

    win7中gbk一个汉字占2个字节

    long lenght();返回值是文件占用的字节数

    long lastModified();获取当前文件最后一次修改的时间

    ​ 返回值单位是毫秒,除以1000 变成秒,才是时间戳

    package com.qfedu.b_file;
    
    import java.io.File;
    
    public class Demo6 {
        public static void main(String[] args) {
            File file = new File("c:/aaa/8.txt");
            System.out.println(file.length());//48
    
            //1659687558179  
            //从1970 1月1日 0时 0分 0 秒  ~ 2022 8  5  16:19
            //的毫秒数
            System.out.println(file.lastModified());
            
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    File[] listFiles(); 获取当前文件夹下面的所有的=文件

    String[] list();获取当前文件夹下面的所有文件的名字

    package com.qfedu.b_file;
    
    import java.io.File;
    
    public class Demo7 {
        public static void main(String[] args) {
            File file = new File("c:/");
            File[] files = file.listFiles();
            for (File file1 : files) {
                //列出来的是对象 File
                System.out.println(file1);
            }
            System.out.println("=======");
            String[] list = file.list();
            for (String s : list) {
                //文件的名字
                System.out.println(s);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    需求:

    删除某一个目录下面的所有的文件
    当你不知道你有多少个文件夹和文件的时候,咱们可以递归的
    
    • 1
    • 2
    package com.qfedu.b_file;
    
    import java.io.File;
    
    public class Demo9 {
        public static void main(String[] args) {
            File file = new File("c:/aaa/bbb");
            del(file);
        }
        public static void del (File file) {
            //所有文件先取出来
            File[] files = file.listFiles();
           
            /**分析
             * c:\aaa\bbb\1.txt
             * c:\aaa\bbb\ddd
             *  file1是ddd  是文件夹
             *  1.txt删除了
             *  再次执行del(file1) ddd文件夹
             *      eee
             *      2.txt
             *        再次执行del(eee) eee文件夹了
             *
             *
             */
            for (File file1 : files) {
                System.out.println(file1);
                if (file1.isDirectory()) {//是文件夹
                    del(file1);//递归
    
                }else {//是文件
                    file1.delete();
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    第八章 Spring AOP
    SemanticKITTI点云标注工具
    DeepLearning - 余弦退火热重启学习率 CosineAnnealingWarmRestartsLR
    c++征途 ---- 类与对象 --- 继承
    HarmonyOS原子化服务开发-应用签名保存好的重要性
    基于区块链的数据共享访问控制模型
    Webpack VS Rollup
    【快速解决】实验三 简单注册的实现《Android程序设计》实验报告
    外滩大会观察|重估蚂蚁!
    AI 考生挑战高考作文获 48 分;IBM 宣布退出俄罗斯市场,已暂停在俄所有业务;OpenCV 4.6 发布|极客头条
  • 原文地址:https://blog.csdn.net/m0_46202060/article/details/133243528