Map汇合

Map汇合是用于保留键值对数据的汇合,汇合中保留着两种数据,key值 和 value值,key值不能反复,value值能够是任意数据类型,任意值。

HashMap

底层是通过哈希表实现的,因为是异步存储,线程不平安,用做key值的对象必须实现hashCode办法和equals办法,不能保障键值对的存储数据。

import java.util.*;public class main {    public static void main(String[] args) {          //1.Map汇合办法          Map<String, Integer> map=new HashMap<String, Integer>();          Integer i1=map.put("张三", 23);          Integer i2=map.put("li三", 23);          Integer i3=map.put("李四", 23);          Integer i4=map.put("王五", 23);//雷同的键不存储,值笼罩,把被笼罩的值返回          //map.remove("张三");//依据键删除值          Collection<Integer> c=map.values();          System.out.println(c);//获取对象中所有的值          System.out.println(map.containsKey("张三"));//是否蕴含传入的键          System.out.println(map.containsValue(23));//是否蕴含传入的值          //2.遍历Map汇合          //办法一:(不简便)          Map<String, Integer> map1=new HashMap<String, Integer>();          map1.put("张三", 23);          map1.put("李四", 23);          map1.put("王五", 23);          //加强for循环          for (String key : map1.keySet()) { //map.keyset()是所有键的汇合              System.out.println(key+"."+map1.get(key));          }          //办法二:(举荐)          Map<String, Integer> map2=new HashMap<String, Integer>();          map2.put("张三", 11);          map2.put("李四", 12);          map2.put("王五", 13);          Set<Map.Entry<String, Integer>> entrySet=map2.entrySet();//将键和值封装成Entry对象,并存储在Set汇合中          //获取每一个对象          Iterator<Map.Entry<String, Integer>> it=entrySet.iterator();          for (Map.Entry<String, Integer> entry : entrySet) {              System.out.println(entry.getKey()+" "+entry.getValue());          }    }}

HashMap和Hashtable的区别

  • 共同点:都是双链汇合,底层都是哈希算法
  • 区别:
    1.HashMap是线程不平安的,效率高,jdk1.2版本
    2.Hashtable是线程平安的,效率低,JDK1.0版本
    3.hashMap能够存储null键和null值
    4.hashtable不能存储null键和null值

LinkedHashMap

和HashSet中的LinkedHashSet一样,HashMap也有一个LinkedHashMap子类,应用双向链表来保护键值对的秩序,迭代程序和插入程序保持一致。

TreeMap

底层是通过红黑树实现的,通过红黑树对所有的key进行排序,非线程平安。

TreeMap有两种排序办法:

  • 天然排序:TreeMap的所有key必须实现Comparable接口,而且所有key应该是同一个类的对象,否则将会抛出ClassCastExcepiton异样。


  • 定制排序:创立TreeMap时,传入一个Comparator对象,该对象负责对TreeMap中所有key进行排序。采纳定制排序时不要求Map的key实现Comparable接口。
TreeMap中判断两个key相等的规范也是两个key通过equals()办法比拟返回true,而通过compareTo()办法返回0,TreeMap即认为这两个key是相等的。

间接看实例:

 import java.util.*;class aTreeMap {    public static void main(String[] args) {        cat c1 = new cat("red", 30);        cat c2 = new cat("black", 20);        cat c3 = new cat("white", 10);        cat c4 = new cat("white", 10);        Map<cat, Integer> treeMap = new TreeMap<cat, Integer>();        treeMap.put(c1, 10);        treeMap.put(c2, 15);        treeMap.put(c3, 5);        treeMap.put(c4, 20);        for (Map.Entry<cat, Integer> entry : treeMap.entrySet()) {            System.out.println(entry.getKey() + " = " + entry.getValue());        }        }}class cat implements Comparable<cat>{    String color;    int count;    cat(String c, int s) {        color = c;        count = s;    }    public String toString(){        return color + " cat";    }    @Override    public int compareTo(cat o) {        return  o.count - this.count;    }}

运行后果:

总结

  • HashMap:实用于Map中插入、删除和定位元素。
  • Treemap:实用于按天然程序或自定义程序遍历键(key)。