Java中集合3_HashMap

1 Map集合

1.1 Map集合概述和特点

* A:Map接口概述

* 查看API可以知道:

* 将键映射到值的对象

* 一个映射不能包含重复的键

* 每个键最多只能映射到一个值

* B:Map接口和Collection接口的不同

* Map是双列的,Collection是单列的

* Map的键唯一,Collection的子体系Set是唯一的

* Map集合的数据结构(hash算法)只针对键有效,跟值无关;Collection集合的数据结构是针对元素有效

1.2 Map集合的功能概述

* A:Map集合的功能概述

* a:添加功能

* V put(K key,V value):添加元素。

* 如果键是第一次存储,就直接存储元素,返回null

* 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

* b:删除功能

* void clear():移除所有的键值对元素

* V remove(Object key):根据键删除键值对元素,并把值返回

* c:判断功能

* boolean containsKey(Object key):判断集合是否包含指定的键

* boolean containsValue(Object value):判断集合是否包含指定的值

* boolean isEmpty():判断集合是否为空

* d:获取功能

* Set<Map.Entry<K,V>> entrySet():拿到所有的键值对象。后面说

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

* Set<K> keySet():获取集合中所有键的集合

* Collection<V> values():获取集合中所有值的集合

* e:长度功能

* int size():返回集合中的键值对的个数

 

package com.jhedu.day19;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo1_Map {
    public static void main(String[] args) {
        demo3();

    }
    public static void demo3() {
        Map<String, Integer> map = new HashMap<>();
        // 返回的值是被覆盖的内容
        // 第一次加入时,集合中并没有任何内容,被覆盖的内容为null,所以返回值时null
        //Map的底层依赖于set,还是set的顶层依赖于Map
        map.put("a",1);
        map.put("b",2);
        map.put("c",3);
        map.put("d",4);
        map.put("e",5);
        boolean b = map.containsKey("a");
        boolean b1 = map.containsKey("f");
        System.out.println(b + "..." + b1);
        boolean b2 = map.containsValue(1);
        boolean b3 = map.containsValue(7);
        System.out.println(b2 + "..." + b3);
        map.clear();
        System.out.println(map.isEmpty());
    }
    public static void demo2() {
        Map<String, Integer> map = new HashMap<>();
        // 返回的值是被覆盖的内容
        // 第一次加入时,集合中并没有任何内容,被覆盖的内容为null,所以返回值时null
        //Map的底层依赖于set,还是set的顶层依赖于Map
        map.put("a",1);
        map.put("b",2);
        map.put("c",3);
        map.put("d",4);
        map.put("e",5);

        // map.clear();
        // 打印集合时,只有输出的内容不是类名 + "@" + 16进制的地址值,
        // 就说该集合重写了Object类的toString方法
        System.out.println(map.size()  + "..." + map);
        Integer i = map.remove("a");
        System.out.println(i);
        System.out.println(map);
    }

    public static void demo() {
        Map<String, Integer> map = new HashMap<>();
        // 返回的值是被覆盖的内容
        // 第一次加入时,集合中并没有任何内容,被覆盖的内容为null,所以返回值时null
        //Map的底层依赖于set,还是set的顶层依赖于Map
        Object b = map.put("a",1);
        Object b1 = map.put("b",2);
        Object b2 = map.put("c",3);
        Object b3 = map.put("d",4);
        Object b4 = map.put("e",5);

        System.out.println(map);
        System.out.println(b + " " + b1 + " " + b2 +" " + b3 + " " + b4);
    }
}

 

1.3 Map集合的遍历之键找值

keySet 返回的是一个Set 集合,set中有迭代器。再调用get方法

public static void demo4() {
    Map<String, Integer> map = new HashMap<>();
    // 返回的值是被覆盖的内容
    // 第一次加入时,集合中并没有任何内容,被覆盖的内容为null,所以返回值时null
    //Map的底层依赖于set,还是set的顶层依赖于Map
    map.put("a",1);
    map.put("b",2);
    map.put("c",3);
    map.put("d",4);
    map.put("e",5);
    Integer i = map.get("a");
    /**
     * 分析:
     * 1. Map中可以通过键获取对应的值 ( Integer i = map.get("a");)
     * 2. 遍历Map时需要首先获得所有键的集合 (Set<String> set = map.keySet())
     * 3. 使用Iterator迭代器遍历上述Set集合
     * 4. 通过Map的键以及get方法可以获取对应的值
     */
    Set<String> set = map.keySet();
    Iterator<String> it = set.iterator();
    while(it.hasNext()){     // 判断集合中是否有元素
        String key = it.next();     // 取到集合中对应的键
        Integer value = map.get(key) ;
        System.out.println(key + "...." + value);
    }
}

1.4 Map集合的遍历之键值对对象找键和值

 * A:键值对对象找键和值思路:

* 获取所有键值对对象的集合

* 遍历键值对对象的集合获取到每一个键值对对象

* 根据键值对对象找键和值

* B:案例演示

* Map集合的遍历之键值对对象找键和值

 

package com.se.Map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Demo04_Test {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("张三", 23);
        map.put("李四", 24);
        map.put("王五", 25);
        map.put("赵六", 26);
        
        //通过map的keySet()方法
        Set<String> set = map.keySet();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            Integer value = map.get(key);
            System.out.println(value);
        }

        for (String s :
                set) {
            Integer value = map.get(s);
            System.out.println(value);
        }
        
        //通过map的entrySet()方法
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); //获取所有的键值对象的集合
        Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator(); //获取迭代器
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> en = iterator.next(); //获取键值对对象
            String key = en.getKey();                       //根据键值对对象获取键
            Integer value = en.getValue();                  //根据键值对对象获取值
            System.out.println(key + "..." + value);
        }


        for (Map.Entry<String, Integer> en : map.entrySet()) {
            System.out.println(en.getKey() + "=" + en.getValue());
        }
    }
}

 

1.5 HashMap集合键是Student 值是String的案例

* A:案例演示

* HashMap集合键是Student值是String的案例

HashMap<Student, String> hm = new HashMap<>();

hm.put(new Student("张三", 23), "北京");

这里面的键相当于hashSet里面的元素 hashSet要求如果是自定义对象必须重写hashcodehe equals方法

 

package com.se.Map;

import java.util.HashMap;

public class Demo03_Map {
    public static void main(String[] args) {
        // 当map集合中键值为自定义对象时,我们必须重写equals方法和hashCode方法
        HashMap<Student, String> map = new HashMap<>();
        map.put(new Student("张三",20),"焦作");
        map.put(new Student("李四",18),"许昌");
        map.put(new Student("王五",16),"漯河");
        System.out.println(map);
        String address = map.get(new Student("王五", 16));
        System.out.println(address);
    }
}

 

2 LinkedHashMap的概述和使用

* A:案例演示

* LinkedHashMap的特点

* 底层是链表实现的可以保证怎么存就怎么取

package com.jhedu.day19;
import java.util.LinkedHashMap;
import java.util.Map;
public class Demo4_LinkedHashMap {
    public static void main(String[] args) {
        Map<String, Integer> map = new LinkedHashMap<>();
        map.put("张三",23);
        map.put("李四",24);
        map.put("王五",25);
        map.put("赵六",26);
        System.out.println(map.toString());
    }
}

2.1 TreeMap集合键是Student值是String的案例

 

package com.jhedu.day19;
import java.util.*;
public class Demo5_TreeMap {
    public static void main(String[] args) {
        //2. 比较器排序 或者重写comparator接口下的compare方法
        TreeMap<Student, String> map = new TreeMap<>(
                new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        int num = o1.getName().compareTo(o2.getName());
                        return num==0?o1.getAge()-o2.getAge():num;
                    }
                }
        );
        map.put(new Student("张三", 23),"北京");
        map.put(new Student("张三",23), "北京");
        map.put(new Student("李四",24),"上海");
        map.put(new Student("王五",25),"广州");
        map.put(new Student("赵六",26),"深圳");
        map.put(new Student("张三",23), "郑州");

        for(Map.Entry<Student,String> s: map.entrySet()){
            System.out.println(s.getKey() + "..." + s.getValue());
        }
    }
    public static void demo2() {
        // 1. 字典顺序排序:当TreeSet集合中的键是自定义对象时,必须重写comparable接口下的compareTo方法
        TreeMap<Student, String> map = new TreeMap<>();
        map.put(new Student("张三", 23),"北京");
        map.put(new Student("张三",23), "北京");
        map.put(new Student("李四",24),"上海");
        map.put(new Student("王五",25),"广州");
        map.put(new Student("赵六",26),"深圳");
        map.put(new Student("张三",23), "郑州");
        System.out.println(map);
    }
    public static void demo1() {
        TreeMap<String, Integer> map = new TreeMap<>();
        // 返回的值是被覆盖的内容
        // 第一次加入时,集合中并没有任何内容,被覆盖的内容为null,所以返回值时null
        //Map的底层依赖于set,还是set的顶层依赖于Map
        map.put("d",4);
        map.put("e",5);
        map.put("a",1);
        map.put("b",2);
        map.put("c",3);
        System.out.println(map);  // TreeMap会按照键的字典顺序进行排序
    }
}

 

2.2 HashMapHashtable的区别

 

* A:面试题

 

* HashMap和Hashtable的区别

 

* HashtableJDK1.0版本出现的,是线程安全的,效率低,HashMap是JDK1.2版本出现的,是线程不安全的,效率高

 

* Hashtable不可以存储null键和null值,HashMap可以存储null键和null值

 

 

 

 

原文地址:https://www.cnblogs.com/LEPENGYANG/p/15022376.html