Map接口概述:
1、Map集合基於鍵(key)/值(value)映射。每個鍵最多隻能映射一個值。鍵可以是任何引用數據類型的值,不可重複;值可以是任何引用數據類型的值,可以重複;鍵值對存放無序。
Map常用實現類:
1、HashMap:允許使用 null值和 null鍵;此類不保證映射的順序;在多線程操作下不安全。
2、LinkedHashMap
3、Properties
Map接口常用方法:
1、put(K key, V value) 將鍵(key)/值(value)映射存放到Map集合中
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();//向HashMap中添加元素 key-value
map.put("Tom", 100);//向HashMap中添加元素key——value
}
}
泛型不能使用基本數據類型,對應的包裝類(byte-Byte short-Short int-Integer float-Float double-Double boolean-Boolean char-Charactor)
2、get(Object key) 返回指定鍵所映射的值,沒有該key對應的值則返回 null
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int scores=map.get("Tom");
System.out.println(scores);
}
}
3、size() 返回Map集合中數據數量
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("Tom", 100);//向HashMap中添加元素key——value
int scores=map.get("Tom");
System.out.println(map.size());//獲取HashMap集合容器中有多少對key——value
}
}
4、clear() 清空Map集合
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
map.clear();//成對清空
}
}
5、isEmpty () 判斷Map集合中是否有數據,如果沒有則返回true,否則返回false
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
map.clear();
System.out.println(map.isEmpty());//HashMap爲空則爲true
}
}
6、remove(Object key) 刪除Map集合中鍵爲key的數據並返回其所對應value值
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int score1 =map.remove("Tom");//刪除HashMap元素,返回value
System.out.println(score1);
}
}
7、values() 返回Map集合中所有value組成的以Collection數據類型格式數據。
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int score1 =map.remove("Tom");//刪除HashMap元素,返回value
System.out.println(score1);
map.put("Lucy", 90);
map.put("Jim", 91);
Collection<Integer>con =map.values();//(瞭解)有HashMap value組成的集合
for (int scores : con) {
System.out.println(scores);
}
}
}
8、containsKey(Object key) 判斷集合中是否包含指定鍵,包含返回 true,否則返回false
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int score1 =map.remove("Tom");
System.out.println(score1);
set.containsKey("Tom");
}
}
}
9、containsValue(Object value) 判斷集合中是否包含指定值,包含返回 true,否則返回false
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int score1 =map.remove("Tom");
System.out.println(score1);
set.containsValue("100");
}
}
10、keySet() 返回Map集合中所有key組成的Set集合
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
Set<String> set=map.keySet();
for (String key : set) {
System.out.println(key+map.get(key));
}
Iterator<String>iterator =set.iterator();
while(iterator.hasNext()) {
String key = iterator.next();
System.out.println(key+map.get(key));
}
}
11、entrySet() 將Map集合每個key-value轉換爲一個Entry對象並返回由所有的Entry對象組成的Set集合
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
Set<Entry<String, Integer>>set1=map.entrySet();//entry類每一對key-value變成一個entry對象,返回由entry對象組成的Set集合
for (Entry<String, Integer> entry : set1) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
Iterator<Entry<String, Integer>> iterator=set1.iterator();
while(iterator.hasNext()) {
Entry<String, Integer>entry=iterator.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}