HashMap(常用)方法個人理解


 
Hashmap的存值:
1  public static void main(String[] args) {
2          ///*Integer*/map.put("1", 1);//向map中添加值(返回這個key以前的值,如果沒有返回null)
3          HashMap<String, Integer> map=new HashMap<>();
4          System.out.println(map.put("1", 1));//null
5          System.out.println(map.put("1", 2));//1
6      }
Hashmap的取值:
1 public static void main(String[] args) {
2         HashMap<String, Integer> map=new HashMap<>();
3         map.put("DEMO", 1);
4         /*Value的類型*///得到map中key相對應的value的值
5         System.out.println(map.get("1"));//null
6         System.out.println(map.get("DEMO"));//1
7     }

Hashmap的判斷為空:

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

Hashmap判斷是否含有key:

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

Hashmap判斷是否含有value:

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

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顯示所有的value值:

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

Hashmap的元素個數:

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

 

Hashmap刪除這個key值下的value:

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

 

Hashmap顯示所有的key和value:

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

 

Hashmap添加另一個同一類型的map下的所有制:

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

 

Hashmap刪除這個key和value:

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

 

Hashmap替換這個key的value:(java8)

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

清空這個hashmap:

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

Hashmap的克隆:

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

 如果當前 Map 不存在鍵 key 或者該 key 關聯的值為 null,那么就執行 put(key, value);否則,便不執行 put 操作:(java8新增方法)

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}

}

 如果當前 Map 的value為xx時則值為xx否則為xx:(java8新增方法)compute 方法更適用於更新 key 關聯的 value 時,新值依賴於舊值的情況 

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

如果當前 Map 的value為xx時則值為xx否則為xx:(java8新增方法)

 

如果當前 Map 的value為xx時則值為xx否則為xx:(java8新增方法)

 

如果當前 Map 的value為xx時則值為xx否則為xx:(java8新增方法)

 

如果當前 Map 的value為xx時則值為xx否則為xx:(java8新增方法)

 

如果當前 Map 的value為xx時則值為xx否則為xx:(java8新增方法)

 

 

java8新增方法

方法詳解


/**/map.computeIfAbsent(key, mappingFunction);
/**/map.computeIfPresent(key, remappingFunction);
/**/map.forEach());
/**/map.merge(key, value, remappingFunction);
/**/map.getOrDefault(key, defaultValue);

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}  此處應該是 {DEMO1=1, DEMO2=2, DEMO3=12222}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM