java集合框架:Map集合

Map集合

  • public interface Map<K,V>两个泛型,将键key映射到值value的对象。一个映射不能包含重复的键每个键最多只能映射到一个值

  • Collection中的集合,元素是孤立存在的,向集合中存储元素采用一个个元素的方式存储;

  • Map中的集合,元素是成对存在的,通过键可以找到所对应的值

  • 键和值的数据类型可以相同,也可以不相同

  • Collection中的集合称为单列集合,Map中的集合称为双列集合

Map常用子类

HashMap<K,V>类

  • 底层是哈希表,查询速度特别快
  • 是一个无序的集合

LinkedHashMap<K,V>类

  • 底层是哈希表+链表
  • 是一个有序的集合



Map接口中常用方法

  • public V put(K key,V value):把指定的键与值添加到Map集合中,返回之前与 key 关联的值,如果没有针对 key 的映射关系,则返回 null

  • public V remove(Object key):把指定的键所对应的键值删除,返回之前与 key 关联的值,如果没有针对 key 的映射关系,则返回 null

  • public V get(Object key):根据指定的键,在Map集合中获取对应的值,如果此映射不包含该键的映射关系,则返回 null

  • boolean containsKey(Object key):判断集合中是否包含指定的键的映射关系

  • public Set<K> keySet()可以用来遍历Map集合 通过键找值的方式

    • keySet() :获取Map集合中所有的键,存储到Set集合中

    • 使用迭代器/增强for 遍历Set集合,可以获取Map集合中的每一个键

    • 最后通过Map集合中的get(key)方法,获取value

public class MapDemo1 {
    public static void main(String[] args) {
        Map<String,Integer> map=new HashMap<>();
        map.put("zhangsan",111);
        map.put("lisi",222);
        map.put("wanger",333);

        //Set<K> keySet()方式来遍历Map集合
        Set<String> set=map.keySet();//把所有的key存到set集合中
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            String key = it.next();//迭代器遍历key
            Integer value = map.get(key);//通过key找value
            System.out.println(key+"--"+value);
        }
    }
}
//结果
//lisi--222
//zhangsan--111
//wanger--333
  • public Set< Map, Entry<K,V> > entrySet():获取到Map集合中所有的键值对对象
    • entrySet():把Map集合内部的多个Entry对象取出来存储到一个Set集合中
    • 遍历Set集合获取每一个Entry对象
    • 通过Entry对象中的方法获取key和value:getKey() getValue
public class MapDemo1 {
    public static void main(String[] args) {
        Map<String,Integer> map=new HashMap<>();
        map.put("zhangsan",111);
        map.put("lisi",222);
        map.put("wanger",333);

        //Set< Map, Entry<K,V> > entrySet()
        Set<Map.Entry<String, Integer>> set = map.entrySet();//把Entry对象存到set集合中
        Iterator<Map.Entry<String, Integer>> it = set.iterator();
        while (it.hasNext()){
            Map.Entry<String, Integer> entry = it.next();//遍历所有的Entry对象
            String key = entry.getKey();//使用Entry的方法获取键
            Integer value = entry.getValue();//使用Entry的方法获取值
            System.out.println(key+"--"+value);
        }
    }
}
//结果
//lisi--222
//zhangsan--111
//wanger--333

Entry键值对对象:接口 Map.Entry<K,V>

  • Map接口中的内部接口Entry
  • 作用:当Map集合创建时,就会在Map集合中创建一个Entry对象,用来记录键与值
  • K getKey() :返回与此项对应的键
  • V getValue() :返回与此项对应的值。



HashMap存储自定义类型键值

  • Map集合要保证key是唯一的:作为key的元素,必须重写hashCode方法和equals方法
  • HashMap存储自定义类型键值:
    • key: Person类型:必须重写hashCode方法和equals方法
    • value: String类型:可以重复
  • 可以存储null值,null键
public class Person {
    private String name;
    private int age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
public class HashMapSavePerson {
    public static void main(String[] args) {
        HashMap<Person,String> map=new HashMap<>();
        map.put(new Person("aaa",11),"bb");
        map.put(new Person("aaa",18),"bb");
        map.put(new Person("aaa",11),"bb");
        map.put(new Person("bbb",18),"bb");
        Set<Map.Entry<Person, String>> set = map.entrySet();
        for (Map.Entry<Person,String> entry:set){
            Person key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"---"+value);
        }
    }
}
//结果
//Person{name='aaa', age=11}---bb
//Person{name='aaa', age=18}---bb
//Person{name='bbb', age=18}---bb



LinkedHashMap集合

  • 继承了HashMap<K,V>集合

  • 底层是哈希表+链表(记录元素的顺序)

  • key不能重复,是有序的集合

Hashtable集合

  • 是最早期的双列集合
  • 底层是一个哈希表,是一个线程安全的集合,单线程集合,速度慢
  • 不能存储null值,null键,会报空指针异常
  • 和Vector一样,已经被更先进的集合(ArrayList,HashMap)取代了
  • Hashtable的子类Properties依然活跃在历史舞台(Properties是唯一个和IO流相结合的集合)
 HashMap<String,String> map = new HashMap<>();
        map.put(null,"a");
        map.put("a",null);
        map.put("b",null);
        map.put(null,"b");
        System.out.println(map);//{null=b, a=null, b=null}
        Hashtable<String,String> map1 = new Hashtable<>();
        map1.put(null,"a");//NullPointerException
        map1.put("a",null);//NullPointerException
        map1.put("b",null);//NullPointerException
        map1.put(null,"a");//NullPointerException
        System.out.println(map1);

posted @ 2020-12-31 09:55  迪迦是真的  阅读(128)  评论(0)    收藏  举报
//复制代码按钮 //代码行号 //评论