package java_util_map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapTest01 {
public static void main(String[] args) {
/*
* Map是一个接口,HashMap是Map的一个实现类
*/
Map<String, String> map = new HashMap<String, String>();
/*
* put()方法向Map中添加key-value对
*/
map.put("0", "Spring");
map.put("1", "Summer");
map.put("2", "Autumn");
map.put("3", "Winter");
/*
* HashMap中的put,如果添加的key值重复,该方法返回旧的value值
*/
System.out.println(map.put("4", "Seasons"));
/*
* 直接打印map对象,可以查看HashMap的toString()方法
*/
System.out.println(map);
/*
* containsKey()返回是否包含指定key值
*/
System.out.println(map.containsKey("2"));//return true
System.out.println(map.containsKey("11"));//return false
/*
* containsValue()返回是否包含指定的value值
*/
System.out.println(map.containsValue("Autumn"));//return true
System.out.println(map.containsValue("222"));//return false
/*
* keySet()返回所有key组成的Set集合
*/
for (String key : map.keySet()) {
System.out.println(key+"-->"+map.get(key)+" ");
}
/*
* isEmpty()判断map是否为空
*/
System.out.println("该map集合是否为空? "+map.isEmpty());
/*
* getOrDefault(key,defaultValue)返回指定key的value值,若没有key值,则返回defaulValue
*/
System.out.println(map.getOrDefault("1", "2"));//return Summer
System.out.println(map.getOrDefault("11", "2"));//return 2
/*
* size():返回key-value对的数量
*/
System.out.println(map.size());//return 5
/*
* values():返回该map中value构成的Collection集合
* @return : Spring Summer Autumn Winter Seasons
*/
Collection<String> values = map.values();
for (String value : values) {
System.out.print(value + " ");
}
/*
* replace(K key, V value):将value值代替指定key的旧value
*/
System.out.println();
map.replace("2", "2");
for (String key : map.keySet()) {
System.out.println(key+" -->"+ map.get(key));
}
/*
* replace(K key, V oldValue, V newValue):将newValue值代替指定key的旧oldValue
* @return:Replaces the entry for the specified key only if currently mapped to the specified value.
*/
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
map.replace("2", "2", "Autumn");//return true
map.replace("2", "1", "Autumn");//return false,because the oldValue is not correct
for(String key : map.keySet()){
System.out.println(key+"-->"+map.get(key));
}
/*
* remove(Object key):移除指定key的key-value
*/
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
map.remove("0");
for (String key : map.keySet()) {
System.out.println(key+"-->"+map.get(key));
}
/*
* putIfAbsent(K key, V value):If the specified key is not
* already associated with a value (or is mapped to null) associates it with the given value and returns null,
* else returns the current value.
*/
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
map.put("5", null);
map.putIfAbsent("5", "Spring");
for (String key : map.keySet()) {
System.out.println(key+"-->"+map.get(key));
}
/*
* entrySet():返回map中key-value的Set集合,Set集合中存的都是无序,不可重复的key-value
* Returns a Set view of the mappings contained in this map.
*/
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^");
Set<Map.Entry<String, String>> mapSet = map.entrySet();
for (Entry<String, String> entry : mapSet) {
System.out.println(entry);
}
}
}JAVA集合------Map (HashMap实现)
原创
©著作权归作者所有:来自51CTO博客作者Code小伟的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java遍历map集合那种方式最快
Java中遍历Map集合常用的几种方法。
System 键值对 java -
集合系列 Map(十二):HashMap
HashMap 是 Map 基于哈希散
链表 红黑树 数组 -
【Java】Collection集合和Map集合(List、Set、HashMap、TreeMap)
一、Collection(存储的值) Map(一个键名,一个值) List(值不唯一,有顺序) 链...
java 集合 List Set javascript
















