Java進階總結——Map和Collections
1.Map
1.1 HashMap
package day21.map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
//1.初始化一個HashMap對象
HashMap<Integer, Integer>map=new HashMap<Integer, Integer>();
//2.添加資料
map.put(0, 151);
map.put(1, 152);
map.put(3, 153);
map.put(2, 154);
map.put(null, 111);
map.put(4, 0);
System.out.println(map.toString());
//3.修改
map.put(null, 154);
System.out.println(map.toString());
//4.删除元素,通過key删除value
map.remove(null);
System.out.println(map.toString());
//5.檢視成員
//通過key擷取value
System.out.println(map.get(3));
//判斷是否包含指定的key值
System.out.println(map.containsKey(null));
//判斷是否包含value值
System.out.println(map.containsValue(152));
//檢視鍵值對的個數
System.out.println(map.size());
//擷取所有的key值
System.out.println("key值");
Set<Integer>keys=map.keySet();
for (Integer integer : keys) {
System.out.print(integer+"\t");
}
System.out.println();
System.out.println("value值");
//擷取到所有的value值
Collection<Integer>values=map.values();
for (Integer integer : values) {
System.out.print(integer+"\t");
}
System.out.println();
//擷取所有的映射關系,進行周遊map
Set<Entry<Integer, Integer>>en=map.entrySet();
for (Entry<Integer, Integer> entry : en) {
System.out.print(entry.getKey()+"--"+entry.getValue());
System.out.println();
}
}
}
輸出結果:

1.2TreeMap
package day21.map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
//初始化一個TreeMap對象
TreeMap<Integer, String>map=new TreeMap<Integer, String>();
//添加
map.put(6, "浪子一秋");
map.put(2, "一葉知秋");
map.put(1, "葬劍山莊");
map.put(9, "武林至尊");
map.put(4, "葬劍灬尊");
System.out.println(map.toString());
//取值
System.out.println(map.firstEntry());
//擷取所有元素
Set<Entry<Integer, String>>en=map.entrySet();
for (Entry<Integer, String> entry : en) {
System.out.print(entry.getKey()+"\t"+entry.getValue());
System.out.println();
}
System.out.println(map.floorEntry(3));
}
}
輸出結果:
2.Collections
package day21.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CollectionsDemo {
public static void main(String[] args) {
//向指定的清單中添加元素
ArrayList<String>list=new ArrayList<String>();
Collections.addAll(list,"id ","name","age","sex");
System.out.println(list.toString());
//使用二分查找法查找指定元素
System.out.println(Collections.binarySearch(list, "sex"));
//将清單中的元素複制到另一個清單中
ArrayList<String>dest=new ArrayList<String>();
Collections.addAll(dest,"id1 ","name","age","sex","nn");
Collections.copy(dest, list);
System.out.println(dest.toString());
//反轉順序
Collections.reverse(dest);
System.out.println(dest.toString());
//擷取最值,assic碼值
System.out.println(Collections.max(dest));
System.out.println(Collections.min(dest));
//比較器
Collections.reverseOrder();
System.out.println(dest.toString());
//Collections.reverseOrder(Comparator<String>cmp);
//填充元素
Collections.fill(dest,"score");
System.out.println(dest.toString());
}
}
輸出結果:
3.知識架構