HashMap
Map集合基于 鍵(key)/值(value)映射,每個鍵最多只能映射一個值,鍵可以是任何參考資料型別的值,不可重復;值可以是任何參考資料型別的值,可以重復;鍵值對存放無序,
HashMap常用方法
put/get 方法
1.put(K key, V value) 將鍵(key)/值(value)映射存放到Map集合中,
2.get(Object key) 回傳指定鍵所映射的值,沒有該key對應的值則回傳 null,
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
System.out.println(map.get("zhaobin"));
System.out.println(map.get("tom"));
}
}
輸出結果
70
null
size()方法
3.size() 回傳Map集合中資料數量,
package test;
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
System.out.println(map.size());
}
}
輸出結果:2
clear()方法
4.clear() 清空Map集合
package test;
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
System.out.println(map.size());
map.clear();
System.out.println(map.size());
}
}
輸出結果:
2
0
isEmpty()方法
5.isEmpty () 判斷Map集合中是否有資料,如果沒有則回傳true,否則回傳false
package test;
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
System.out.println(map.size());
map.clear();
System.out.println(map.isEmpty());
System.out.println(map.size());
}
}
輸出結果:
2
true
0
remove() 方法
6.remove(Object key) 洗掉Map集合中鍵為key的資料并回傳其所對應value值,
package test;
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
System.out.println(map.remove("bob"));
System.out.println(map.size());
}
}
輸出結果:
60
1
values()方法
7.values() 回傳Map集合中所有value組成的以Collection資料型別格式資料,
package test;
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
map.put("bb", 6);
System.out.println(map.values());
}
}
輸出結果:
[70, 6, 60]
keySet() 方法
8.keySet() 回傳Map集合中所有key組成的Set集合
package test;
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
map.put("bb", 6);
System.out.println(map.keySet());
}
}
輸出結果:
[zhaobin, bb, bob]
containsKey(Object key) 方法
9.containsKey(Object key) 判斷集合中是否包含指定鍵,包含回傳 true,否則回傳false
package test;
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
map.put("bb", 6);
System.out.println(map.containsKey("bob"));
System.out.println(map.containsKey("b"));
}
}
輸出結果:
true
false
containsValue(Object value)方法
10.containsValue(Object value) 判斷集合中是否包含指定值,包含回傳 true,否則回傳false,
package test;
import java.util.HashMap;
public class Map {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("zhaobin",70);
map.put("bob", 60);
map.put("bb", 6);
System.out.println(map.containsValue(70));
System.out.println(map.containsValue(100));
}
}
輸出結果:
true
false
entrySet()方法
11.HashMap用entrySet() 遍歷集合,
entrySet() 將Map集合每個key-value轉換為一個Entry物件并回傳由所有的Entry物件組成的Set集合
用法如下:
package test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Map<String, String> studentMap = new HashMap<String, String>();
studentMap.put("002", "小王");
studentMap.put("001", "小李");
Set<Entry<String, String>> studentSet = studentMap.entrySet();
for (Entry<String, String> entry : studentSet) {
String key = entry.getKey();
String value = https://www.cnblogs.com/wiseleer/p/entry.getValue();
System.out.println(key+":"+value);
}
}
本文來自博客園,作者:wiselee/,轉載請注明原文鏈接:https://www.cnblogs.com/wiseleer/p/16784420.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/514053.html
標籤:Java
上一篇:第一章 Java概述與環境搭建
下一篇:匯出Excel添加水印
