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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

JAVA 基础之容器集合(Collection和Map)

發布時間:2025/3/15 编程问答 9 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA 基础之容器集合(Collection和Map) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

前言

一.Collection集合

? ? ? ??

?1.1List集合

1.1.1ArrayList集合

1.1.2LinkedList集合

1.2Set集合

1.2.1HashSet集合

HashSet集合保證元素唯一性源碼分析:

1.2.2TreeSet集合

比較器排序Comparator的使用:

?二.Map集合

? 2.1Map集合的概述與特點

2.2Map集合的獲取功能

2.3Map集合的遍歷方式(方式一)

2.4Map集合的遍歷方式(方式二)

2.5HashMap集合


前言

? ? ? ? 本次我將分享的是java中常用的容器集合,大體分為了兩類(Collection單列集合和Map雙列集合),什么是雙列,單列集合呢?看完這篇博客,或許你將有些許收獲。Collection集合下主要講解List集合和Set集合,而雙列集合,我主要講解HashMap集合。

一.Collection集合

? ? ? ??

Collection集合概述

  • 是單例集合的頂層接口,它表示一組對象,這些對象也稱為Collection的元素

  • JDK 不提供此接口的任何直接實現,它提供更具體的子接口(如Set和List)實現

常用方法:

?1.1List集合

List集合概述:

  • 有序集合(也稱為序列),用戶可以精確控制列表中每個元素的插入位置。用戶可以通過整數索引訪問元素,并搜索列表中的元素

  • 與Set集合不同,列表通常允許重復的元素

List集合特點:

  • 有索引

  • 可以存儲重復元素

  • 元素存取有序

List集合特有的方法:

?遍歷方式:

//學生類 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;} }//-------------------------------------------------------- //測試類 public class ListDemo {public static void main(String[] args) {//創建List集合對象List<Student> list = new ArrayList<Student>();//創建學生對象Student s1 = new Student("林青霞", 30);Student s2 = new Student("張曼玉", 35);Student s3 = new Student("王祖賢", 33);//把學生添加到集合list.add(s1);list.add(s2);list.add(s3);//迭代器:集合特有的遍歷方式Iterator<Student> it = list.iterator();while (it.hasNext()) {Student s = it.next();System.out.println(s.getName()+","+s.getAge());}System.out.println("--------");//普通for:帶有索引的遍歷方式for(int i=0; i<list.size(); i++) {Student s = list.get(i);System.out.println(s.getName()+","+s.getAge());}System.out.println("--------");//增強for:最方便的遍歷方式for(Student s : list) {System.out.println(s.getName()+","+s.getAge());}} }

? ? ? ? 接下來講的是List集合的實現類:

1.1.1ArrayList集合

? ? ?ArrayList集合的底層是數組結構實現,查詢快、增刪慢,它的一些常用方法可以參考List集合的

常用方法。

1.1.2LinkedList集合

? ? ?LinkedList集合底層是鏈表結構實現,查詢慢、增刪快,它有一些特有的常用方法

1.2Set集合

???????

Set集合的特點

  • 元素存取無序

  • 沒有索引、只能通過迭代器或增強for循環遍歷

  • 不能存儲重復元素

在我講解HashSet之前我們必須先了解哈希值的概念,方便我們理解接下來的一些集合

  • 哈希值簡介

    是JDK根據對象的地址或者字符串或者數字算出來的int類型的數值

  • 如何獲取哈希值

    Object類中的public int hashCode():返回對象的哈希碼值

  • 哈希值的特點

    • 同一個對象多次調用hashCode()方法返回的哈希值是相同的

    • 默認情況下,不同對象的哈希值是不同的。而重寫hashCode()方法,可以實現讓不同對象的哈希值相同

1.2.1HashSet集合

????????

HashSet集合的特點

  • 底層數據結構是哈希表

  • 對集合的迭代順序不作任何保證,也就是說不保證存儲和取出的元素順序一致

  • 沒有帶索引的方法,所以不能使用普通for循環遍歷

  • 由于是Set集合,所以是不包含重復元素的集合

HashSet的基本使用:

public class HashSetDemo01 {public static void main(String[] args) {//創建集合對象HashSet<String> hs = new HashSet<String>();//添加元素hs.add("hello");hs.add("world");hs.add("java");hs.add("world");//遍歷for(String s : hs) {System.out.println(s);}} }

HashSet集合保證元素唯一性源碼分析:

1.根據對象的哈希值計算存儲位置

如果當前位置沒有元素則直接存入

如果當前位置有元素存在,則進入第二步

2.當前元素的元素和已經存在的元素比較哈希值

如果哈希值不同,則將當前元素進行存儲

如果哈希值相同,則進入第三步

3.通過equals()方法比較兩個元素的內容

如果內容不相同,則將當前元素進行存儲

如果內容相同,則不存儲當前元素

圖解:

拓展:LinkedHashSet

LinkedHashSet集合特點

  • 哈希表和鏈表實現的Set接口,具有可預測的迭代次序

  • 由鏈表保證元素有序,也就是說元素的存儲和取出順序是一致的

  • 由哈希表保證元素唯一,也就是說沒有重復的元素

1.2.2TreeSet集合

????????

TreeSet集合概述

  • 元素有序,可以按照一定的規則進行排序,具體排序方式取決于構造方法

    • TreeSet():根據其元素的自然排序進行排序

    • TreeSet(Comparator comparator) :根據指定的比較器進行排序

  • 沒有帶索引的方法,所以不能使用普通for循環遍歷

  • 由于是Set集合,所以不包含重復元素的集合

要理解好TreeSet必須先了解自然排序Comparable:

  • 案例需求

    • 存儲學生對象并遍歷,創建TreeSet集合使用無參構造方法

    • 要求:按照年齡從小到大排序,年齡相同時,按照姓名的字母順序排序

  • 實現步驟

    • 用TreeSet集合存儲自定義對象,無參構造方法使用的是自然排序對元素進行排序的

    • 自然排序,就是讓元素所屬的類實現Comparable接口,重寫compareTo(T o)方法

    • 重寫方法時,一定要注意排序規則必須按照要求的主要條件和次要條件來寫

public class Student implements Comparable<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 int compareTo(Student s) { // return 0; // return 1; // return -1;//按照年齡從小到大排序int num = this.age - s.age; // int num = s.age - this.age;//年齡相同時,按照姓名的字母順序排序int num2 = num==0?this.name.compareTo(s.name):num;return num2;} } //------------------------------------------------ public class TreeSetDemo02 {public static void main(String[] args) {//創建集合對象TreeSet<Student> ts = new TreeSet<Student>();//創建學生對象Student s1 = new Student("xishi", 29);Student s2 = new Student("wangzhaojun", 28);Student s3 = new Student("diaochan", 30);Student s4 = new Student("yangyuhuan", 33);Student s5 = new Student("linqingxia",33);Student s6 = new Student("linqingxia",33);//把學生添加到集合ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);ts.add(s6);//遍歷集合for (Student s : ts) {System.out.println(s.getName() + "," + s.getAge());}} }

比較器排序Comparator的使用:

  • 案例需求

    • 存儲學生對象并遍歷,創建TreeSet集合使用帶參構造方法

    • 要求:按照年齡從小到大排序,年齡相同時,按照姓名的字母順序排序

  • 實現步驟

    • 用TreeSet集合存儲自定義對象,帶參構造方法使用的是比較器排序對元素進行排序的

    • 比較器排序,就是讓集合構造方法接收Comparator的實現類對象,重寫compare(T o1,T o2)方法

    • 重寫方法時,一定要注意排序規則必須按照要求的主要條件和次要條件來寫

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;} } //=================================================================== public class TreeSetDemo {public static void main(String[] args) {//創建集合對象TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {//this.age - s.age//s1,s2int num = s1.getAge() - s2.getAge();int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;return num2;}});//創建學生對象Student s1 = new Student("xishi", 29);Student s2 = new Student("wangzhaojun", 28);Student s3 = new Student("diaochan", 30);Student s4 = new Student("yangyuhuan", 33);Student s5 = new Student("linqingxia",33);Student s6 = new Student("linqingxia",33);//把學生添加到集合ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);ts.add(s6);//遍歷集合for (Student s : ts) {System.out.println(s.getName() + "," + s.getAge());}} }

?二.Map集合

? ? ? ? 2.1Map集合的概述與特點

  • Map集合概述

    interface Map<K,V> ?K:鍵的類型;V:值的類型
  • Map集合的特點

    • 鍵值對映射關系

    • 一個鍵對應一個值

    • 鍵不能重復,值可以重復

    • 元素存取無序

Map集合的基本使用:

Map集合的一些常用方法:

public class MapDemo02 {public static void main(String[] args) {//創建集合對象Map<String,String> map = new HashMap<String,String>();//V put(K key,V value):添加元素map.put("張無忌","趙敏");map.put("郭靖","黃蓉");map.put("楊過","小龍女");//V remove(Object key):根據鍵刪除鍵值對元素 // System.out.println(map.remove("郭靖")); // System.out.println(map.remove("郭襄"));//void clear():移除所有的鍵值對元素 // map.clear();//boolean containsKey(Object key):判斷集合是否包含指定的鍵 // System.out.println(map.containsKey("郭靖")); // System.out.println(map.containsKey("郭襄"));//boolean isEmpty():判斷集合是否為空 // System.out.println(map.isEmpty());//int size():集合的長度,也就是集合中鍵值對的個數System.out.println(map.size());//輸出集合對象System.out.println(map);} }

2.2Map集合的獲取功能

? ?方法介紹:

public class MapDemo03 {public static void main(String[] args) {//創建集合對象Map<String, String> map = new HashMap<String, String>();//添加元素map.put("張無忌", "趙敏");map.put("郭靖", "黃蓉");map.put("楊過", "小龍女");//V get(Object key):根據鍵獲取值 // System.out.println(map.get("張無忌")); // System.out.println(map.get("張三豐"));//Set<K> keySet():獲取所有鍵的集合 // Set<String> keySet = map.keySet(); // for(String key : keySet) { // System.out.println(key); // }//Collection<V> values():獲取所有值的集合Collection<String> values = map.values();for(String value : values) {System.out.println(value);}} }

2.3Map集合的遍歷方式(方式一)

  • 遍歷思路

    • 我們剛才存儲的元素都是成對出現的,所以我們把Map看成是一個夫妻對的集合

      • 把所有的丈夫給集中起來

      • 遍歷丈夫的集合,獲取到每一個丈夫

      • 根據丈夫去找對應的妻子

  • 步驟分析

    • 獲取所有鍵的集合。用keySet()方法實現

    • 遍歷鍵的集合,獲取到每一個鍵。用增強for實現

    • 根據鍵去找值。用get(Object key)方法實現

    • public class MapDemo01 {public static void main(String[] args) {//創建集合對象Map<String, String> map = new HashMap<String, String>();//添加元素map.put("張無忌", "趙敏");map.put("郭靖", "黃蓉");map.put("楊過", "小龍女");//獲取所有鍵的集合。用keySet()方法實現Set<String> keySet = map.keySet();//遍歷鍵的集合,獲取到每一個鍵。用增強for實現for (String key : keySet) {//根據鍵去找值。用get(Object key)方法實現String value = map.get(key);System.out.println(key + "," + value);}} }

      2.4Map集合的遍歷方式(方式二)

  • 遍歷思路

    • 我們剛才存儲的元素都是成對出現的,所以我們把Map看成是一個夫妻對的集合

      • 獲取所有結婚證的集合

      • 遍歷結婚證的集合,得到每一個結婚證

      • 根據結婚證獲取丈夫和妻子

  • 步驟分析

    • 獲取所有鍵值對對象的集合

      • Set<Map.Entry<K,V>> entrySet():獲取所有鍵值對對象的集合

    • 遍歷鍵值對對象的集合,得到每一個鍵值對對象

      • 用增強for實現,得到每一個Map.Entry

    • 根據鍵值對對象獲取鍵和值

      • 用getKey()得到鍵

      • 用getValue()得到值

  • 代碼實現

public class MapDemo02 {public static void main(String[] args) {//創建集合對象Map<String, String> map = new HashMap<String, String>();//添加元素map.put("張無忌", "趙敏");map.put("郭靖", "黃蓉");map.put("楊過", "小龍女");//獲取所有鍵值對對象的集合Set<Map.Entry<String, String>> entrySet = map.entrySet();//遍歷鍵值對對象的集合,得到每一個鍵值對對象for (Map.Entry<String, String> me : entrySet) {//根據鍵值對對象獲取鍵和值String key = me.getKey();String value = me.getValue();System.out.println(key + "," + value);}} }

2.5HashMap集合

? ? ? ? HashMap是Map的實現類,HashMap可實現快速存儲和檢索,但缺點是包含的元素是無序的,適用于在Map中插入、刪除和定位元素。常用方法參考Map集合。

總結

以上是生活随笔為你收集整理的JAVA 基础之容器集合(Collection和Map)的全部內容,希望文章能夠幫你解決所遇到的問題。

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