Java中Map集合(三十二)

Map集合

在现实生活中,有非常多的东西,是和另外一种东西对应的,并且还是唯一的,比如:身份证号与个人,个人与手机,一夫一妻。。。等,这种关系就是对应关系,又叫做映射。Java为这种数据类型提供了专门的接口,就是Map<K,V>接口。

Map<K,V>集合的特点

  • Map<K,V>集合是一个双列集合,一个元素包含两个值。
  • Map<K,V>集合中的元素,key和value的数据类型可以相同,也可以不相同。
  • Map<K,V>集合中的元素,key是不允许重复的,但是value可以重复。
  • Map<K,V>集合中的元素,key和value是一一对应的。

Map<K,V>集合与Collection的区别

  • Collection 中的集合,元素是孤立存在的,而Map<K,V>集合中的元素是成对出现的。
  • Collection 集合称为单列集合,Map<K,V>集合称为双列集合。

Map<K,V>的常用方法

Map<K,V>中定义了很多方法,都是它的子类常用的方法,只要会用这几个方法,Map集合就等于会了,只用再了解一下,每一个子类的做用就行了。

方法

  • public V put(K key,V value); 把指定的键与指定折值添加到Map集合中。
  • public V remove(Object key); 把指定的键所对应的键值对元素在Map集合中删除,返回被删除元素的值。
  • public V get(Object key); 根据指定的键,在Map集合中取对应的值。
  • public Set<K> keySet(); 获取Map集合中所有的键,存到Set集合中去。
  • public Set<Map.Entry<K,V>> entrySet(); 获取Map集合中所有的键值对对象的集合。

使用

public V put(K key,V value); 把指定的键与指定折值添加到Map集合中。

    Map<Integer, String> map = new HashMap<>();
    map.put(1, "a");
    map.put(2, "b");
    map.put(3, "c");
    map.put(4, "d");
    //只可以存储一个null键
    map.put(null, "e");
    //Map集合是无序的
    System.out.println(map);    //{null=e, 1=a, 2=b, 3=c, 4=d}

public V remove(Object key); 把指定的键所对应的键值对元素在Map集合中删除,返回被删除元素的值。

   //数据为上面的数据
   //删除key为null的元素
    String remove = map.remove(null);
    //返回值是被删除的元素的value
    System.out.println(remove); //e
    System.out.println(map);    //{1=a, 2=b, 3=c, 4=d}

public V get(Object key); 根据指定的键,在Map集合中取对应的值。

   String s = map.get(2);
   System.out.println(s);      //b
   System.out.println(map);    //{null=e, 1=a, 2=b, 3=c, 4=d}

public Set<K> keySet(); 获取Map集合中所有的键,存到Set集合中去。

   //取出Map集合中所有的key
   Set<Integer> integers = map.keySet();
   //对Map集合进行遍厉的时候才使用的
   for (Integer integer :integers) {
       //用get方法通过key来获取值
       String s = map.get(integer);
       System.out.println(s);
   }
   //也可以使用Set集合里面的迭代器来进行循环

public Set<Map.Entry<K,V>> entrySet(); 获取Map集合中所有的键值对对象的集合。

    //这也是一种遍厉Map集合的方法
    Set<Map.Entry<Integer, String>> entries = map.entrySet();
    for (Map.Entry<Integer, String> entry : entries) {
        //用Map.Entry里面的getKey和getValue方法来获取key和value
        System.out.print("key:" + entry.getKey());
        System.out.println("value:" + entry.getValue());
        /*
            key:nullvalue:e
            key:1value:a
            key:2value:b
            key:3value:c
            key:4value:d
         */
    }

Entry<K,V>Map接口,中是一个内部接口,并且在所有的实现类中实现,这个实现类的做用是用来记录Map集合中所有的Key和对应的Value

HashMap<K,V>集合

存储数据用的是哈希表结构,元素存取是无序。因为要保证唯一,所以必须要保证,key的对象必须要重写hashCode()方法和equals()方法。


因为基本数据类型里面都重写了这两个方法,所以我自定义一个对象做为key值,看看重写了这两个方法和不重写了这两个方法的区别。
重写:

    Map<Student, String> stuMap = new HashMap<>();
    stuMap.put(new Student("张三", 18), "不重写");
    stuMap.put(new Student("张三", 18), "不重写");
    stuMap.put(new Student("王五", 13), "不重写");
    stuMap.put(new Student("赵六", 15), "不重写");
    System.out.println(stuMap);//四个数据全有
    //{Student{name='王五', age=13}=不重写, Student{name='张三', age=18}=不重写, 
    // Student{name='张三', age=18}=不重写, Student{name='赵六', age=15}=不重写}

不重写:

    Map<Student, String> stuMap = new HashMap<>();
    stuMap.put(new Student("张三", 18), "重写");
    stuMap.put(new Student("张三", 18), "重写");
    stuMap.put(new Student("王五", 13), "重写");
    stuMap.put(new Student("赵六", 15), "重写");
    System.out.println(stuMap);//重复的张三没有了
    //{Student{name='张三', age=18}=不重写, Student{name='赵六', age=15}=不重写, 
    // Student{name='王五', age=13}=不重写}

上面的重复虽然保证唯一了,但是没有保证有序,所以我们就要用到LinkedHashMap集合。

LinkedHashMap<K,V>集合

LinkedHashMapHashMap下面的子类,HashMap的所有的方法,LinkedHashMap都有,但是LinkedHashMapHashMap好用的一点是,它可以保证存取的顺序一致。

    Map<Student, String> stuMap = new LinkedHashMap<>();
    stuMap.put(new Student("张三", 18), "重写");
    stuMap.put(new Student("张三", 18), "重写");
    stuMap.put(new Student("王五", 13), "重写");
    stuMap.put(new Student("赵六", 15), "重写");
    System.out.println(stuMap);//顺序和存的顺序一样了
    //{Student{name='张三', age=18}=重写, Student{name='王五', age=13}=重写, 
    //Student{name='赵六', age=15}=重写}

上面的这两个实现类都是同步的!


细节决定成败!
个人愚见,如有不对,恳请扶正!

猜你喜欢

转载自www.cnblogs.com/xdtg/p/12671661.html
今日推荐