Java?Collection接口中的常用方法總結(jié)
前言
本節(jié)將大概用代碼案例簡單總結(jié)一下 Collection 接口中的一些方法,我們會以他的實(shí)現(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() 將另一個(gè)集合中的元素添加到當(dāng)前集合中
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add("bb");
coll.addAll(coll1);
System.out.println(coll);
}
}

求長度
// size() 求添加的元素個(gè)數(shù)
Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(new String("miao"));
System.out.println(coll.size());
判斷當(dāng)前集合是否為空
Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(new String("miao"));
//isEmpty() 判斷當(dāng)前集合是否為空
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());
判斷當(dāng)前對象是否在集合中
Collection coll = new ArrayList();
coll.add("aniu");
coll.add(123); //自動裝箱
coll.add(new String("miao"));
// contains() 判斷對象是否在當(dāng)前集合中
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() 判斷形參集合中的元素是否在當(dāng)前集合中
System.out.println(coll.containsAll(coll1));
本質(zhì)上依舊是用equals一個(gè)個(gè)比較
移除
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() 從當(dāng)前集合中移除形參集合中的所有元素,即差集
coll.removeAll(coll1);
System.out.println(coll);

removeAll() 相當(dāng)于求差集,那么也有對應(yīng)求交集的!
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() 判斷兩個(gè)集合是否相等,因?yàn)檫@里使用ArrayList()實(shí)現(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)文章
Java使用注解實(shí)現(xiàn)BigDecimal的四舍五入
BigDecimal是Java中的一個(gè)類,位于java.math包中,它提供了任意精度的有符號十進(jìn)制數(shù)字的表示,以及對這些數(shù)字進(jìn)行算術(shù)運(yùn)算的方法,本文介紹了Java使用注解實(shí)現(xiàn)BigDecimal的四舍五入的相關(guān)知識,需要的朋友可以參考下2024-09-09
eclipse自動創(chuàng)建SpringBoot項(xiàng)目報(bào)錯的解決
這篇文章主要介紹了eclipse自動創(chuàng)建SpringBoot項(xiàng)目報(bào)錯的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解OpenAPI開發(fā)如何動態(tài)的添加接口實(shí)現(xiàn)
這篇文章主要為大家介紹了OpenAPI開發(fā)如何動態(tài)的添加接口實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Java實(shí)現(xiàn)添加條形碼到PDF表格的方法詳解
條碼的應(yīng)用已深入生活和工作的方方面面。本文以操作PDF文件為例,介紹如何利用Java語言在編輯表格時(shí),向單元格中添加條形碼,感興趣的可以學(xué)習(xí)一下2022-06-06
IDEA中springboot的熱加載thymeleaf靜態(tài)html頁面的方法
這篇文章主要介紹了IDEA中springboot的熱加載thymeleaf靜態(tài)html頁面的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Java?深入學(xué)習(xí)static關(guān)鍵字和靜態(tài)屬性及方法
這篇文章主要介紹了Java?深入學(xué)習(xí)static關(guān)鍵字和靜態(tài)屬性及方法,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

