Java中HashSet和LinkedHashSet的常用方法示例詳解
一.HashSet集合
①.add(Object o):向Set集合中添加元素,不允許添加重復(fù)數(shù)據(jù)。
②.size():返回Set集合中的元素個(gè)數(shù)
public class Test {
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>(); //調(diào)用HashSet無參構(gòu)造方法——>創(chuàng)建HashMap對(duì)象并給map全局變量。
set.add("qq");
set.add("bb");
System.out.println(set);
System.out.println(set.size());
}
}注意:不會(huì)按照保存的順序存儲(chǔ)數(shù)據(jù)(順序不定),遍歷時(shí)不能保證下次結(jié)果和上次相同。且向HashSet集合中添加元素,HashSet add方法實(shí)質(zhì)是map全局變量調(diào)用了put方法,將數(shù)據(jù)存到了key,因?yàn)镠ashMap的 key不允許,所以HashSet添加的元素也不允許重復(fù)。
③.remove(Object o): 刪除Set集合中的obj對(duì)象,刪除成功返回true,否則返回false。
④.isEmpty():如果Set不包含元素,則返回 true。
public class Test {
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>();
set.add("qq");
set.add("bb");
System.out.println(set.isEmpty());
System.out.println(set.remove("bb"));
System.out.println(set);
}
}⑤.clear(): 移除此Set中的所有元素。
public class Test {
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>();
set.add("qq");
set.add("bb");
System.out.println(set);
set.clear();
System.out.println(set);
}
}⑥.iterator():返回在此Set中的元素上進(jìn)行迭代的迭代器。
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>();
set.add("qq");
set.add("bb");
Iterator<String> ite =set.iterator();
while(ite.hasNext())
{
System.out.println(ite.next());
}
}⑦.contains(Object o):判斷集合中是否包含obj元素。
public class Test {
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>();
set.add("qq");
set.add("bb");
System.out.println(set.contains("qq"));
}
}⑧:加強(qiáng)for循環(huán)遍歷Set集合。
public class Test {
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>();
set.add("qq");
set.add("bb");
for (String name : set) { //使用foreach進(jìn)行遍歷。
System.out.println(name);
}
}
}二.LinkedHashSet集合
1. 添加元素
使用 add() 方法添加元素。
如果元素已存在,則不會(huì)重復(fù)添加。
linkedHashSet.add("Apple");
linkedHashSet.add("Banana");
linkedHashSet.add("Cherry");
linkedHashSet.add("Apple"); // 重復(fù)元素,不會(huì)添加
System.out.println(linkedHashSet); // 輸出: [Apple, Banana, Cherry]2. 刪除元素
使用 remove() 方法刪除指定元素。
使用 clear() 方法清空所有元素。
linkedHashSet.remove("Banana");
System.out.println(linkedHashSet); // 輸出: [Apple, Cherry]
linkedHashSet.clear();
System.out.println(linkedHashSet); // 輸出: []3. 檢查元素是否存在
使用 contains() 方法檢查是否包含指定元素。
boolean containsApple = linkedHashSet.contains("Apple");
System.out.println("Contains Apple: " + containsApple); // 輸出: Contains Apple: true4. 獲取大小
使用 size() 方法獲取集合中元素的數(shù)量。
int size = linkedHashSet.size();
System.out.println("Size: " + size); // 輸出: Size: 25. 遍歷 LinkedHashSet
使用 for-each 循環(huán)遍歷。
使用 Iterator 遍歷。
// 使用 for-each 循環(huán)遍歷
for (String fruit : linkedHashSet) {
System.out.println(fruit);
}
// 使用 Iterator 遍歷
Iterator<String> iterator = linkedHashSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}6. 轉(zhuǎn)換為數(shù)組
使用 toArray() 方法將 LinkedHashSet 轉(zhuǎn)換為數(shù)組。
String[] array = linkedHashSet.toArray(new String[0]);
for (String s : array) {
System.out.println(s);
}7. 其他常用方法
isEmpty(): 檢查 LinkedHashSet 是否為空。
retainAll(): 保留與指定集合相同的元素。
removeAll(): 刪除與指定集合相同的元素。
LinkedHashSet<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
LinkedHashSet<String> toRetain = new LinkedHashSet<>();
toRetain.add("Apple");
toRetain.add("Cherry");
fruits.retainAll(toRetain); // 只保留 Apple 和 Cherry
System.out.println(fruits); // 輸出: [Apple, Cherry]
fruits.removeAll(toRetain); // 刪除 Apple 和 Cherry
System.out.println(fruits); // 輸出: []到此這篇關(guān)于Java中HashSet和LinkedHashSet的常用方法的文章就介紹到這了,更多相關(guān)Java HashSet和LinkedHashSet內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis-plus QueryWrapper中or,and的使用及說明
使用MyBatis Plus QueryWrapper時(shí),因同時(shí)添加角色權(quán)限固定條件和多字段模糊查詢導(dǎo)致數(shù)據(jù)異常展示,排查發(fā)現(xiàn)OR邏輯覆蓋了權(quán)限條件,通過正確使用and()方法,將條件組合為AND邏輯,解決了權(quán)限過濾失效的問題2025-07-07
SpringBoot整合Druid數(shù)據(jù)源過程詳解
這篇文章主要介紹了SpringBoot整合Druid數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Struts2實(shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條功能
這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Java使用modbus-master-tcp實(shí)現(xiàn)modbus tcp通訊
這篇文章主要為大家詳細(xì)介紹了另外一種Java語(yǔ)言的modbux tcp通訊方案,那就是modbus-master-tcp,文中的示例代碼講解詳細(xì),需要的可以了解下2023-12-12
Java BigDecimal類的使用和注意事項(xiàng)
這篇文章主要講解Java中BigDecimal類的用法,并簡(jiǎn)單介紹一些注意事項(xiàng),希望能給大家做一個(gè)參考。2016-06-06
Spring從@Aspect到Advisor使用演示實(shí)例
這篇文章主要介紹了Spring從@Aspect到Advisor使用演示實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
使用MapStruct進(jìn)行Java Bean映射的方式
MapStruct是一個(gè)用于JavaBean映射的注解處理器,它通過注解生成類型安全且性能優(yōu)異的映射代碼,避免手動(dòng)編寫重復(fù)的樣板代碼,主要特性包括類型安全、高性能、簡(jiǎn)潔和可定制性,使用步驟包括定義映射接口、創(chuàng)建源類和目標(biāo)類、生成映射代碼并調(diào)用映射方法2025-02-02
Spring中的10種事務(wù)失效的常見場(chǎng)景
這篇文章主要介紹了Spring中的10種事務(wù)失效的常見場(chǎng)景,Spring的聲明式事務(wù)功能更是提供了極其方便的事務(wù)配置方式,配合Spring Boot的自動(dòng)配置,大多數(shù)Spring Boot項(xiàng)目只需要在方法上標(biāo)記@Transactional注解,即可一鍵開啟方法的事務(wù)性配置,需要的朋友可以參考下2023-11-11

