map初始容量是16
package com.tedu.study01;
import java.util.HashMap;
import java.util.Map;
public class study_Map {
public static void main(String[] args) {
// 创建一个map集合
Map<String,String > map = new HashMap<>();
// 在集合中存储元素
map.put("name","胡国");
map.put("age","18");
map.put("addr","北京");
map.put("name2","姜育恒");
System.out.println(map);
System.out.println(map.get("addr"));
System.out.println(map.remove("addr"));
System.out.println(map);
System.out.println(map.keySet());
}
}
综合map实例
package com.tedu.study01;
import java.util.*;
public class study_Map {
public static void main(String[] args) {
// 创建一个map集合
Map<String,String > map = new HashMap<>();
// 在集合中存储元素
map.put("name","胡国");
map.put("age","18");
map.put("addr","北京");
map.put("name2","姜育恒");
System.out.println(map);
System.out.println(map.get("addr"));
System.out.println(map.remove("addr"));
System.out.println(map);
System.out.println(map.keySet());
System.out.println(map.values());
// 将map集合转换成set,通过keyset方法
Set<String> keySet = map.keySet();
for (String key :
keySet) {
System.out.println(key+":"+map.get(key));
}
System.out.println("===========================");
//方式二 将map集合转换成set集合,通过entrySet()将map转换成set
Set<Map.Entry<String,String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry :
entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("====================================");
// 将map转换成collection集合,通过values方法获取value
Collection<String> values = map.values();
for (String a :
values) {
// 可以 通过key获取value,不能通过value获取key
System.out.println(a);
}
// 方式四
System.out.println("=======++++++++++++++++++++++++++++++++++++++++++++++++");
Set<Map.Entry<String,String>> entrySet2 = map.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet2.iterator();
while (iterator.hasNext()){
Map.Entry<String,String> entry = iterator.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
标签:map,java,Map,System,put,println,out
From: https://www.cnblogs.com/ch2020/p/16867539.html