双列集合Map的实现类
生活随笔
收集整理的這篇文章主要介紹了
双列集合Map的实现类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Map接口【和Collection接口并列】
Map接口
成員方法【實現于Map接口,TreeMap也可實現,這里以HashMap為例】
//HashMap實現類 :無序[HashSet底存原理] 哈希表 public class Demo1 {public static void main(String[] args) {HashMap<String, Integer> map = new HashMap<>();//添加鍵值對[put方法]map.put("張三",20);map.put("李四",20);//如果鍵一樣,則后面的值會會覆蓋前面的值map.put("張三",40);map.put("王五",20);System.out.println(map);//remove 根據鍵刪除鍵值對元素,返回刪除的鍵對應的值Integer value1 = map.remove("張三");System.out.println(value1);//clear清空/*map.clear();System.out.println(map);*///containsKeys判斷集合中是否包含指定的鍵,并返回booleanboolean result1 = map.containsKey("張三");boolean result2 = map.containsKey("李四");System.out.println(result1);System.out.println(result2);//containsValue判斷集合中是否包含指定的值,并返回booleanboolean result3 = map.containsValue(20);boolean result4 = map.containsValue(40);System.out.println(result3);System.out.println(result4);//isEmpty判斷集合是否為空,并返回booleanboolean result5 = map.isEmpty();System.out.println(result5);//int size()集合的長度,集合中鍵值對的個數System.out.println(map.size());//keySet獲取所有的鍵Set<String> keys = map.keySet();System.out.println(keys);//get(Key)獲取鍵對應的值Integer value2 = map.get("張三");Integer value3 = map.get("李四");System.out.println(value2);System.out.println(value3);//entrySet獲取所有的鍵值對Set<Map.Entry<String, Integer>> entries = map.entrySet();System.out.println(entries);} }打印結果: ---------------------------------------------------------------- {李四=20, 張三=40, 王五=20} 40 false true true false false 2 [李四, 王五] null 20 [李四=20, 王五=20]Map集合遍歷方法
//遍歷HashMap public class Demo3 {public static void main(String[] args) {//創建集合并添加元素HashMap<String, String> map = new HashMap<>();map.put("1號丈夫", "1號妻子");map.put("2號丈夫", "2號妻子");map.put("3號丈夫", "3號妻子");map.put("1號丈夫", "1號妻子");//第一中方法:鍵找值的方式//keySet獲取所有的鍵//返回值為set集合[set為接口,集合類型為其具體的實現類]Set<String> keys = map.keySet();for (String key : keys) {String value = map.get(key);System.out.println(key + "..." + value);}System.out.println("----------------------------");//第二種方法:獲取鍵值對對象//entrySet獲取所有的鍵值對對象// //返回值為set集合[set集合存儲的是鍵值對對象,Entry存儲的是鍵和值]Set<Map.Entry<String, String>> entries = map.entrySet();for (Map.Entry<String,String> entry : entries) {System.out.println(entry.getKey() + "..." + entry.getValue());}} }打印結果: ----------------------------------------------------------------- 1號丈夫...1號妻子 2號丈夫...2號妻子 3號丈夫...3號妻子 ---------------------------- 1號丈夫...1號妻子 2號丈夫...2號妻子 3號丈夫...3號妻子實現類HashMap
1.特點:無序,雙列,鍵唯一,值可重復,不可排序
2.底層原理:哈希表【鏈表,數組,[鏈表滿8為]紅黑樹】
HashMap和HashSet一樣底層為哈希表,默認比較為地址值,API有的類已經寫好的我們就不需要重寫,但我們自定義的類一般都需要重寫HashCode方法
a.【HashMap默認比較鍵的地址值】
//HashMap存儲對象 //保證鍵不重復 public class Demo4 {public static void main(String[] args) {HashMap<Student, String> map = new HashMap<>();Student student1 = new Student("JS", 1800);Student student2 = new Student("LQ", 2000);Student student3 = new Student("小汪", 18);Student student4 = new Student("小汪", 18);map.put(student1, "獸族");map.put(student2, "不死族");map.put(student3,"獸族");map.put(student4, "牛牛牛");// public int hashCode() {// int h = 0;// for (Entry<K, V> entry : entrySet())// h += entry.hashCode();// return h;// }Set<Map.Entry<Student, String>> entries = map.entrySet();for (Map.Entry<Student, String> entry : entries) {System.out.println(entry.hashCode());}//鍵值對遍歷Set<Map.Entry<Student, String>> entries1 = map.entrySet();for (Map.Entry<Student, String> entry : entries) {System.out.println(entry.getKey() + "..." + entry.getValue());}} }打印結果: --------------------------------------------------------------- 473594216 780099464 189191576 1314561244 Student{name='小汪', age=18}...牛牛牛 Student{name='LQ', age=2000}...不死族 Student{name='JS', age=1800}...獸族 Student{name='小汪', age=18}...獸族b.【HashMap重寫鍵的equals方法和hashCode方法比較鍵的屬性值】
---------------------測試類代碼不變--------------------------- public class Student {private String name;private int age;public Student(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;if (age != student.age) return false;return name != null ? name.equals(student.name) : student.name == null;}@Overridepublic int hashCode() {int result = name != null ? name.hashCode() : 0;result = 31 * result + age;return result;} }打印結果: -------------------------------------------------------------- 14493644 20137578 746797 //鍵相同的時候,后面鍵對應的值就會覆蓋前面的值 Student{name='小汪', age=18}...牛牛牛 Student{name='LQ', age=2000}...不死族 Student{name='JS', age=1800}...獸族與HashSet的區別:重寫針對的都是HashMap的鍵,其他步驟一樣。
實現類TreeMap
1.特點:無序,雙列,鍵唯一,值可重復,有排序功能
2.底層原理:紅黑樹
3.需要指定排序規則,API中有一些類已經實現了Comparable接口],給出了默認排序規則,如:Integer:數值大小[升序] String:字典順序等
與TreeSet的區別:重寫針對的都是TreeMap的鍵,其他步驟一樣。
Map接口圖解
總結
以上是生活随笔為你收集整理的双列集合Map的实现类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有app源码怎么制作软件(有app源码怎
- 下一篇: 手机炉石安卓拔线插件(手机炉石安卓)