Map接口与实现类
Map集合
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aypGpS63-1617108697435)(D:\学习\tupian\集合框架\集合\map.png)]](https://i-blog.csdnimg.cn/blog_migrate/5ce30a100cb26f1cd369750e96ffc1cc.png#pic_center)
-
特点:
- 用于储存任意键值对(Key-Value)
- 键:无序、无下标、不允许重复(唯一)
- 值:无序、无下标、允许重复
-
方法:
- V put(K key,V value) // 将对象存入到集合中,关联键值。key重复则覆盖原值。
- Object get(Object key) // 根据键获取对应的值。
- Set // 返回所有key。
- Collection values() // 返回包含所有值的Collection集合。
- Set<Map.Entry<K,V>> // 键值匹配的Set集合。
-
代码示例:
// 创建Map集合 Map<String, String> map = new HashMap<>(); // 添加元素 map.put("cn","中国"); map.put("uk","英国"); map.put("usa","美国"); map.put("cn","zhongguo"); System.out.println("元素个数:"+map.size()); System.out.println(map.toString()); // 删除 map.remove("usa"); System.out.println("删除之后:"+map.size()); // 遍历 // 使用keySet(); System.out.println("-----keySet()------"); // Set<String> keyset = map.keySet(); for(String key : map.keySet()){ System.out.println(key+"---"+map.get(key)); } // 使用entrySet()方法 Set<Map.Entry<String, String>> entriex = map.entrySet(); for(Map.Entry<String, String> entry : entries){ System.out.println(entry.getKey()+"----"+entry.getValue()); } // 判断 System.out.println(map.containsKey( "con")); System.out.println(map.containsValue("泰国"));
Map集合的实现类
-
HashMap【重点】:
-
JDK1.2 版本,线程不安全,运行效率快;允许用null 作为key或是value。
-
案例:
public class Student{ private String name; private int stuNo; public Student() } public class Demo2{ public static void main(String[] atgs){ // 创建集合 HashMap<Student,String> students = new HashMap<Student,String>(); // 添加元素 Student s1 = new Student("张三",12) Student s2 = new Student("斯",22) Student s3 = new Student("三",32) students.put(s1, "北京"); students.put(s2, "上海"); students.put(s3, "杭州"); students.put(s3, "南京"); // 后put值会替换前面的 students.put(new Student("三",32),"南京") // 这样可以加进去为什么? // 是因为这个对象地址是重新new了的,地址不一样,所以才会加进去 使用key可hashcode和equals作为重复,这时我们要去重,再Student里重写equals方法 System.out.println("元素个数:"+students.size()); System.out.println(sutdents.toString()); // 删除 students.remove(s1); System.out.println("删除之后:"+students.size()); // 遍历 for(Sutdent key : students.keySet()){ System.out.println(key.toString()+"========"+students.get(key)); } // 使用entrySet for(Map.Entry<Student.String> entry : students.entrySet()){ System.out.println(entry.getKey()+"====="+entry.getValue()); } // 判断 System.out.println(students.containsKey(new Student("三",32))); System.our.ptintln(students.containsValues("杭州") } }
-
-
HashMap【重点】:
- JDK1.2版本,线程不安全,运行效率快;允许用null 作为key或是value。
-
Hashtable:
- JDK1.0版本,线程安全,运行效率慢;不允许null作为key或是value。
-
Properties:
- Hashtable 的子类,要求key和value都是String。通常用于配置文件的读取。
-
TreeMap:
-
实现了SortedMap接口(是Map的子接口),可以对key自动排序。
-
例:
// 新建集合 TreeMap<Student,String> treeMap = new TreeMap<Strudent,String>(); // 添加元素 Student s1 = new Student("送悟空",100); Student s2 = new Student("猪八戒",101); Student s3 = new Student("沙和尚",102); treeMap.put(s1,"北京"); treeMap.put(s2,"上海"); treeMap.put(s3,"青岛"); treeMap.put(new Student("沙和尚",102),"南京");// 这时不会添加到第四个,因为它的键重复了,但是会把值替换掉。 System.out.println("元素个数"); // 删除 treeMap.remove(s3); System.out.println(treeMap.size()); // 遍历 for(Student key :treeMap.keySet()){ System.our.println(key+"-----"+treeMap.get(key)); } for(Map.Entry<Student,String> entry : treeMap.entrySet){ System.our.println(entry.getKey()+"----"+entry.getValue()); } // 判断 System.our.println(treeMap.containsKey(s1));
-
771





