日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA 映射表

發布時間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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<>();//put() 放入元素map.put(1,"aaa");map.put(2,"ccc");map.put(3,"eee");//get()返回鍵對應的值System.out.println(map.get(1));//aaa//size()System.out.println(map.size());//3System.out.println(map);//{1=aaa, 2=ccc, 3=eee}//remove()移除指定鍵System.out.println(map.remove(2));//cccSystem.out.println(map);//{1=aaa, 3=eee}//clear()刪除所有元素map.clear();System.out.println(map);//{}map.put(1,"aaa");map.put(2,"aaa");map.put(3,"eee");System.out.println( map.containsKey(1) );//trueSystem.out.println(map.containsValue("ccc"));//true// keySet();//獲取鍵的集合System.out.println(map.keySet());//[1, 2, 3]// values();//獲取所有值的集合System.out.println(map.values());//[aaa, aaa, eee]} }

遍歷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");//1通過可以通過鍵找值的方式來遍歷Set<Integer> k = map.keySet();for (int key:k) {String v = map.get(key);System.out.println(key+" "+v);}//2通過 Map.Entry<K, V> 方法返回的Set集合遍歷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) {//存儲鍵是 String 值是StudentHashMap<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 元素有序,且唯一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);} }//其中重寫Student的compareTo方法按照年齡排序 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 映射表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。