欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java?Collection接口中的常用方法總結(jié)

 更新時間:2022年12月05日 08:41:52   作者:館主阿牛  
這篇文章將大概用代碼案例簡單總結(jié)一下?Collection?接口中的一些方法,我們會以他的實現(xiàn)類?Arraylist?為例創(chuàng)建對象??煲黄饋砜纯窗?/div>

前言

本節(jié)將大概用代碼案例簡單總結(jié)一下 Collection 接口中的一些方法,我們會以他的實現(xiàn)類 Arraylist 為例創(chuàng)建對象。一起來看看吧!

Collection 接口中的常用方法

添加

import java.util.ArrayList;
import java.util.Collection;

/**
 * @Author:Aniu
 * @Date:2022/12/4 16:19
 * @description TODO
 */
public class Demo {
    public static void main(String[] args) {
        Collection coll = new ArrayList();
        // add(Object e) 增加
        coll.add("aniu");
        coll.add(123); //自動裝箱
        coll.add(new String("miao"));
        System.out.println(coll);
        System.out.println("------------------");
        // addAll() 將另一個集合中的元素添加到當前集合中
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add("bb");
        coll.addAll(coll1);
        System.out.println(coll);
    }
}

求長度

// size() 求添加的元素個數(shù)
 Collection coll = new ArrayList();
 coll.add("aniu");
 coll.add(123); //自動裝箱
 coll.add(new String("miao"));
 System.out.println(coll.size());

判斷當前集合是否為空

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(new String("miao"));
//isEmpty() 判斷當前集合是否為空
System.out.println(coll.isEmpty());

清空集合元素

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(new String("miao"));
//clear() 清空集合元素
System.out.println(coll.clear());

判斷當前對象是否在集合中

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(new String("miao"));
// contains() 判斷對象是否在當前集合中
System.out.println(coll.contains(new String("aniu")));

這里要注意的是,contains本質(zhì)上是用equals比較的,因此,對于自定義對象,要記得重寫equals方法!

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(new String("miao"));
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add("aniu");
// containsAll() 判斷形參集合中的元素是否在當前集合中
System.out.println(coll.containsAll(coll1));

本質(zhì)上依舊是用equals一個個比較

移除

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(456);
coll.add(new String("miao"));
// remove() 移除
coll.remove(123);
System.out.println(coll);
System.out.println("------------");

Collection coll1 = new ArrayList();
coll1.add(456);
coll1.add(new String("miao"));
// removeAll() 從當前集合中移除形參集合中的所有元素,即差集
coll.removeAll(coll1);
System.out.println(coll);

removeAll() 相當于求差集,那么也有對應求交集的!

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(new String("miao"));
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add(new String("miao"));
// retainAll() 即求交集
coll.retainAll(coll1);
System.out.println(coll);

判斷相等

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
Collection coll1 = new ArrayList();
coll1.add(123);
coll.add("aniu");
// equals() 判斷兩個集合是否相等,因為這里使用ArrayList()實現(xiàn),因此要考慮順序
System.out.println(coll.equals(coll1));

集合轉(zhuǎn)換為數(shù)組

Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
// toArray() 集合轉(zhuǎn)數(shù)組
Object[] arr = coll.toArray();
System.out.println(Arrays.toString(arr));

數(shù)組轉(zhuǎn)換為集合

既然說到了集合轉(zhuǎn)數(shù)組,這里就說一下數(shù)組轉(zhuǎn)集合!

 List list = Arrays.asList(new String[]{"aniu", "tom"});
 System.out.println(list);

結(jié)語

本來關(guān)于這些api是不想總結(jié)的,像String中的一些api,和其他語言中的差不多,我就沒總結(jié)!集合中的方法名與其他語言稍微有不同,這里快速過一下。

到此這篇關(guān)于Java Collection接口中的常用方法總結(jié)的文章就介紹到這了,更多相關(guān)Java Collection接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis-Plus 批量保存的操作方法

    MyBatis-Plus 批量保存的操作方法

    在項目開發(fā)中,需要插入批量插入20多萬條數(shù)據(jù),通過日志觀察,發(fā)現(xiàn)在調(diào)用MyBatis-Plus中的saveBatch()方法性能非常的差,本篇文章主要分享一下saveBatch()的原理以及使用的注意事項,感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • Java顯示程序包不存在的三種解決方法總結(jié)

    Java顯示程序包不存在的三種解決方法總結(jié)

    在Java開發(fā)中,有時會遇到“程序包javax.servlet不存在”等錯誤提示,這通常是因為缺少必要的庫或依賴項,這篇文章主要給大家介紹了關(guān)于Java顯示程序包不存在的三種解決方法,需要的朋友可以參考下
    2024-07-07
  • spring-boot 多線程并發(fā)定時任務的解決方案

    spring-boot 多線程并發(fā)定時任務的解決方案

    這篇文章主要介紹了spring-boot 多線程并發(fā)定時任務的解決方案,需要的朋友可以參考下
    2019-08-08
  • Spring Validation和Hibernate Validator結(jié)合國際化代碼實例

    Spring Validation和Hibernate Validator結(jié)合國際化代碼實例

    這篇文章主要介紹了Spring Validation和Hibernate Validator結(jié)合國際化代碼實例,我們需要對請求參數(shù)進行非空、長度、正確性進行校驗, 本文主要講解Spring Validation 和 Hibernate Validator, 同時整合i18n(國際化)實現(xiàn)參數(shù)校驗自動,需要的朋友可以參考下
    2023-10-10
  • java虛擬機原理:Class字節(jié)碼二進制文件分析

    java虛擬機原理:Class字節(jié)碼二進制文件分析

    class文件全名稱為Java class文件,主要在平臺無關(guān)性和網(wǎng)絡移動性方面使Java更適合網(wǎng)絡。它在平臺無關(guān)性方面的任務是:為Java程序提供獨立于底層主機平臺的二進制形式的服務。下面我們來詳細解讀下它吧
    2021-09-09
  • java客戶端Etcd官方倉庫jetcd中KeepAlive接口實現(xiàn)

    java客戶端Etcd官方倉庫jetcd中KeepAlive接口實現(xiàn)

    這篇文章主要為大家介紹了java客戶端Etcd官方倉庫jetcd中KeepAlive接口實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,多多加薪
    2022-02-02
  • Java操作Mongodb數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的增刪查改功能示例

    Java操作Mongodb數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的增刪查改功能示例

    這篇文章主要介紹了Java操作Mongodb數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的增刪查改功能,結(jié)合完整實例形式分析了java針對MongoDB數(shù)據(jù)庫的連接、增刪改查等相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • 詳解Springboot應用啟動以及關(guān)閉時完成某些操作

    詳解Springboot應用啟動以及關(guān)閉時完成某些操作

    這篇文章主要介紹了詳解Springboot應用啟動以及關(guān)閉時完成某些操作,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • JDK生成WebService客戶端代碼以及調(diào)用方式

    JDK生成WebService客戶端代碼以及調(diào)用方式

    WebService 是一種跨編程語言和跨操作系統(tǒng)平臺的遠程調(diào)用技術(shù),下面這篇文章主要給大家介紹了關(guān)于JDK生成WebService客戶端代碼以及調(diào)用方式的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-08-08
  • Unity2019-2020 個人版官方免費激活詳細方法

    Unity2019-2020 個人版官方免費激活詳細方法

    這篇文章主要介紹了Unity2019-2020 個人版官方免費激活詳細方法,激活方法分位兩種一種是激活新許可證,一種是手動激活,感興趣的朋友跟隨小編一起看看吧
    2021-04-04

最新評論