hashMap常用方法

hashMap放入值

public static void main(String[] args) {
         ///*Integer*/map.put("1", 1);//向map中添加值(返回這個key以前的值,如果沒有返回null)
         HashMap<String, Integer> map=new HashMap<>();
         System.out.println(map.put("1", 1));//null
         System.out.println(map.put("1", 2));//1
     }

hashMap取值

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        map.put("DEMO", 1);
        /*Value的類型*///得到map中key相對應的value的值
        System.out.println(map.get("1"));//null
        System.out.println(map.get("DEMO"));//1
    }

hashMap判斷是否爲空

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判斷map是否爲空
        System.out.println(map.isEmpty());//true
        map.put("DEMO", 1);
        System.out.println(map.isEmpty());//false
    }

hashMap判斷是否有key

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判斷map中是否存在這個key
        System.out.println(map.containsKey("DEMO"));//false
        map.put("DEMO", 1);
        System.out.println(map.containsKey("DEMO"));//true
    }

hashMap判斷是否有value

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///判斷map中是否存在這個value
        System.out.println(map.containsValue(1));//false
        map.put("DEMO", 1);
        System.out.println(map.containsValue(1));//true
    }

hashMap刪除指定key下的value

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*Integer*///刪除key值下的value
        System.out.println(map.remove("1"));//null
        map.put("DEMO", 2);
        System.out.println(map.remove("DEMO"));//2(刪除的值)
    }

hashMap顯示所有的key

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*SET<String>*///顯示map所有的key
        System.out.println(map.keySet());//[]
        map.put("DEMO1", 1);
        System.out.println(map.keySet());//[DEMO1]
        map.put("DEMO2", 2);
        System.out.println(map.keySet());//[DEMO1, DEMO2]
    }

hashMap顯示所有的value

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*Collection<Integer>*///顯示所有的value值
        System.out.println(map.values());//[]
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map.values());//[1, 2]
    }

hashMap的元素個數

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*int*///顯示map裏的值得數量
        System.out.println(map.size());//0
        map.put("DEMO1", 1);
        System.out.println(map.size());//1
        map.put("DEMO2", 2);
        System.out.println(map.size());//2
    }

hashMap顯示所有的key和value

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*SET<map<String,Integer>>*///顯示所有的key和value
        System.out.println(map.entrySet());//[]
        map.put("DEMO1", 1);
        System.out.println(map.entrySet());//[DEMO1=1]
        map.put("DEMO2", 2);
        System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]
    }

hashMap添加一個map到另一個同類型的map下

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        HashMap<String, Integer> map1=new HashMap<>();
        /*void*///將同一類型的map添加到另一個map中
        map1.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO2=2}
        map.putAll(map1);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
    }

hashMap刪除指定的key和value(key和value都作爲刪除條件,兩者需要同時成立)

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///刪除這個鍵值對
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        System.out.println(map.remove("DEMO2", 1));//false
        System.out.println(map.remove("DEMO2", 2));//true
        System.out.println(map);//{DEMO1=1}
    }

hashMap替換某個key的value

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*value*///判斷map中是否存在這個key
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        System.out.println(map.replace("DEMO2", 1));//2
        System.out.println(map);//{DEMO1=1, DEMO2=1}
    }

清空hashMap

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*void*///清空map
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        map.clear();//2
        System.out.println(map);//{}
    }

克隆hashMap

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*object*///克隆這個map
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map.clone());//{DEMO1=1, DEMO2=2}
        Object clone = map.clone();
        System.out.println(clone);//{DEMO1=1, DEMO2=2}
    }

hashMap中如果鍵key不存在或者關聯的值爲null,那麼就執行put(key,value),否則不執行!

public static void main(String[] args) {
		HashMap<String, Integer> map=new HashMap<>();
		/*boolean*///判斷map中是否存在這個key
		map.put("DEMO1", 1);
		map.put("DEMO2", 2);
		System.out.println(map);//{DEMO1=1, DEMO2=2}
		System.out.println(map.putIfAbsent("DEMO1", 12222));//1
		System.out.println(map.putIfAbsent("DEMO3", 12222));//null
		System.out.println(map);//{DEMO1=1, DEMO2=2,DEMO3=12222}
	}

hahsMap的compute方法適用於更新key關聯的value時,新值依賴於舊值的情況

public static void main(String[] args) {
        HashMap<String, Integer> map=new HashMap<>();
        /*boolean*///當這個value爲null時爲1,否則爲3
        map.put("DEMO1", 1);
        map.put("DEMO2", 2);
        System.out.println(map);//{DEMO1=1, DEMO2=2}
        map.compute("DEMO2", (k,v)->v==null?1:3);
        System.out.println(map);//{DEMO1=1, DEMO2=3}
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章