Collection接口和常用方法
一、Collection接口實(shí)現(xiàn)類的特點(diǎn)
public interface Collection
- Collection實(shí)現(xiàn)子類可以存放多個元素,每個元素可以是Object。
- 有些Collection的實(shí)現(xiàn)類,可以存放重復(fù)的元素,有些不可以。
- 有些Collection的實(shí)現(xiàn)類,有些是有序的(如:List),有些不是有序的(如:Set)。
- Collection接口沒有直接的實(shí)現(xiàn)子類,是通過它的子接口Set 和 List來實(shí)現(xiàn)的。
二、常用方法
- add:添加單個元素
- remove:刪除指定元素
- contains:查找元素是否存在
- size:獲取元素個數(shù)
- isEmpty:判斷是否為空
- clear:清空
- addAll:添加多個元素
- containsAll:查找多個元素是否都存在
- removeAll:刪除多個元素
//代碼演示:
import java.util.ArrayList;
import java.util.List;
/**
* @author
* @version 1.0
*/
public class CollectionMethod {
@SuppressWarnings({"all"})
public static void main(String[] args) {
//Collecton接口常用方法,以實(shí)現(xiàn)子類ArrayList來演示
List list = new ArrayList();
// add:添加單個元素
list.add("jack");
list.add("Tom");
list.add(10);//有自動裝箱的過程,相當(dāng)于list.add(new Integer(10));
list.add(true);
System.out.println("list = " + list);
// remove:刪除指定元素
list.remove(0);//刪除第一個元素
System.out.println(list.remove(true));//返回是否刪除成功
System.out.println("list = "+list);
// contains:查找元素是否存在
System.out.println(list.contains("Tom"));
// size:獲取元素個數(shù)
System.out.println(list.size());
// isEmpty:判斷集合是否為空
System.out.println(list.isEmpty());
// clear:清空集合
list.clear();
System.out.println("list = " + list);
// addAll:添加多個元素,實(shí)現(xiàn)了Collection接口的類的對象都可以傳入
ArrayList list2 = new ArrayList();
list2.add("紅樓夢");
list2.add("三國演義");
list.addAll(list2);
System.out.println("list = "+ list);
// containsAll:查找多個元素是否都存在
System.out.println(list.containsAll(list2));
// removeAll:刪除多個元素
list.add("聊齋");
list.removeAll(list2);
System.out.println("list = " + list);
}
}
/*
運(yùn)行結(jié)果:
list = [jack, Tom, 10, true]
true
list = [Tom, 10]
true
2
false
list = []
list = [紅樓夢, 三國演義]
true
list = [聊齋]
*/
三、Collection接口遍歷元素方式1 —— 使用 iterator(迭代器)
-
基本介紹:
- Iterator對象稱為迭代器,主要用于遍歷 Collection 集合中的元素。
- 所有實(shí)現(xiàn)了 Collection 接口的集合類都有都有一個 iterator() 方法,用以返回一個實(shí)現(xiàn)了 Iterator接口的對象,即可以返回一個迭代器。
- Iterator 僅用于遍歷集合,Iterator 本身并不存放對象。
- Iterator的結(jié)構(gòu):
-
迭代器的執(zhí)行原理:
Iterator iterator = coll.iterator();//得到一個集合的迭代器 while (iterator.hasNext()) { //hasNext():判斷是否還有下一個元素; System.out.println(iterator.next()); //next()作用:1. 下移 2. 將下移以后集合位置上的元素返回 }
-
Iterator接口的方法
注意:在調(diào)用 it.next()方法前必須要調(diào)用 it.hasNext() 進(jìn)行檢測。若不調(diào)用,且下一條記錄無效,直接調(diào)用 it.next() 會拋出 NoSuchElementException異常。
-
-
代碼演示:
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /** * @author * @version 1.0 */ public class CollectionIterator { @SuppressWarnings({"all"}) public static void main(String[] args) { //Collection接口遍歷對象方式1:迭代器遍歷 Collection col = new ArrayList(); col.add(new Book("三國演義","羅貫中",10.1)); col.add(new Book("紅樓夢","曹雪芹",20.1)); col.add(new Book("西游記","吳承恩",30.1)); //System.out.println(col); //現(xiàn)在希望能夠遍歷 col集合 //1. 先得到 col 對應(yīng)的 迭代器 Iterator iterator = col.iterator(); //2. 使用 while循環(huán)遍歷 // while (iterator.hasNext()){//判斷是否還有數(shù)據(jù) // //返回下一個元素,類型是Object // Object obj = iterator.next(); // System.out.println(obj); // } //這里有一個快捷鍵,可以快速生成 while循環(huán) =》itit //顯示所有快捷鍵的快捷鍵 ctrl + j //itit while (iterator.hasNext()) { Object next = iterator.next(); System.out.println(next); } //3. 當(dāng)退出while循環(huán)后,這時 iterator迭代器 指向了最后的元素 //iterator.next();//這里再使用next()就會拋出異常java.util.NoSuchElementException //4. 如果希望再次遍歷,就需要重置iterator迭代器 iterator = col.iterator(); System.out.println("第二次遍歷"); while (iterator.hasNext()) { Object obj = iterator.next(); System.out.println(obj); } } } class Book{ private String name; private String author; private double price; public Book(String name, String author, double price) { this.name = name; this.author = author; this.price = price; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}'; } } /* 運(yùn)行結(jié)果: Book{name='三國演義', author='羅貫中', price=10.1} Book{name='紅樓夢', author='曹雪芹', price=20.1} Book{name='西游記', author='吳承恩', price=30.1} 第二次遍歷 Book{name='三國演義', author='羅貫中', price=10.1} Book{name='紅樓夢', author='曹雪芹', price=20.1} Book{name='西游記', author='吳承恩', price=30.1} */
四、Collection接口遍歷元素方式2 —— for循環(huán)增強(qiáng)
增強(qiáng)for循環(huán),可以代替iterator迭代器。
特點(diǎn):增強(qiáng)for就是簡化版的iterator,本質(zhì)一樣,foreach的底層就是迭代器。只能用于遍歷集合或數(shù)組。
-
基本語法:文章來源:http://www.zghlxwxcb.cn/news/detail-409848.html
//for( 元素類型 元素名 : 集合名或數(shù)組名){ // 訪問元素 //} for (Object object : col){ System.out.println(object); }
-
案例演示:文章來源地址http://www.zghlxwxcb.cn/news/detail-409848.html
import java.util.ArrayList; import java.util.Collection; public class CollectionFor { @SuppressWarnings({"all"}) public static void main(String[] args) { Collection col = new ArrayList(); col.add(new Book("三國演義","羅貫中",10.1)); col.add(new Book("紅樓夢","曹雪芹",20.1)); col.add(new Book("西游記","吳承恩",30.1)); //1. 使用增強(qiáng)for循環(huán),在Collection集合 //2. 增強(qiáng)for,底層仍然是迭代器 //3. 增強(qiáng)for可以理解成就是簡化版的迭代器 for(Object book: col){ System.out.println(book); } //增強(qiáng)for,也可以直接在數(shù)組使用 int[] nums = {1,2,3,4,5,6}; for (int i : nums){ System.out.println(i); } //增強(qiáng)for 快捷鍵:I 或 col.for } } /* 運(yùn)行結(jié)果: Book{name='三國演義', author='羅貫中', price=10.1} Book{name='紅樓夢', author='曹雪芹', price=20.1} Book{name='西游記', author='吳承恩', price=30.1} 1 2 3 4 5 6 */
到了這里,關(guān)于Collection接口和常用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!