Map与List、Set接口不同,它是由一系列键值对组成的集合,提供了key到Value的映射。同时它也没有继承Collection。在Map中它保证了key与value之间的一一对应关系。也就是说一个key对应一个value,所以它不能存在相同的key值,当然value值可以相同。实现map的有:HashMap、TreeMap、HashTable、Properties、EnumMap。字典(Dictionary)
一、HashMap
以哈希表数据结构实现,查找对象时通过哈希函数计算其位置,它是为快速查询而设计的,其内部定义了一个hash表数组(Entry[] table),元素会通过哈希转换函数将元素的哈希地址转换成数组中存放的索引,如果有冲突,则使用散列链表的形式将所有相同哈希地址的元素串起来,可能通过查看HashMap.Entry的源码它是一个单链表结构。
key:键,唯一,钥匙,关键的
二、Map基本操作
1、基本数据
package com.ft.suanfa; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map.Entry; import java.util.TreeMap; public class NewYearAndFirstDemo { public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String,Integer>(); //向map中添加元素 map.put("Tome", 26); map.put("Jack", 18); map.put("Micky", 17); map.put("Kate", 16); //根据key获取value System.out.println("Jack is "+map.get("Jack")+ "years old"); //移除 map.remove("Micky"); //遍历map for (Entry<String,Integer> entry : map.entrySet()) { System.out.println("name: "+entry.getKey()+" age: "+entry.getValue()); } //key相同的元素将被覆盖 map.put("Jack", 19); System.out.println("Jack is "+map.get("Jack")+ "years old"); //判断是否包含某个Key if(map.containsKey("Tom")) { System.out.println("The map include the value 26"); } //判断map是否为空 if(!map.isEmpty()) { System.out.println("The map size="+map.size()); } //获取key的集合 for (String str : map.keySet()) { System.out.println(str); } TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>(); treeMap.putAll(treeMap); for (Entry<String,Integer> entry : treeMap.entrySet()) { System.out.println("name: "+entry.getKey()+" age: "+entry.getValue()); } LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<String, Integer>(); linkedHashMap.put("Tom", 26); linkedHashMap.put("Jack", 18); linkedHashMap.put("Micky", 17); linkedHashMap.put("Kate", 15); for (Entry<String,Integer> entry : linkedHashMap.entrySet()) { System.out.println("name: "+entry.getKey()+" age: "+entry.getValue()); } } }
打印输出:
Jack is 18years old
name: Tome age: 26
name: Kate age: 16
name: Jack age: 18
Jack is 19years old
The map size=3
Tome
Kate
Jack
name: Tom age: 26
name: Jack age: 18
name: Micky age: 17
name: Kate age: 15
2、Student类
package com.ft.suanfa.demo0104; import java.util.HashMap; import java.util.Map.Entry; public class MapTest { public static void main(String[] args) { HashMap<String, Student> map = new HashMap<String, Student>(); Student tom = new Student("201701", "张学友", "男", 99); Student rose = new Student("201702", "陈慧娴", "女", 98); map.put(tom.no, tom); map.put(rose.no, rose); map.put("201703", new Student("201703", "陈奕迅", "男", 97)); map.put("201704", new Student("201704", "黄家驹", "男", 96)); map.put("201705", new Student("201705", "川普", "男", 95)); //map集合长度 System.out.println("map长度= "+map.size()); //遍历map集合的key for (Entry<String, Student> en : map.entrySet()) { System.out.println(en.getKey()); } System.out.println("---------------"); //遍历map集合的value for (Entry<String, Student> en : map.entrySet()) { System.out.println(en.getValue()); } map.clear(); System.out.println("map长度= "+map.size()); } } class Student{ public String no; public String name; public String sex; public int score; public Student() {} public Student(String no, String name, String sex, int score) { super(); this.no = no; this.name = name; this.sex = sex; this.score = score; } @Override public String toString() { return "Student [no=" + no + ", name=" + name + ", sex=" + sex + ", score=" + score + "]"; } }
打印输出:
map长度= 5
201705
201704
201701
201703
201702
Student [no=201705, name=川普, sex=男, score=95]
Student [no=201704, name=黄家驹, sex=男, score=96]
Student [no=201701, name=张学友, sex=男, score=99]
Student [no=201703, name=陈奕迅, sex=男, score=97]
Student [no=201702, name=陈慧娴, sex=女, score=98]
map长度= 0
三、HashMap与TreeMap的区别
TreeMap:键以某种排序规则排序,内部以red-black(红-黑)树数据结构实现,实现了SortedMap接口
HashMap:也是以哈希表数据结构实现的,解决冲突时采用了散列链表的形式,不过性能比TreeMap要低