一、Map接口概述
1、將鍵映射到值的對象
2、一個映射不能包含重復的鍵
3、每個鍵最多只能映射到一個值
二、Map接口和Collection接口的不同
1、Map是雙列的,Collection是單列的
2、Map的鍵唯一,Collection的子體系Set是唯一的
3、Map集合的數據結構值針對鍵有效,跟值無關;Collection集合的數據結構是針對元素有效
三、Map集合的功能
1、添加功能
V put(K key,V value):添加元素。
如果鍵是第一次存儲,就直接存儲元素,返回null
如果鍵不是第一次存在,就用值把以前的值替換掉,返回以前的值
2、刪除功能
void clear():移除所有的鍵值對元素
V remove(Object key):根據鍵刪除鍵值對元素,并把值返回
3、判斷功能
boolean containsKey(Object key):判斷集合是否包含指定的鍵
boolean containsValue(Object value):判斷集合是否包含指定的值
boolean isEmpty():判斷集合是否為空
4、獲取功能
Set<Map.Entry<K,V>> entrySet():
V get(Object key):根據鍵獲取值
Set<K> keySet():獲取集合中所有鍵的集合
Collection<V> values():獲取集合中所有值的集合
5、長度功能
int size():返回集合中的鍵值對的個數
四、Collections成員方法
1、public static <T> void sort(List<T> list)
2、public static <T>int binarySearch(List<?> list,T key)
3、public static <T> T max(Collection<?>coll)
4、public static void reverse(List<?> list) 反轉
5、public static void shuffle(List<?> list) 隨機置換,用來洗牌
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import java.util.TreeMap;import java.util.TreeSet;import com.cat.collection.Student;public class MapSet { public static void main(String[] args) { /* * 一、Map接口概述 1、將鍵映射到值的對象 2、一個映射不能包含重復的鍵 3、每個鍵最多只能映射到一個值 二、Map接口和Collection接口的不同 1、Map是雙列的,Collection是單列的 2、Map的鍵唯一,Collection的子體系Set是唯一的 3、Map集合的數據結構值針對鍵有效,跟值無關;Collection集合的數據結構是針對元素有效 三、Map集合的功能 1、添加功能 V put(K key,V value):添加元素。 如果鍵是第一次存儲,就直接存儲元素,返回null 如果鍵不是第一次存在,就用值把以前的值替換掉,返回以前的值 2、刪除功能 void clear():移除所有的鍵值對元素 V remove(Object key):根據鍵刪除鍵值對元素,并把值返回 3、判斷功能 boolean containsKey(Object key):判斷集合是否包含指定的鍵 boolean containsValue(Object value):判斷集合是否包含指定的值 boolean isEmpty():判斷集合是否為空 4、獲取功能 Set<Map.Entry<K,V>> entrySet(): V get(Object key):根據鍵獲取值 Set<K> keySet():獲取集合中所有鍵的集合 Collection<V> values():獲取集合中所有值的集合 5、長度功能 int size():返回集合中的鍵值對的個數 Collections類概述 四、Collections成員方法 1、public static <T> void sort(List<T> list) 2、public static <T> int binarySearch(List<?> list,T key) 3、public static <T> T max(Collection<?> coll) 4、public static void reverse(List<?> list) 反轉 5、public static void shuffle(List<?> list) 隨機置換,用來洗牌 * */ putMethod(); removeMethod(); forMap(); mapEntry(); hashMap();//HashMap linkedMap();//LinkedHashMap treeMap1();//TreeMap treeMap2();//TreeMap,直接傳比較器 hashTable(); card();//洗牌斗地主無序的 card1();//洗牌斗地主有序的 } //洗牌斗地主有序的 PRivate static void card1() { System.out.println("=============洗牌斗地主有序的============"); String[] numA = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] colorA = {"紅桃","黑桃","方片","梅花"}; HashMap<Integer, String> hm = new HashMap<>(); ArrayList<Integer> list = new ArrayList<>(); int index = 0; //獲得一副牌 for(String num:numA){ for(String color:colorA){ hm.put(index,color.concat(num)); //concat拼接字符串 list.add(index); index++; } } hm.put(index,"小王"); list.add(index); index++; hm.put(index,"大王"); list.add(index); //洗牌 Collections.shuffle(list);//隨機置換,用來洗牌// System.out.println(list); //三人組成斗地主,還有底牌 TreeSet<Integer> gaojin = new TreeSet<>(); TreeSet<Integer> wuge = new TreeSet<>(); TreeSet<Integer> me = new TreeSet<>(); TreeSet<Integer> dipai = new TreeSet<>(); //發牌 for(int i = 0;i < list.size();i++){ if(i >= list.size() - 3){ dipai.add(list.get(i)); }else if(i % 3 == 0){ gaojin.add(list.get(i)); }else if(i % 3 == 1){ wuge.add(list.get(i)); }else{ me.add(list.get(i)); } } //看牌 lookPai(gaojin,hm,"高進"); lookPai(wuge,hm,"五哥"); lookPai(me,hm,"我"); lookPai(dipai,hm,"底牌"); } //看牌 public static void lookPai(TreeSet<Integer> treeSet,HashMap<Integer, String> hash,String name){ System.out.println(name + "的牌是:"); for(Integer i : treeSet){ System.out.print(hash.get(i) + ","); } System.out.println(); } //洗牌斗地主無序的 private static void card() { System.out.println("=============洗牌斗地主無序的============"); String[] numA = {"A","2","3","4","5","6","7","8","9","10","J","Q","K",}; String[] colorA = {"黑桃","紅桃","方片","梅花"}; ArrayList<String> poker = new ArrayList<>(); //獲得一副牌 for(String color:colorA){ for(String num:numA){ poker.add(color.concat(num)); //concat拼接字符串 } } poker.add("大王"); poker.add("小王"); //三人組成斗地主,還有底牌 ArrayList<String> gaojin = new ArrayList<>(); ArrayList<String> wuge = new ArrayList<>(); ArrayList<String> me = new ArrayList<>(); ArrayList<String> dipai = new ArrayList<>(); //洗牌 Collections.shuffle(poker);//隨機置換,用來洗牌 //發牌 for(int i = 0;i < poker.size();i++){ if(i >= poker.size() - 3){ dipai.add(poker.get(i)); }else if(i % 3 == 0){ gaojin.add(poker.get(i)); }else if(i % 3 == 1){ wuge.add(poker.get(i)); }else{ me.add(poker.get(i)); } } System.out.println(gaojin); System.out.println(wuge); System.out.println(me); System.out.println(dipai); } private static void hashTable() { /* * HashMap和Hashtable的區別 1、Hashtable是JDK1.0版本出現的,是線程安全的,效率低 HashMap是JDK1.2版本出現的,是線程不安全的,效率高 2、Hashtable不可以存儲null鍵和null值 HashMap可以存儲null鍵和null值 * */ System.out.println("=============HashMap和Hashtable============"); HashMap<String, Integer> map = new HashMap<>(); map.put(null, 10); map.put("s", null); System.out.println(map); // //java.lang.NullPointerException// Hashtable<String, Integer> table = new Hashtable<>();// table.put(null, 10);// table.put("s", null);// System.out.println(table); } private static void treeMap2() { System.out.println("=============treeMap2============"); //直接使用匿名比較器 TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num = s1.getName().compareTo(s2.getName()); return num == 0 ? s1.getAge() - s2.getAge() : num; } }); //實現Comparable接口,重寫compareTo方法 map.put(new Student("張三", 12),"北京"); map.put(new Student("李四", 13),"深圳"); map.put(new Student("張三", 21),"上海"); map.put(new Student("王五", 16),"上海"); map.put(new Student("趙六", 15),"廣州"); System.out.print('張' + '0' + " "); System.out.print('李' + '0' + " "); System.out.print('王' + '0' + " "); System.out.print('趙' + '0' + " "); System.out.println(); System.out.println(map); } private static void treeMap1() { System.out.println("=============treeMap1============"); TreeMap<Student, String> map = new TreeMap<>(); //實現Comparable接口,重寫compareTo方法 map.put(new Student("張三", 12),"北京"); map.put(new Student("李四", 13),"深圳"); map.put(new Student("張三", 12),"上海"); map.put(new Student("趙六", 15),"廣州"); System.out.println(map); } private static void linkedMap() { /* * LinkedHashMap的特點 底層是鏈表實現的可以保證怎么存就怎么取 * */ System.out.println("=============linkedMap============"); LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); map.put("張三", 12); map.put("李四", 13); map.put("王五", 14); map.put("趙六", 15); System.out.println(map); } private static void hashMap() { System.out.println("=============hashMap============"); HashMap<Student, String> map = new HashMap<>(); map.put(new Student("張三", 12),"北京"); map.put(new Student("李四", 13),"深圳"); map.put(new Student("張三", 12),"上海"); map.put(new Student("趙六", 15),"廣州"); System.out.println(map); } private static void mapEntry() { System.out.println("=============mapEntry============"); Map<String, Integer> map = new HashMap<>(); map.put("張三", 12); map.put("李四", 13); map.put("王五", 14); map.put("趙六", 15); //Map.Entry說明Entry是Map的內部接口,將鍵和值封裝成了Entry對象,并存儲在Set集合中 Set<Map.Entry<String, Integer>> entrySet = map.entrySet();// Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();// while(it.hasNext()){// Entry<String, Integer> entry = it.next();// String key = entry.getKey();// Integer value = entry.getValue();// System.out.println(key + "--" + value);// } //在此處使用Entry也是可以的,因為Entry implements Map.Entry<K,V> for (Entry<String, Integer> entry : entrySet){ System.out.println(entry.getKey() + "--" + entry.getValue()); } } private static void forMap() { System.out.println("=============遍歷============"); Map<String, Integer> map = new HashMap<>(); map.put("張三", 12); map.put("李四", 13); map.put("王五", 14); map.put("趙六", 15); //map中沒有Iterator迭代器// Set<String> keySet = map.keySet(); //keySet()獲取所有鍵的集合// Iterator<String> it = keySet.iterator();//獲取迭代器// while(it.hasNext()){// String key = it.next();// Integer value = map.get(key);// System.out.println(key + "--" + value);// } Set<String> keySet = map.keySet(); for(String key:keySet){ Integer value = map.get(key); System.out.println(key + "--" + value); } } private static void removeMethod() { System.out.println("=============刪除============"); Map<String, Integer> map = new HashMap<>(); map.put("張三", 12); map.put("李四", 13); map.put("王五", 14); map.put("趙六", 15); System.out.println(map.remove("李四")); System.out.println(map); //根據鍵刪除值// map.clear(); //刪除所有元素值// System.out.println(map); System.out.println(map.containsKey("王五")); System.out.println(map.containsValue(20)); System.out.println(map.isEmpty()); System.out.println(map.values()); System.out.println(map.size()); } public static void putMethod() { Map<String, Integer> map = new HashMap<>(); Integer i1 = map.put("張三", 12); Integer i2 = map.put("李四", 13); Integer i3 = map.put("王五", 14); Integer i4 = map.put("趙六", 15); Integer i5 = map.put("張三", 20);//相同的鍵不存儲值,將值給覆蓋掉并返回 System.out.println(map);//底層hash算法,不能保證怎么存怎么取 System.out.println(i1 + " " + i2 + " " + i3 + " " + i4 + " " + i5 ); }}
新聞熱點
疑難解答