您的位置:首页 > 编程语言 > Java开发

JAVA集合------Map (HashMap实现)

2016-09-02 20:34 453 查看
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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hash Map