天天看点

Day14 JavaSE基础复习 (18)Map集合 (19) 异常&IO(File类)(18)Map集合 (19) 异常&IO(File类) 

(18)Map集合 

一.

  1.  Collection的添加是add();

     Map的添加是put(key,value);

  2. Map集合遍历之键值对对象找键和值
    Map<String,Integer> map = new HashMap<>();
    map.put("张三",23);
    
    //Map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并存储在Set集合中
    Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
    //获取每一个对象
    Iterator<Map.Entry<String,Integer>> it = entrySet.iterator();
    while(it.hasNext){
        //获取每一个Entry对象
        Map.Entry<String,Integer> en = it.next();
        String key = en.getKey();
        Integer value = en.getValue();
        syso(key + "=" + value);
    }
    
    //用增强for循环遍历
    for(Map.Entry<String,Integer> en: map.entrySet){
        syso(en.getKey() + "=" + en.getValue());
    }
               
  3. Map集合遍历之键找值
    Map<String,Integer> map = new HashMap<>();
    map.put("张三",23);
    
    //迭代器遍历
    Set<String> keySet = map.keySet();
    Iterator<String> it = keySet.iterator();
    while(it.hasNext){
        String key = it.next();
        Integer value = map.get(key);
        syso(key + "=" + value);
    }
    
    //增强for循环
    for(String key : map.keySet()){
        syso(key + "=" + map.get(key));
    }
               
  4. HashMap和Hashtable   //因为Hashtable早期出现,没遵守命名规范

    共同点:底层都是哈希算法,都是双列集合

    区别:

            1.  HashMap是线程不安全的,效率高,JDK1.2版本

                 Hashtable是线程安全的,效率低,JDK1.0版本

            2.  HashMap可以存储null键和null值

                 Hashtable不可以存储null键和null值

  5. Collections工具类,内部都是静态方法,构造方法私有,直接类名.方法名调用

    Collections.sort(list);  //排序

    Collections.binarySearch(list,key);   //list是集合,key是list中的元素,该方法返回key的索引,

                                                             //若不包含key,则返回(-插入点-1)

    Collections.reverse(list);      //反转

    Collections.shuffle(list);          //随机置换

(19) 异常&IO(File类) 

一. 异常

二.File类  import java.io.File;

  1. new File(String pathname);   //根据一个路径得到File对象

    new File(String parent,String child);  //根据一个目录和一个子文件/目录得到File对象

    new File(File parent,String child);  //根据一个父File对象和一个子文件/目录得到File对象

  2. file.createNewFile();        //返回的是boolean值   创建文件,如果没有就创建返回true,有就不创建返回false
  3. file.mkdir();                 //返回的是boolean值   创建文件夹,如果没有就创建返回true,有就不创建返回false

    file.mkdirs();             //创建多级目录,mkdir则不行

  4. file1.renameTo(file2);        //如果路径名相同,就是改名       如果路径名不同,就是改名并截切
  5. file.delete();          //如果删除一个文件夹,那么文件夹必须是空的
  6. file.setReadable(true);

    file.setWritable(true);

    file.canReadable();        //windows系统认为所有的文件都是可读的

    file.canWritable();          //windows系统可以设置为不可写

  7. file.getAbsolutePath();   //获取绝对路径

    file.getPath();    //获取构造方法中传入的路径  File file = new File("路径");

  8. file.lastModified();    //文件的最后修改时间,返回的是毫秒值
  9. file.list();     //返回的是String[ ]           存的是指定目录下的所有文件或者文件夹的名称

    file.listFiles();    //返回的是File[ ]       获取指定目录下的所有文件或者文件夹

  10. 文件名称过滤器,因为FilenameFilter是接口,所以用匿名内部类 
    File dir = new File("d:\\");
    String[] arr = dir.list(new FilenameFilter(){
        
        public boolean accept(File dir,String name){
            File file = new File(dir,name);      
            //下边是看文件是否符合要求,符合就返回true,
            //list(FilenameFilter filenameFilter)底层中如果accept()返回的是true,就添加在数组中
            return file.isFile() && file.getName().endWith("**");
        }
    });