Map
特点:
存储键值对(Key-Value),无序、无下标,键不可重复,值可重复
基本方法
- int size();
获取容器长度
- boolean isEmpty();
判空
- boolean containsKey(Object key);
判断是否有参数键
- boolean containsValue(Object value);
判断是否有参数值
- V get(Object key);
获取参数键的值
- V put(K key, V value);
添加键值对
- V remove(Object key);
移除键值对
- Set<Map.Entity<K,V>>
键值匹配set集合
- Collection< V > values();
返回包含所有值的Collection集合
简单实例
使用HashMap实现
package map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo1 {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("ele1", "1");
map.put("ele2", "2");
map.put("ele3", "3");
map.put("ele4", "4");
map.forEach((x, y) -> {
System.out.println(x + "|" + y);
});
System.out.println("----------------------");
map.remove("ele3");
Collection<String> values = map.values();
System.out.println(values);
System.out.println("---------------------");
Set<String> set = map.keySet();
System.out.println(set);
System.out.println("---------------------");
Set<Map.Entry<String, String>> entries = map.entrySet();
System.out.println(entries);
System.out.println("--------------------");
map.clear();
System.out.println("---------------------");
map.forEach((x, y) -> {
System.out.println(x + "|" + y);
});
}
}