生活随笔
收集整理的這篇文章主要介紹了
JAVA 映射表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Map
映射表是一種依照鍵/值對存儲元素的容器。它提供了通過鍵快速獲取、刪除和更新鍵/值對的功能。映射表將鍵與值一起保存,鍵可以是任意類型的對象,映射表中不能有重復的鍵,如果存儲的鍵在映射表中已經存在則值會覆蓋。
Map是映射表的父接口,他的方法有
V put (K key, V value):添加元素。
V get(Object Key) 返回鍵對應的值
int size() 返回映射表中的條目數
void clear ():移除所有的鍵值對元素
V remove (Object key):根據鍵刪除鍵值對元素,并把值返回
boolean containsKey (Object key):判斷集合是否包含指定的鍵
boolean containsValue (Object value):判斷集合是否包含指定的值
boolean isEmpty ():判斷集合是否為空
Set keySet();//獲取鍵的集合
Collection values();//獲取所有值的集合
Set<Map<K,V>> entrySet()返回一個包含了該映射表中的條目的集合。
public class MapDemo {public static void main(String
[] args
) {Map
<Integer, String> map
= new HashMap<>();map
.put(1,"aaa");map
.put(2,"ccc");map
.put(3,"eee");System
.out
.println(map
.get(1));System
.out
.println(map
.size());System
.out
.println(map
);System
.out
.println(map
.remove(2));System
.out
.println(map
);map
.clear();System
.out
.println(map
);map
.put(1,"aaa");map
.put(2,"aaa");map
.put(3,"eee");System
.out
.println( map
.containsKey(1) );System
.out
.println(map
.containsValue("ccc"));System
.out
.println(map
.keySet());System
.out
.println(map
.values());}
}
遍歷Map的方法
1通過可以通過鍵找值的方式來遍歷
2通過 Map.Entry<K, V> 方法返回的Set集合遍歷
public class MapDemo {public static void main(String
[] args
) {Map
<Integer, String> map
= new HashMap<>();map
.put(1,"aaa");map
.put(2,"aaa");map
.put(3,"eee");map
.put(4,"fff");map
.put(5,"iii");Set
<Integer> k
= map
.keySet();for (int key
:k
) {String v
= map
.get(key
);System
.out
.println(key
+" "+v
);}Set
<Map
.Entry
<Integer, String>> entries
= map
.entrySet();for (Map
.Entry
<Integer, String> entry
: entries
) {System
.out
.println(entry
.getKey()+" "+entry
.getValue());}}
}
Map接口下有三個具體的類實現了Map接口,分別是HashMap、LinkedHashMap、TreeMap。
演示使用Map存儲自定義的類
public class Student {private String name
;private int age
;public Student() {}public Student(String name
, int age
) {this.name
= name
;this.age
= age
;}public String
getName() {return name
;}public void setName(String name
) {this.name
= name
;}public int getAge() {return age
;}public void setAge(int age
) {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
;return age
== student
.age
&&Objects
.equals(name
, student
.name
);}@Overridepublic int hashCode() {return Objects
.hash(name
, age
);}
}
HashMap
HashMap與HashSet相同,底層都是通過哈希表來實現的。都是通過重寫hashCode()與equals()方法來確保元素的唯一性。
而HashSet的底層其實也是使用了HashMap。
public class MapDemo {public static void main(String
[] args
) {HashMap
<String, Student> hashMap
= new HashMap<>();hashMap
.put("s001",new Student("鐘楚紅",23));hashMap
.put("s002", new Student("梅艷芳", 27));hashMap
.put("s003", new Student("李麗珍", 26));hashMap
.put("s004", new Student("翁虹", 27));hashMap
.put("s005", new Student("葉子楣", 29));hashMap
.put("s005", new Student("葉子楣222", 29));for (String s
: hashMap
.keySet()) {Student student
= hashMap
.get(s
);System
.out
.println(s
+"=="+student
.getName()+"=="+student
.getAge());}}
}
LinkedHashMap
LinkedHashMap在HashMap的基礎上實現了元素的有序性。
LinkedHashMap與LinkedHashSet相同,都是通過鏈表來實現元素的有序性。
public class LinkedHashMapDemo {public static void main(String
[] args
) {LinkedHashMap
<Integer, String> map
= new LinkedHashMap<>();map
.put(100, "abc");map
.put(200, "ccc");map
.put(300, "ddd");Set
<Map
.Entry
<Integer, String>> entries
= map
.entrySet();for (Map
.Entry
<Integer, String> entry
: entries
) {Integer key
= entry
.getKey();String value
= entry
.getValue();System
.out
.println(key
+"==="+value
);}}
}
TreeSet
TreeSet底層的數據結構是二叉樹,與TreeSet相同可以通過自然排序(實現comparable接口重寫compareTo()方法)或者比較排序(通過構造函數添加比較器—重寫Comparator接口的compare方法)。
自然排序
public class TreeMapDemo {public static void main(String
[] args
) {TreeMap
<Student, Integer> map
= new TreeMap<>();map
.put(new Student("鐘楚紅", 23), 10);map
.put(new Student("梅艷芳", 27), 9);map
.put(new Student("李麗珍", 26), 1);map
.put(new Student("翁虹", 27), 4);map
.put(new Student("葉子楣", 29), 2);map
.put(new Student("葉子楣222", 29), 3);System
.out
.println(map
);}
}
public int compareTo(Student s
) {int num
=this.getAge()-s
.getAge();int num2
=num
==0?this.name
.compareTo(s
.name
):num
;return num2
;}
選擇排序
public class TreeMapDemo2 {public static void main(String
[] args
) {TreeMap
<Student, Integer> map
= new TreeMap<>(new Comparator<Student>() {@Overridepublic int compare(Student o1
, Student o2
) {int num
=o1
.getName().compareTo(o2
.getName());int num2
=num
==0?o1
.getAge()-o2
.getAge():num
;return num2
;}});map
.put(new Student("鐘楚紅", 23), 10);map
.put(new Student("梅艷芳", 27), 9);map
.put(new Student("李麗珍", 26), 1);map
.put(new Student("翁虹", 27), 4);map
.put(new Student("葉子楣", 29), 2);map
.put(new Student("葉子楣222", 29), 3);System
.out
.println(map
);}
}
總結
以上是生活随笔為你收集整理的JAVA 映射表的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。