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

一.Collection集合
Collection集合概述
是單例集合的頂層接口,它表示一組對象,這些對象也稱為Collection的元素
JDK 不提供此接口的任何直接實現(xiàn),它提供更具體的子接口(如Set和List)實現(xiàn)
常用方法:

1.1List集合
List集合概述:
有序集合(也稱為序列),用戶可以精確控制列表中每個元素的插入位置。用戶可以通過整數(shù)索引訪問元素,并搜索列表中的元素
與Set集合不同,列表通常允許重復(fù)的元素
List集合特點:
有索引
可以存儲重復(fù)元素
元素存取有序
List集合特有的方法:

遍歷方式:
//學(xué)生類
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) {
//創(chuàng)建List集合對象
List<Student> list = new ArrayList<Student>();
//創(chuàng)建學(xué)生對象
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("張曼玉", 35);
Student s3 = new Student("王祖賢", 33);
//把學(xué)生添加到集合
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集合的實現(xiàn)類:
1.1.1ArrayList集合
ArrayList集合的底層是數(shù)組結(jié)構(gòu)實現(xiàn),查詢快、增刪慢,它的一些常用方法可以參考List集合的
常用方法。
1.1.2LinkedList集合
LinkedList集合底層是鏈表結(jié)構(gòu)實現(xiàn),查詢慢、增刪快,它有一些特有的常用方法

1.2Set集合
Set集合的特點
元素存取無序
沒有索引、只能通過迭代器或增強for循環(huán)遍歷
不能存儲重復(fù)元素
在我講解HashSet之前我們必須先了解哈希值的概念,方便我們理解接下來的一些集合
哈希值簡介
是JDK根據(jù)對象的地址或者字符串或者數(shù)字算出來的int類型的數(shù)值
如何獲取哈希值
Object類中的public int hashCode():返回對象的哈希碼值
哈希值的特點
同一個對象多次調(diào)用hashCode()方法返回的哈希值是相同的
默認(rèn)情況下,不同對象的哈希值是不同的。而重寫hashCode()方法,可以實現(xiàn)讓不同對象的哈希值相同
1.2.1HashSet集合
HashSet集合的特點
底層數(shù)據(jù)結(jié)構(gòu)是哈希表
對集合的迭代順序不作任何保證,也就是說不保證存儲和取出的元素順序一致
沒有帶索引的方法,所以不能使用普通for循環(huán)遍歷
由于是Set集合,所以是不包含重復(fù)元素的集合
HashSet的基本使用:
public class HashSetDemo01 {
public static void main(String[] args) {
//創(chuàng)建集合對象
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.根據(jù)對象的哈希值計算存儲位置
如果當(dāng)前位置沒有元素則直接存入
如果當(dāng)前位置有元素存在,則進入第二步
2.當(dāng)前元素的元素和已經(jīng)存在的元素比較哈希值
如果哈希值不同,則將當(dāng)前元素進行存儲
如果哈希值相同,則進入第三步
3.通過equals()方法比較兩個元素的內(nèi)容
如果內(nèi)容不相同,則將當(dāng)前元素進行存儲
如果內(nèi)容相同,則不存儲當(dāng)前元素
圖解:

拓展:LinkedHashSet
LinkedHashSet集合特點
哈希表和鏈表實現(xiàn)的Set接口,具有可預(yù)測的迭代次序
由鏈表保證元素有序,也就是說元素的存儲和取出順序是一致的
由哈希表保證元素唯一,也就是說沒有重復(fù)的元素
1.2.2TreeSet集合
TreeSet集合概述
元素有序,可以按照一定的規(guī)則進行排序,具體排序方式取決于構(gòu)造方法
TreeSet():根據(jù)其元素的自然排序進行排序
TreeSet(Comparator comparator) :根據(jù)指定的比較器進行排序
沒有帶索引的方法,所以不能使用普通for循環(huán)遍歷
由于是Set集合,所以不包含重復(fù)元素的集合
要理解好TreeSet必須先了解自然排序Comparable:
案例需求
存儲學(xué)生對象并遍歷,創(chuàng)建TreeSet集合使用無參構(gòu)造方法
要求:按照年齡從小到大排序,年齡相同時,按照姓名的字母順序排序
實現(xiàn)步驟
用TreeSet集合存儲自定義對象,無參構(gòu)造方法使用的是自然排序?qū)υ剡M行排序的
自然排序,就是讓元素所屬的類實現(xiàn)Comparable接口,重寫compareTo(T o)方法
重寫方法時,一定要注意排序規(guī)則必須按照要求的主要條件和次要條件來寫
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;
}
@Override
public 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) {
//創(chuàng)建集合對象
TreeSet<Student> ts = new TreeSet<Student>();
//創(chuàng)建學(xué)生對象
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);
//把學(xué)生添加到集合
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的使用:
案例需求
存儲學(xué)生對象并遍歷,創(chuàng)建TreeSet集合使用帶參構(gòu)造方法
要求:按照年齡從小到大排序,年齡相同時,按照姓名的字母順序排序
實現(xiàn)步驟
用TreeSet集合存儲自定義對象,帶參構(gòu)造方法使用的是比較器排序?qū)υ剡M行排序的
比較器排序,就是讓集合構(gòu)造方法接收Comparator的實現(xiàn)類對象,重寫compare(T o1,T o2)方法
重寫方法時,一定要注意排序規(guī)則必須按照要求的主要條件和次要條件來寫
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) {
//創(chuàng)建集合對象
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//this.age - s.age
//s1,s2
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return num2;
}
});
//創(chuàng)建學(xué)生對象
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);
//把學(xué)生添加到集合
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集合的特點
鍵值對映射關(guān)系
一個鍵對應(yīng)一個值
鍵不能重復(fù),值可以重復(fù)
元素存取無序
Map集合的基本使用:
Map集合的一些常用方法:

public class MapDemo02 {
public static void main(String[] args) {
//創(chuàng)建集合對象
Map<String,String> map = new HashMap<String,String>();
//V put(K key,V value):添加元素
map.put("張無忌","趙敏");
map.put("郭靖","黃蓉");
map.put("楊過","小龍女");
//V remove(Object key):根據(jù)鍵刪除鍵值對元素
// 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():集合的長度,也就是集合中鍵值對的個數(shù)
System.out.println(map.size());
//輸出集合對象
System.out.println(map);
}
}
2.2Map集合的獲取功能
方法介紹:

public class MapDemo03 {
public static void main(String[] args) {
//創(chuàng)建集合對象
Map<String, String> map = new HashMap<String, String>();
//添加元素
map.put("張無忌", "趙敏");
map.put("郭靖", "黃蓉");
map.put("楊過", "小龍女");
//V get(Object key):根據(jù)鍵獲取值
// 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集合的遍歷方式(方式一)
遍歷思路
我們剛才存儲的元素都是成對出現(xiàn)的,所以我們把Map看成是一個夫妻對的集合
把所有的丈夫給集中起來
遍歷丈夫的集合,獲取到每一個丈夫
根據(jù)丈夫去找對應(yīng)的妻子
步驟分析
獲取所有鍵的集合。用keySet()方法實現(xiàn)
遍歷鍵的集合,獲取到每一個鍵。用增強for實現(xiàn)
根據(jù)鍵去找值。用get(Object key)方法實現(xiàn)
public class MapDemo01 {
public static void main(String[] args) {
//創(chuàng)建集合對象
Map<String, String> map = new HashMap<String, String>();
//添加元素
map.put("張無忌", "趙敏");
map.put("郭靖", "黃蓉");
map.put("楊過", "小龍女");
//獲取所有鍵的集合。用keySet()方法實現(xiàn)
Set<String> keySet = map.keySet();
//遍歷鍵的集合,獲取到每一個鍵。用增強for實現(xiàn)
for (String key : keySet) {
//根據(jù)鍵去找值。用get(Object key)方法實現(xiàn)
String value = map.get(key);
System.out.println(key + "," + value);
}
}
}
2.4Map集合的遍歷方式(方式二)
遍歷思路
我們剛才存儲的元素都是成對出現(xiàn)的,所以我們把Map看成是一個夫妻對的集合
獲取所有結(jié)婚證的集合
遍歷結(jié)婚證的集合,得到每一個結(jié)婚證
根據(jù)結(jié)婚證獲取丈夫和妻子
步驟分析
獲取所有鍵值對對象的集合
Set<Map.Entry<K,V>> entrySet():獲取所有鍵值對對象的集合
遍歷鍵值對對象的集合,得到每一個鍵值對對象
用增強for實現(xiàn),得到每一個Map.Entry
根據(jù)鍵值對對象獲取鍵和值
用getKey()得到鍵
用getValue()得到值
代碼實現(xiàn)
public class MapDemo02 {
public static void main(String[] args) {
//創(chuàng)建集合對象
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) {
//根據(jù)鍵值對對象獲取鍵和值
String key = me.getKey();
String value = me.getValue();
System.out.println(key + "," + value);
}
}
}
2.5HashMap集合
HashMap是Map的實現(xiàn)類,HashMap可實現(xiàn)快速存儲和檢索,但缺點是包含的元素是無序的,適用于在Map中插入、刪除和定位元素。常用方法參考Map集合。
到此這篇關(guān)于JAVA容器集合全面解析(Collection和Map)的文章就介紹到這了,更多相關(guān)JAVA容器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java?HashTable與Collections.synchronizedMap源碼深入解析
- 淺談collection標(biāo)簽的oftype屬性能否為java.util.Map
- Java中Collection、List、Set、Map之間的關(guān)系總結(jié)
- 如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for X
- 淺談Java中常用數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)類 Collection和Map
- java 集合----Map、Collection
- Java Map集合與Collection類的使用詳解
相關(guān)文章
Springboot集成百度地圖實現(xiàn)定位打卡的示例代碼
本文主要介紹了Springboot集成百度地圖實現(xiàn)定位打卡的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
Java反射(JDK)與動態(tài)代理(CGLIB)詳解
下面小編就為大家?guī)硪黄獪\談Java反射與動態(tài)代理。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-08-08
alibaba?seata服務(wù)端具體實現(xiàn)
seata是來處理分布式服務(wù)之間互相調(diào)用的事務(wù)問題,本文重點給大家介紹alibaba-seata實現(xiàn)方法,文中通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02

