双列集合
双列集合的特点
双列集合
Map的常见API
Map是双列集合的顶层接口,它的功能是全部双列集合都可以继承使用的
V put(K key,V value) 添加元素
再添加元素的时候,如果键不存在,那么直接把键值对象添加到map中,方法返回null
在添加数据的时候,如果键是存在的,那么会把原有的键值对象覆盖,会把覆盖的值进行返回
import java.util.HashMap;
import java.util.Map;
public class Map_Test {
public static void main(String[] args) {
Map<String,String> m = new HashMap<String, String>();
System.out.println(m.put("111", "222"));
m.put("333","444");
System.out.println(m);
m.remove("111");
System.out.println(m);
System.out.println(m.containsKey("111"));
System.out.println(m.containsValue("444"));
System.out.println(m.size());
}
}
Map集合的遍历方式
- 键找值
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Map_Test {
public static void main(String[] args) {
Map<String,String> m = new HashMap<String, String>();
System.out.println(m.put("111", "222"));
m.put("333","444");
//获取键值的单列集合
Set<String> keys = m.keySet();
//lamb表达式
keys.forEach(o -> System.out.println(m.get(o)));
for(String s : keys){
System.out.println(m.get(s));
}
}
}
- 键值对
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Map_Test {
public static void main(String[] args) {
Map<String,String> m = new HashMap<String, String>();
System.out.println(m.put("111", "222"));
m.put("333","444");
//键值对集合
Set<Map.Entry<String, String>> entries = m.entrySet();
for(Map.Entry<String,String>entry : entries){
System.out.println(entry.getKey() +" = " + entry.getValue()) ;
}
//迭代器遍历
Iterator<Map.Entry<String, String>> it = entries.iterator();
while(it.hasNext()){
Map.Entry<String,String> t = it.next();
System.out.println(t.getKey() + " = " + t.getValue());
}
}
}
3.lambda表达式遍历
public class Map_Test {
public static void main(String[] args) {
Map<String,String> m = new HashMap<String, String>();
System.out.println(m.put("111", "222"));
m.put("333","444");
//lambda表达式遍历
m.forEach((key,value) -> System.out.println(key + " = " + value));
}
}
标签:学习,Map,util,18,System,2024,println,import,out
From: https://www.cnblogs.com/XYu1230/p/18421239