映射表Map是用来存放键/值对的一种数据结构,只要知道键,就可以查到对应的值。
Map有两个通用的实现:HashMap和TreeMap。
其中散列映射表对键进行散列,树映射表用键的整体顺序对元素进行排序,并将其组织成搜索树。散列或比较函数只能作用于键。
键必须是唯一的。如果对用一个键两次调用put方法,第二个值就会取代第一个值。
Map有三个视图:键集[Set< K > keySet()]、值集合[Collection< K > values()]和键/值对集[Set< Map.Entry< K , V > > entrySet()]
eg1:
private static void initHashMap() {
// TODO Auto-generated method stub
Map<Integer, String> map=new HashMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");
map.put(4, "D");
map.put(5, "E");
map.put(6, "F");
System.out.println(map);
System.out.println(map.keySet());//键集
System.out.println(map.values());//值集合
System.out.println(map.entrySet());//键/值对集
for(Map.Entry<Integer, String> entry:map.entrySet()){
System.out.println(entry.getKey()+" - - "+entry.getValue());
if(entry.getKey()==6){
map.entrySet().remove(entry);//可删除元素
}
}
System.out.println(map);
map.put(1, "a");//对同一个键重复添加,会覆盖掉原有的值
System.out.println(map);
map.remove(2, "B");
System.out.println(map);
map.replace(3, "D");
System.out.println(map);
}
结果:
{1=A, 2=B, 3=C, 4=D, 5=E, 6=F}
[1, 2, 3, 4, 5, 6]
[A, B, C, D, E, F]
[1=A, 2=B, 3=C, 4=D, 5=E, 6=F]
1 - - A
2 - - B
3 - - C
4 - - D
5 - - E
6 - - F
{1=A, 2=B, 3=C, 4=D, 5=E}
{1=a, 2=B, 3=C, 4=D, 5=E}
{1=a, 3=C, 4=D, 5=E}
{1=a, 3=D, 4=D, 5=E}
TreeMap可以实现有顺序(默认增序)的map(SortedMap),但其比较函数只能作用于键,其中默认的比较函数是从小到大增序排列。
eg2:
private static void initTreeMap() {
// TODO Auto-generated method stub
SortedMap<Integer, Person> map=new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
//降序排列
return o2-o1;
}
});
map.put(1, new Person(1, "A", 15));
map.put(3, new Person(3, "B", 11));
map.put(5, new Person(5, "C", 13));
map.put(2, new Person(2, "D", 19));
map.put(4, new Person(4, "E", 10));
for(Integer key:map.keySet()){
System.out.println(key+" - - "+map.get(key));
}
for(Person p:map.values()){
System.out.println(p);
}
}
结果:map中的数据均按照键值从大到小排序
5 - - Person [id=5, name=C, age=13]
4 - - Person [id=4, name=E, age=10]
3 - - Person [id=3, name=B, age=11]
2 - - Person [id=2, name=D, age=19]
1 - - Person [id=1, name=A, age=15]
Person [id=5, name=C, age=13]
Person [id=4, name=E, age=10]
Person [id=3, name=B, age=11]
Person [id=2, name=D, age=19]
Person [id=1, name=A, age=15]
除了使用Map中比较函数来进行排序,我们可以使用键/值对集(Map.Entry)来进行排序。就是将Map.Entry转换为List,从而对List进行排序,这种方法不仅可以对键进行排序,也可以对值进行排序,但是它只是对Map.Entry转换出的List进行排序,而原有的Map顺序没变
eg3:
private static void initTreeMap() {
// TODO Auto-generated method stub
SortedMap<Integer, Person> map=new TreeMap<>();
map.put(1, new Person(1, "A", 15));
map.put(3, new Person(3, "B", 11));
map.put(5, new Person(5, "C", 13));
map.put(2, new Person(2, "D", 19));
map.put(4, new Person(4, "E", 10));
// 这里将map.entrySet()转换成list
List<Map.Entry<Integer, Person>> list = new ArrayList<Map.Entry<Integer, Person>>(map.entrySet());
// 然后通过比较器来实现排序
Collections.sort(list, new Comparator<Map.Entry<Integer, Person>>() {
// 升序排序
public int compare(Entry<Integer, Person> o1, Entry<Integer, Person> o2) {
return o1.getValue().getAge()-o2.getValue().getAge();
}
});
for(Map.Entry<Integer, Person> m:list){
System.out.println(m.getKey()+" - - "+m.getValue());
}
System.out.println("----------------------");
for(Integer key:map.keySet()){
System.out.println(key+" - - "+map.get(key));
}
for(Person p:map.values()){
System.out.println(p);
}
}
结果:我们会发现Map.Entry转换出的List实现了对Person进行增序排列,而原有的Map则实现了默认排序(按照键进行增序排列)
4 - - Person [id=4, name=E, age=10]
3 - - Person [id=3, name=B, age=11]
5 - - Person [id=5, name=C, age=13]
1 - - Person [id=1, name=A, age=15]
2 - - Person [id=2, name=D, age=19]
----------------------
1 - - Person [id=1, name=A, age=15]
2 - - Person [id=2, name=D, age=19]
3 - - Person [id=3, name=B, age=11]
4 - - Person [id=4, name=E, age=10]
5 - - Person [id=5, name=C, age=13]
Person [id=1, name=A, age=15]
Person [id=2, name=D, age=19]
Person [id=3, name=B, age=11]
Person [id=4, name=E, age=10]
Person [id=5, name=C, age=13]