Java Map集合的详解

一,Map

先说一下Map吧~

如果程序中存储了几百万个学生,而且经常需要使用学号来搜索某个学生,那么这个需求有效的数据结构就是Map

Map是一种依照键(key)存储元素的容器,键(key)很像下标,在List中下标是整数。在Map中键(key)可以使任意类型的对象。Map中不能有重复的键(Key),每个键(key)都有一个对应的值(value)。

一个键(key)和它对应的值构成map集合中的一个元素。

Map中的元素是两个对象,一个对象作为键,一个对象作为值。键不可以重复,但是值可以重复。

看顶层共性方法找子类特有对象.

Map与Collection在集合框架中属并列存在

Map存储的是键值对

Map存储元素使用put方法,Collection使用add方法

Map集合没有直接取出元素的方法,而是先转成Set集合,在通过迭代获取元素

Map集合中键要保证唯一性

也就是Collection是单列集合, Map 是双列集合。

总结: 

Map一次存一对元素, Collection 一次存一个。Map 的键不能重复,保证唯一。

Map 一次存入一对元素,是以键值对的形式存在.键与值存在映射关系.一定要保证键的唯一性.

查看api文档:

interface Map<K,V>

K - 此映射所维护的键的类型

V - 映射值的类型

概念

将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。

特点

Key和Value是1对1的关系,如:门牌号 :家  老公:老婆

双列集合

  1. Map学习体系:
  2. ---| Map 接口 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
  3. ---| HashMap 采用哈希表实现,所以无序
  4. ---| TreeMap 可以对健进行排序
  5. ---|Hashtable:
  6. 底层是哈希表数据结构,线程是同步的,不可以存入 null键, null值。
  7. 效率较低,被HashMap 替代。
  8. ---|HashMap:
  9. 底层是哈希表数据结构,线程是不同步的,可以存入 null键, null值。
  10. 要保证键的唯一性,需要覆盖hashCode方法,和equals方法。
  11. ---| LinkedHashMap:
  12. 该子类基于哈希表又融入了链表。可以Map集合进行增删提高效率。
  13. ---|TreeMap:
  14. 底层是二叉树数据结构。可以对map集合中的键进行排序。需要使用Comparable或者Comparator 进行比较排序。 return 0,来判断键的唯一性。

常见方法

  1. 1、添加:
  2. 1V put(K key, V value) (可以相同的key值,但是添加的value值会覆
  3. 盖前面的,返回值是前一个,如果没有就返回null
  4. 2、putAll(Map<? extends K,? extends V> m) 从指定映射中将所有映射关
  5. 系复制到此映射中(可选操作)。
  6. 2、删除
  7. 1、remove() 删除关联对象,指定key对象
  8. 2、clear() 清空集合对象
  9. 3、获取
  10. 1:value get(key); 可以用于判断键是否存在的情况。当指定的键不存在的时候,返
  11. 回的是 null
  12. 3、判断:
  13. 1boolean isEmpty() 长度为0返回true否则false
  14. 2、boolean containsKey(Object key) 判断集合中是否包含指定的key
  15. 3、boolean containsValue(Object value) 判断集合中是否包含指定的value
  16. 4、长度:
  17. Int size()

添加:

该案例使用了HashMap,建立了学生姓名和年龄之间的映射关系。并试图添加重复的键。

  1. public class Demo1 {
  2. public static void main(String[] args) {
  3. // 定义一个Map的容器对象
  4. Map<String, Integer > map1 = new HashMap<String, Integer >();
  5. map1.put( "jack", 20);
  6. map1.put( "rose", 18);
  7. map1.put( "lucy", 17);
  8. map1.put( "java", 25);
  9. System.out.println(map1);
  10. // 添加重复的键值(值不同),会返回集合中原有(重复键)的值, System.out.println(map1.put("jack", 30)); //20
  11. Map<String, Integer> map2 = new HashMap<String, Integer>();
  12. map2.put( "张三丰", 100);
  13. map2.put( "虚竹", 20);
  14. System.out.println( "map2:" + map2);
  15. // 从指定映射中将所有映射关系复制到此映射中。
  16. map1.putAll(map2);
  17. System.out.println( "map1:" + map1);
  18. //
  19. }
  20. }

删除:

  1. // 删除:
  2. // remove() 删除关联对象,指定key对象
  3. // clear() 清空集合对象
  4. Map<String, Integer> map1 = new HashMap<String, Integer>();
  5. map1.put( "jack", 20);
  6. map1.put( "rose", 18);
  7. map1.put( "lucy", 17);
  8. map1.put( "java", 25);
  9. System.out.println(map1);
  10. // 指定key,返回删除的键值对映射的值。
  11. System.out.println( "value:" + map1.remove( "java"));
  12. map1.clear();
  13. System.out.println( "map1:" + map1);

获取:

  1. // 获取:
  2. // V get(Object key) 通过指定的key对象获取value对象
  3. // int size() 获取容器的大小
  4. Map<String, Integer> map1 = new HashMap<String, Integer>();
  5. map1.put( "jack", 20);
  6. map1.put( "rose", 18);
  7. map1.put( "lucy", 17);
  8. map1.put( "java", 25);
  9. System.out.println(map1);
  10. // V get(Object key) 通过指定的key对象获取value对象
  11. // int size() 获取容器的大小
  12. System.out.println( "value:" + map1.get( "jack"));
  13. System.out.println( "map.size:" + map1.size());

判断:

  1. // 判断:
  2. // boolean isEmpty() 长度为0返回true否则false
  3. // boolean containsKey(Object key) 判断集合中是否包含指定的key
  4. // boolean containsValue(Object value)
  5. Map<String, Integer> map1 = new HashMap<String, Integer>();
  6. map1.put( "jack", 20);
  7. map1.put( "rose", 18);
  8. map1.put( "lucy", 17);
  9. map1.put( "java", 25);
  10. System.out.println(map1);
  11. System.out.println( "isEmpty:" + map1.isEmpty());
  12. System.out.println( "containskey:" + map1.containsKey( "jack"));
  13. System.out.println( "containsvalues:" + map1.containsValue( 100));

遍历Map的方式:

  1. 1、将map 集合中所有的键取出存入set集合。
  2. Set<K> keySet() 返回所有的key对象的Set集合
  3. 再通过get方法获取键对应的值。
  4. 2、 values() ,获取所有的值.
  5. Collection<V> values()不能获取到key对象
  6. 3、 Map.Entry对象 推荐使用 重点
  7. Set<Map.Entry<k,v>> entrySet()
  8. 将map 集合中的键值映射关系打包成一个对象
  9. Map.Entry对象通过Map.Entry 对象的getKey,
  10. getValue获取其键和值。

第一种方式:使用keySet

将Map转成Set集合(keySet()),通过Set迭代器取出Set集合中的每一个元素(Iterator)就是Map集合中的所有的键,再通过get方法获取键对应的值。

  1. public class Demo2 {
  2. public static void main(String[] args) {
  3. Map<Integer, String> map = new HashMap<Integer, String>();
  4. map.put( 1, "aaaa");
  5. map.put( 2, "bbbb");
  6. map.put( 3, "cccc");
  7. System.out.println(map);
  8. //
  9. // 获取方法:
  10. // 第一种方式: 使用keySet
  11. // 需要分别获取key和value,没有面向对象的思想
  12. // Set<K> keySet() 返回所有的key对象的Set集合
  13. Set<Integer> ks = map.keySet();
  14. Iterator<Integer> it = ks.iterator();
  15. while (it.hasNext()) {
  16. Integer key = it.next();
  17. String value = map.get(key);
  18. System.out.println( "key=" + key + " value=" + value);
  19. }
  20. }
  21. }

第二种方式: 通过values 获取所有值,不能获取到key对象

  1. public static void main(String[] args) {
  2. Map<Integer, String> map = new HashMap<Integer, String>();
  3. map.put( 1, "aaaa");
  4. map.put( 2, "bbbb");
  5. map.put( 3, "cccc");
  6. System.out.println(map);
  7. // 第二种方式:
  8. // 通过values 获取所有值,不能获取到key对象
  9. // Collection<V> values()
  10. Collection<String> vs = map.values();
  11. Iterator<String> it = vs.iterator();
  12. while (it.hasNext()) {
  13. String value = it.next();
  14. System.out.println( " value=" + value);
  15. }
  16. }

第三种方式: Map.Entry

public static interface Map.Entry<K,V>

通过Map中的entrySet()方法获取存放Map.Entry<K,V>对象的Set集合。

Set<Map.Entry<K,V>> entrySet()

面向对象的思想将map集合中的键和值映射关系打包为一个对象,就是Map.Entry

,将该对象存入Set集合,Map.Entry是一个对象,那么该对象具备的getKeygetValue获得键和值。

  1. public static void main(String[] args) {
  2. Map<Integer, String> map = new HashMap<Integer, String>();
  3. map.put( 1, "aaaa");
  4. map.put( 2, "bbbb");
  5. map.put( 3, "cccc");
  6. System.out.println(map);
  7. // 第三种方式: Map.Entry对象 推荐使用 重点
  8. // Set<Map.Entry<K,V>> entrySet()
  9. // 返回的Map.Entry对象的Set集合 Map.Entry包含了key和value对象
  10. Set<Map.Entry<Integer, String>> es = map.entrySet();
  11. Iterator<Map.Entry<Integer, String>> it = es.iterator();
  12. while (it.hasNext()) {
  13. // 返回的是封装了key和value对象的Map.Entry对象
  14. Map.Entry<Integer, String> en = it.next();
  15. // 获取Map.Entry对象中封装的key和value对象
  16. Integer key = en.getKey();
  17. String value = en.getValue();
  18. System.out.println( "key=" + key + " value=" + value);
  19. }
  20. }

二,HashMap

底层是哈希表数据结构,线程是不同步的,可以存入null键,null值。要保证键的唯一性,需要覆盖hashCode方法,和equals方法。

案例:自定义对象作为Map的键。

  1. public class Demo3 {
  2. public static void main(String[] args) {
  3. HashMap<Person, String> hm = new HashMap<Person, String>();
  4. hm.put( new Person( "jack", 20), "1001");
  5. hm.put( new Person( "rose", 18), "1002");
  6. hm.put( new Person( "lucy", 19), "1003");
  7. hm.put( new Person( "hmm", 17), "1004");
  8. hm.put( new Person( "ll", 25), "1005");
  9. System.out.println(hm);
  10. System.out.println(hm.put( new Person( "rose", 18), "1006"));
  11. Set<Entry<Person, String>> entrySet = hm.entrySet();
  12. Iterator<Entry<Person, String>> it = entrySet.iterator();
  13. while (it.hasNext()) {
  14. Entry<Person, String> next = it.next();
  15. Person key = next.getKey();
  16. String value = next.getValue();
  17. System.out.println(key + " = " + value);
  18. }
  19. }
  20. }
  21. class Person {
  22. private String name;
  23. private int age;
  24. Person() {
  25. }
  26. public Person(String name, int age) {
  27. this.name = name;
  28. this.age = age;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. public int getAge() {
  37. return age;
  38. }
  39. public void setAge(int age) {
  40. this.age = age;
  41. }
  42. @Override
  43. public int hashCode() {
  44. return this.name.hashCode() + age * 37;
  45. }
  46. @Override
  47. public boolean equals(Object obj) {
  48. if (obj instanceof Person) {
  49. Person p = (Person) obj;
  50. return this.name.equals(p.name) && this.age == p.age;
  51. } else {
  52. return false;
  53. }
  54. }
  55. @Override
  56. public String toString() {
  57. return "Person@name:" + this.name + " age:" + this.age;
  58. }
  59. }
  60. }

三,TreeMap

TreeMap的排序,TreeMap可以对集合中的键进行排序。如何实现键的排序?

方式一:元素自身具备比较性

和TreeSet一样原理,需要让存储在键位置的对象实现Comparable接口,重写compareTo方法,也就是让元素自身具备比较性,这种方式叫做元素的自然排序也叫做默认排序。

方式二:容器具备比较性

当元素自身不具备比较性,或者自身具备的比较性不是所需要的。那么此时可以让容器自身具备。需要定义一个类实现接口Comparator,重写compare方法,并将该接口的子类实例对象作为参数传递给TreeMap集合的构造方法。

注意:当Comparable比较方式和Comparator比较方式同时存在时,以Comparator的比较方式为主;

注意:在重写compareTo或者compare方法时,必须要明确比较的主要条件相等时要比较次要条件。(假设姓名和年龄一直的人为相同的人,如果想要对人按照年龄的大小来排序,如果年龄相同的人,需要如何处理?不能直接return 0,以为可能姓名不同(年龄相同姓名不同的人是不同的人)。此时就需要进行次要条件判断(需要判断姓名),只有姓名和年龄同时相等的才可以返回0.

通过return 0来判断唯一性。

  1. public class Demo4 {
  2. public static void main(String[] args) {
  3. TreeMap<String, Integer> tree = new TreeMap<String, Integer>();
  4. tree.put( "张三", 19);
  5. tree.put( "李四", 20);
  6. tree.put( "王五", 21);
  7. tree.put( "赵六", 22);
  8. tree.put( "周七", 23);
  9. tree.put( "张三", 24);
  10. System.out.println(tree);
  11. System.out.println( "张三".compareTo( "李四")); //-2094
  12. }
  13. }

自定义元素排序

  1. public class Demo3 {
  2. public static void main(String[] args) {
  3. TreeMap<Person, String> hm = new TreeMap<Person, String>(
  4. new MyComparator());
  5. hm.put( new Person( "jack", 20), "1001");
  6. hm.put( new Person( "rose", 18), "1002");
  7. hm.put( new Person( "lucy", 19), "1003");
  8. hm.put( new Person( "hmm", 17), "1004");
  9. hm.put( new Person( "ll", 25), "1005");
  10. System.out.println(hm);
  11. System.out.println(hm.put( new Person( "rose", 18), "1006"));
  12. Set<Entry<Person, String>> entrySet = hm.entrySet();
  13. Iterator<Entry<Person, String>> it = entrySet.iterator();
  14. while (it.hasNext()) {
  15. Entry<Person, String> next = it.next();
  16. Person key = next.getKey();
  17. String value = next.getValue();
  18. System.out.println(key + " = " + value);
  19. }
  20. }
  21. }
  22. class MyComparator implements Comparator<Person> {
  23. @Override
  24. public int compare(Person p1, Person p2) {
  25. if (p1.getAge() > p2.getAge()) {
  26. return - 1;
  27. } else if (p1.getAge() < p2.getAge()) {
  28. return 1;
  29. }
  30. return p1.getName().compareTo(p2.getName());
  31. }
  32. }
  33. class Person implements Comparable<Person> {
  34. private String name;
  35. private int age;
  36. Person() {
  37. }
  38. public Person(String name, int age) {
  39. this.name = name;
  40. this.age = age;
  41. }
  42. public String getName() {
  43. return name;
  44. }
  45. public void setName(String name) {
  46. this.name = name;
  47. }
  48. public int getAge() {
  49. return age;
  50. }
  51. public void setAge(int age) {
  52. this.age = age;
  53. }
  54. @Override
  55. public int hashCode() {
  56. return this.name.hashCode() + age * 37;
  57. }
  58. @Override
  59. public boolean equals(Object obj) {
  60. if (obj instanceof Person) {
  61. Person p = (Person) obj;
  62. return this.name.equals(p.name) && this.age == p.age;
  63. } else {
  64. return false;
  65. }
  66. }
  67. @Override
  68. public String toString() {
  69. return "Person@name:" + this.name + " age:" + this.age;
  70. }
  71. @Override
  72. public int compareTo(Person p) {
  73. if ( this.age > p.age) {
  74. return 1;
  75. } else if ( this.age < p.age) {
  76. return - 1;
  77. }
  78. return this.name.compareTo(p.name);
  79. }
  80. }

 

注意:Set的元素不可重复,Map的键不可重复,如果存入重复元素如何处理

Set元素重复元素不能存入add方法返回false

Map的重复健将覆盖旧键,将旧值返回

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值