Map接口

:::tips

特点(JDK8):

  • Map与Collection并列存在,用于保存具有映射关系的数据:Key - Value
  • Map中key和value可以是任何数据类型的数据,会封装到HashMap$Node对象中
  • Map中的key不允许重复,value可以重复
  • Map中的key可以为null有且只有一个,value可为多个
  • 常用String类作为Map的key
  • key和value存在单向一对一关系
  • Map存放数据的k-y存放在HashMap$Node对象中,因为Node实现了Entry接口,所有一对k-v就是一个Entry
    • k-v 最后是HashMap$Node node = newNode(hash, key, value, null)
    • k-v 为了方便程序员的遍历,还会创建EntrySet集合,该集合存放的元素类似是Entry,而一个Entry对象就有k,v,EntrySet>即:transient Set> entrySet;
    • entrySet中,定义类型是Map.Entry,但实际存放的还是HashMap$Node,这是因为static class Node implements Map.Entry
    • 当把HashMap$Node对象存放到entrySet就方便我们遍历,因为Map.Entry提供了k getKey();V getValue();方法

常用方法

  • V put(K key,V value) 添加元素
  • V get(Object key) 根据指定的键,在Map集合中获取对应的值
  • V remove(Object key) 根据键删除键值对元素
  • void clear() 移除所有的键值对元素
  • boolean containsKey(Object key) 判断集合是否包含指定的键
  • boolean containsValue(Object value) 判断集合是否包含指定的值
  • boolean isEmpty() 判断集合是否为空
  • int size() 集合的长度,也就是集合中键值对的个数

Map遍历方式

键找值方式
  • 通过元素中的键,获取键所对应的值分析步骤:

    • 获取Map中所有的键,方法提示: keySet()
    • 遍历键的Set集合,得到每一个键
    • 根据键,获取键所对应的值。方法提示: get(K key)
      常用方法
  • Set keySet() 获取所有键的集合

  • V get(Object key) 根据键获取值

键值对方式
  • Map.Entry接口
    • Map中将每个键和值封装成一个个的Entry对象
    • 并提供 getKey() 和 getValue() 方法,用于获取Entry中封装的键和值
  • 分析步骤

    • 获取map集合中所有 Entry 对象的集合: entrySet()方法
    • 遍历Entry的集合,获取每个Entry对象
    • 使用 Entry 提供的方法获取键和值:getKey() getValue()
      常用方法
  • Set> entrySet() 获取所有Entry对象的集合

  • K getKey() 获得键
  • V getValue() 获得值 :::

    HashMap

    :::tips

    概述

  • 底层采用的哈希表结构,元素的存取无序。(HashSet的底层其实是借助了HashMap实现存储)

  • 要保证键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。

特点

  • HashMap底层是哈希表结构+链表/红黑树
  • 依赖hashCode方法和equals方法保证键的唯一
  • 如果键要存储的是自定义对象,需要重写hashCode和equals方法 :::

    TreeMap

    :::tips

    概述:

  • TreeMap集合底层的数据结构是红黑树;可以对元素的键进行排序

  • 排序方式有两种:自然排序,比较器排序。(键的排序规则与TreeSet一样)

特点

  • TreeMap是Map里面的一个实现类
  • TreeMap底层是红黑树结构;可以对元素的键进行排序(TreeSet底层就是使用TreeMap实现存储的)
  • 排序方式有两种:自然排序,比较器排序。

TreeMap的排序

  • 自然排序:使用无参构造器创建TreeMap集合,存入集合的键需要实现Comparable接口
  • 比较器排序:使用带参构造器创建TreeMap集合,传入Comparator接口的实现类 ::: ```java public static void main(String[] args) { HashMap map = new HashMap<>(); map.put(“id01”, “张一”); //如果键不重复,则返回null String s1 = map.put(“id02”, “张二”); map.put(“id03”, “张三”); //如果键重复,新的值会更新存入,旧的值会被返回 String s2 = map.put(“id03”, “张四”); //System.out.println(s1);//null //System.out.println(s2);//张三

    //获取数据get(Object key),返回value //String value = map.get(“id01”); //System.out.println(value);

    //remove(Object key),返回value //String value = map.remove(“id01”); //System.out.println(value);

    //清空 //map.clear();

    //判断键或者值是否存在,键唯一,值多个 boolean b1 = map.containsKey(“id01”); boolean b2 = map.containsKey(“张一”); System.out.println(b1); System.out.println(b2);

    System.out.println(map);

}

  1. ```java
  2. public static void main(String[] args) {
  3. HashMap<String, String> map = new HashMap<>();
  4. map.put("孙策", "大桥");
  5. map.put("周瑜", "小乔");
  6. map.put("吕布", "貂蝉");
  7. //1.获取所有键值对对象 Map.Entry<K, V>
  8. /*
  9. set集合
  10. Map.Entry<String, String>集合的元素
  11. */
  12. Set<Map.Entry<String, String>> set = map.entrySet();
  13. //2.遍历集合
  14. for (Map.Entry<String, String> entry : set) {
  15. String key = entry.getKey();
  16. String value = entry.getValue();
  17. System.out.println(key + ", " + value);
  18. }
  19. }
public static void main(String[] args) {
    HashMap<String, String> map = new HashMap<>();
    map.put("孙策", "大桥");
    map.put("周瑜", "小乔");
    map.put("吕布", "貂蝉");

    //1.获取所有键的集合
    Set<String> set = map.keySet();

    //2.遍历集合
    for (String key : set) {
        //3.根据键获取值
        String value = map.get(key);
        System.out.println(key + ", " + value);
    }
}
/*
    创建一个HashMap集合,键是学生对象(Student),值是家庭住址(String)。
    存储三个键值对元素,并遍历
    要求:学生和年龄相同表示同一个对象,不能存入集合
 */
public static void main(String[] args) {
    HashMap<Student, String> map = new HashMap<>();
    map.put(new Student("张一", 21), "北京");
    map.put(new Student("张二", 22), "上海");
    map.put(new Student("张三", 23), "广州");
    map.put(new Student("张三", 23), "深圳");

    Set<Map.Entry<Student, String>> entrySet = map.entrySet();
    for (Map.Entry<Student, String> e : entrySet) {
        Student key = e.getKey();
        String value = e.getValue();
        System.out.println(key.getName() + ", " + key.getAge() + ", " + value);
    }
}
public static void main(String[] args) {
    TreeMap<Student, String> treeM = new TreeMap<>(new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            return o2.getAge() - o1.getAge();
        }
    });
    treeM.put(new Student("张一", 21), "北京");
    treeM.put(new Student("张二", 22), "上海");
    treeM.put(new Student("张三", 23), "广州");
    treeM.put(new Student("张四", 23), "深圳");

    Set<Map.Entry<Student, String>> entrySet = treeM.entrySet();
    for (Map.Entry<Student, String> e : entrySet) {
        Student key = e.getKey();
        String value = e.getValue();
        System.out.println(key.getName() + ", " + key.getAge() + ", " + value);
    }
}
public class Student implements Comparable<Student>{
    private String name;
    private int age;
    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
    //构造方法setgettoString方法
}
//录入一个字符串,获取每个元素出现的次数
//aabcacbabd --> a(4) b(3) c(2) d(1)
public static void main(String[] args) {
    //创建TreeMap集合,键是字符,值是次数
    TreeMap<Character, Integer> treeM = new TreeMap<>();
    //键盘录入
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入字符串");
    String str = sc.next();
    //for循环取出字符串的每个字符
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        //判断字符是否已经存在集合中
        if (treeM.containsKey(ch)) {
            //如果存在,取出出现的数值
            Integer count = treeM.get(ch);
            //更新集合中的值
            treeM.put(ch, count + 1);
        } else {
            //如果不存在则将数值设置为1
            treeM.put(ch, 1);
        }
    }
    //遍历集合
    Set<Map.Entry<Character, Integer>> entrySet = treeM.entrySet();
    for (Map.Entry<Character, Integer> entry : entrySet) {
        Character key = entry.getKey();
        Integer value = entry.getValue();
        System.out.print(key + "(" + value + ") ");
    }
}