zoukankan      html  css  js  c++  java
  • hashmap可以用null为键值

    import java.util.HashMap;
    import java.util.Map;
    import java.util.TreeMap;
    
    public class TestMain {
    	public static void main(String[] args) {
    
    		// HashMap可以的键值可以是null, "".
    		Map<String, String> strMap1 = new HashMap<String, String>();
    		strMap1.put(null, "1");
    		strMap1.put("", "2");
    		strMap1.put(" ", "3");
    		strMap1.put(null, "4");
    		System.out.println(strMap1.get(null));
    		for (String s : strMap1.keySet()) {
    			System.out.println(s);
    		}
    		for (String s : strMap1.values()) {
    			System.out.println(s);
    		}
    		
    		// TreeMap的键值不能是null
    		Map<String, String> strMap2 = new TreeMap<String, String>();
    		strMap2.put(null, "1");
    		//strMap2.put("", "2");
    		//strMap2.put(" ", "3");
    		//strMap2.put(null, "4");
    		//System.out.println(strMap2.get(null));
    		for (String s : strMap2.keySet()) {
    			System.out.println(s);
    		}
    		for (String s : strMap2.values()) {
    			System.out.println(s);
    		}
    	}
    }
    
  • 相关阅读:
    HUST-1350 Trie
    hihocoder-第六十一周 Combination Lock
    hihocoder-1196 : 高斯消元·二
    hihocoder-1195 : 高斯消元·一
    SPOJ
    HDU-5074
    UVALive
    POJ-2195
    UVALive
    POJ-1556
  • 原文地址:https://www.cnblogs.com/killbug/p/2321065.html
Copyright © 2011-2022 走看看