Day14 JavaSE基础复习 (18)Map集合 (19) 异常&IO(File类)
2019-05-24 18:12
232 查看
(18)Map集合
一.
- Collection的添加是add();
Map的添加是put(key,value); - Map集合遍历之键值对对象找键和值
[code]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()); }
- Map集合遍历之键找值
[code]Map<String,Integer> map = new HashMap<>(); map.put("张三",23); //迭代器遍历 Set<String> keySet = map.keySet(); Itera 4000 tor<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)); }
-
HashMap和Hashtable //因为Hashtable早期出现,没遵守命名规范
共同点:底层都是哈希算法,都是双列集合
区别:
1. HashMap是线程不安全的,效率高,JDK1.2版本
Hashtable是线程安全的,效率低,JDK1.0版本
2. HashMap可以存储null键和null值
Hashtable不可以存储null键和null值 -
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;
- new File(String pathname); //根据一个路径得到File对象
new File(String parent,String child); //根据一个目录和一个子文件/目录得到File对象
new File(File parent,String child); //根据一个父File对象和一个子文件/目录得到File对象 - file.createNewFile(); //返回的是boolean值 创建文件,如果没有就创建返回true,有就不创建返回false
- file.mkdir(); //返回的是boolean值 创建文件夹,如果没有就创建返回true,有就不创建返回false
file.mkdirs(); //创建多级目录,mkdir则不行 - file1.renameTo(file2); //如果路径名相同,就是改名 如果路径名不同,就是改名并截切
- file.delete(); //如果删除一个文件夹,那么文件夹必须是空的
- file.setReadable(true);
file.setWritable(true);
file.canReadable(); //windows系统认为所有的文件都是可读的
file.canWritable(); //windows系统可以设置为不可写 - file.getAbsolutePath(); //获取绝对路径
file.getPath(); //获取构造方法中传入的路径 File file = new File("路径"); - file.lastModified(); //文件的最后修改时间,返回的是毫秒值
- file.list(); //返回的是String[ ] 存的是指定目录下的所有文件或者文件夹的名称
file.listFiles(); //返回的是File[ ] 获取指定目录下的所有文件或者文件夹 - 文件名称过滤器,因为FilenameFilter是接口,所以用匿名内部类
[code]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("**"); } });
相关文章推荐
- 复习java基础第四天(集合:List、Map、Collections、Enumeration)
- Java基础复习之遍历Map集合
- JavaSE复习之四 基础知识:集合
- JavaSE复习之四 基础知识:集合 补充(1)
- Java基础复习:Map集合常用类 && Collections类 && Arrays类
- JAVA基础复习十八-Map集合、Collections类
- Java基础知识19——集合之Map双列集合以及Map相关面试题
- JavaSE复习之四 基础知识:集合 补充(2)
- JavaSE基础18笔记集合
- Java基础复习:Map集合的输出
- 【Java基础18_1】Map集合
- JavaSE——Day18——Map集合详解
- JavaSE基础复习五:集合和集合框架
- JavaSE基础Map集合
- JavaSE复习之四 基础知识:集合 补充(3)
- JavaSe基础XX18——IO流_4
- JavaSE复习之一 基础知识:Java的基础语法
- 黑马程序员——javase基础--集合
- JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)
- 黑马程序员-JAVASE入门(Map集合)