java-map接口学习
Java Map接口
-
Map接口是基于键(key)和值(value)对的集合。每个键值对被称为一个条目(entry)。Map中的键是唯一的。
-
如果需要根据键进行搜索、更新或删Java Map接口除元素,那么Map是很有用的。
-
在Java中,有两个接口用于实现Map:Map和SortedMap,以及三个类:HashMap、LinkedHashMap和TreeMap。Java Map的层次结构如下所示:
-
Map不允许重复的键,但可以有重复的值。HashMap和LinkedHashMap允许使用null键和null值,但TreeMap不允许使用null键或null值。
-
Map不能直接遍历,因此需要使用keySet()或entrySet()方法将其转换为Set。
-
HashMap HashMap是Map的实现,但它不维护任何顺序。
-
LinkedHashMap LinkedHashMap是Map的实现,它继承自HashMap类,并且按照插入顺序维护元素顺序。
-
TreeMap TreeMap是Map和SortedMap的实现,它按照升序维护元素顺序。
HashMap
import org.junit.Test;
import java.util.*;
public class TestMap {
@Test
public void test_hashmap() {
Map map1 = new HashMap();// 非泛型
Map<Integer, String> map = new HashMap<Integer, String>();//泛型
//向map中添加元素
map.put(1, "Amit");
map.put(5, "Rahul");
map.put(2, "Jai");
map.put(6, "Amit");
//遍历Map
Set set = map.entrySet();//转换为Set以便遍历
Iterator itr = set.iterator();
while (itr.hasNext()) {
//转换为Map.Entry以便分别获取键和值
Map.Entry entry = (Map.Entry) itr.next();
System.out.println(entry.getKey() + " " + entry.getValue());
}
//元素可以以任何顺序遍历 注意 必须是泛型 指定key value 的类型
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
@Test //比较
public void compared(){
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(100,"Amit");
map.put(101,"Vijay");
map.put(102,"Rahul");
//按照 键 比较
//返回包含此Map中的映射的Set视图
map.entrySet()
//返回以此集合为源的顺序流
.stream()
//根据提供的比较器进行排序
.sorted(Map.Entry.comparingByKey())
//对流中的每个元素执行操作
.forEach(System.out::println);
//按照 键 比较 逆序
map.entrySet()
//返回以此集合为源的顺序流
.stream()
//根据提供的比较器进行排序
.sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
//对流中的每个元素执行操作
.forEach(System.out::println);
//按照 值 比较
//返回包含此Map中的映射的Set视图
map.entrySet()
//返回以此集合为源的顺序流
.stream()
//根据提供的比较器进行排序
.sorted(Map.Entry.comparingByValue())
//对流中的每个元素执行操作
.forEach(System.out::println);
//按照 值 降序比较
map.entrySet()
//返回以此集合为源的顺序流
.stream()
//根据提供的比较器进行排序
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
//对流中的每个元素执行操作
.forEach(System.out::println);
}
}
LinkedHashMap
@Test //
public void LHmap(){
LinkedHashMap<Integer,String> hm=new LinkedHashMap<Integer,String>();
hm.put(1,"yawei");
hm.put(2,"honglou");
hm.put(3,"amei");
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
//获取键 列表
System.out.println(hm.keySet());
//获取值 列表
System.out.println(hm.values());
//获取键值对
System.out.println(hm.entrySet());
//remove函数
hm.remove(1);
System.out.println(hm);
}
TreeMap
- TreeMap类是基于红黑树实现的。它提供了一种在排序顺序中高效存储键值对的方式。
@Test
public void Treemap(){
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(100, "Amneda");
map.put(102, "yankou");
map.put(101, "shifu");
map.put(103, "hhh");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
//remove 函数
map.remove(103);
System.out.println(map);
//降序
System.out.println(map.descendingMap());
// 返回键小于或等于指定键的键值对
System.out.println(map.headMap(102));
// 返回键大于或等于指定键的键值对
System.out.println("tailMap: " + map.tailMap(102, true));
// 返回存在于指定键之间的键值对
System.out.println("subMap: " + map.subMap(100, false, 102, true));
}
例子
@Test
public void ex_map(){
HashMap<Integer, Integer> map = new HashMap<>();
map.put(1,1);
map.put(2,2);
map.compute(1,(key,value)->value+1);
System.out.println(map);
}