最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中HashSet和LinkedHashSet的常用方法示例詳解

 更新時(shí)間:2025年12月02日 16:34:29   作者:柒.梧.  
本文主要介紹了Java中HashSet和LinkedHashSet的常用方法,包括添加、刪除、檢查元素、獲取大小、遍歷和轉(zhuǎn)換為數(shù)組等操作,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

一.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: true

4. 獲取大小

使用 size() 方法獲取集合中元素的數(shù)量。

int size = linkedHashSet.size();
System.out.println("Size: " + size); // 輸出: Size: 2

5. 遍歷 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)文章

  • SpringBoot2.0 ZipKin示例代碼

    SpringBoot2.0 ZipKin示例代碼

    這篇文章主要介紹了SpringBoot2.0 ZipKin示例代碼,詳細(xì)的介紹了什么是ZipKin以及SpringBoot2.0 ZipKin示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Spring中WebDataBinder使用詳解

    Spring中WebDataBinder使用詳解

    這篇文章主要為大家詳細(xì)介紹了Spring中WebDataBinder的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • mybatis-plus QueryWrapper中or,and的使用及說明

    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ù)源過程詳解

    這篇文章主要介紹了SpringBoot整合Druid數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Struts2實(shí)現(xiàn)文件上傳時(shí)顯示進(jìn)度條功能

    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通訊

    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類的使用和注意事項(xiàng)

    這篇文章主要講解Java中BigDecimal類的用法,并簡(jiǎn)單介紹一些注意事項(xiàng),希望能給大家做一個(gè)參考。
    2016-06-06
  • Spring從@Aspect到Advisor使用演示實(shí)例

    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進(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中的10種事務(wù)失效的常見場(chǎng)景,Spring的聲明式事務(wù)功能更是提供了極其方便的事務(wù)配置方式,配合Spring Boot的自動(dòng)配置,大多數(shù)Spring Boot項(xiàng)目只需要在方法上標(biāo)記@Transactional注解,即可一鍵開啟方法的事務(wù)性配置,需要的朋友可以參考下
    2023-11-11

最新評(píng)論

宜宾市| 五莲县| 醴陵市| 抚宁县| 桂东县| 米泉市| 响水县| 乌兰察布市| 福建省| 苍溪县| 固镇县| 共和县| 上思县| 临沭县| 文昌市| 泰来县| 砚山县| 中牟县| 平邑县| 旬阳县| 六盘水市| 陵川县| 普陀区| 菏泽市| 绥阳县| 客服| 全南县| 安化县| 镇江市| 玛纳斯县| 老河口市| 沐川县| 文成县| 乐陵市| 怀安县| 红河县| 蓬莱市| 盐城市| 江安县| 滁州市| 云龙县|