一、Collection接口的方法
是抽象接口, 定義集合的相關(guān)操作,存儲元素集合
-
添加一個元素
Collection<String> list = new ArrayList();
list.add("Merry");
list.add("Jack");
System.out.println(list); //重寫了toString方法
-
刪除一個元素
list.remove("Jack");
-
獲取元素的個數(shù)
System.out.println(list.size());
-
判斷是否包含一個元素
if (list.contains("Jack")){
System.out.println("有這個元素");
}else{
System.out.println("沒有這個元素");
}
-
判斷是否為空
if (list.isEmpty()){
System.out.println("是空的");
}
-
判斷是否包含一個元素
if (list.contains("Jack")){
System.out.println("有這個元素");
}else{
System.out.println("沒有這個元素");
}
-
判斷兩個集合是否相同
Collection<String > list2 = new ArrayList<>();
list2.add("Android");
list2.add("Jack");
list2.add("Tom");
list2.add("Merry");
if (list.equals(list2)){
System.out.println("兩個集合相同");
}else{
System.out.println("兩個集合不相同");
}
-
清空
list.clear();
System.out.println(list);

image.png
二、集合的遍歷
集合框架:Iterator
- Iterator(迭代器)用于遍歷集合元素,獲取迭代器可以使用Collection定義的方法:-Iterator iterator()
- 迭代器Iterator是一個接口,集合在重寫Collection的iterator()方法時利用內(nèi)部類提供了迭代器的實現(xiàn)
- Iterator提供了統(tǒng)一的遍歷集合元素的方式,其提供了用于遍歷集合的方法(一般結(jié)合while()使用):
? ?boolean hasNext():判斷集合是否還有元素可以遍歷
? ? E next():返回迭代的下一個元素
? ? Object next():返回迭代器剛越過的元素的引用,返回值是Object,需要強制轉(zhuǎn)換成自己需要的類型
? void remove():刪除迭代器剛越過的元素- Iterator有一個很大的優(yōu)點,就是我們不必知道集合的內(nèi)部結(jié)果,集合的內(nèi)部結(jié)構(gòu)、狀態(tài)由Iterator來維持,
- 通過統(tǒng)一的方法hasNext()、next()來判斷、獲取下一個元素,
1. 使用Iterator來遍歷
//hasNext 判斷是否有元素
//next 獲取下一個對象
//remove 刪除當前遍歷后的對象
Iterator iterator = list2.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
2. for-each 增強for循環(huán) 快速循環(huán)
for (String string:list2){
System.out.println(string+" ");
}
3. for 循環(huán)
for (int i = 0;i < list2.size();i++){
System.out.println(((ArrayList<String>) list2).get(i));
}
三、List接口 (extends Collection)
| ArrayList | LinkedArrayList |
|---|---|
| 連續(xù)的內(nèi)存空間 | 內(nèi)部使用鏈表實現(xiàn) ,基本不連續(xù) |
| ? ? ? ? ? ? ? ?優(yōu)點:訪問方便 get() ???? ? ? ? ? ? ? 缺點:刪除 添加 不高效 | 優(yōu)點:增加 刪除效率高 |
集合里面只能放對象,自動的將基本數(shù)據(jù)類型包裝為對應(yīng)的類
基本數(shù)據(jù)類型: byte short int long float double char boolean
包裝類: Byte Short Integer Long Float Double Char Boolean
常見方法:
- add(element)在末尾添加;
- add(i,element)在指定位置插入
- 訪問指定元素
- 修改一個元素
- 刪除指定位置的元素
- 刪除指定的對象
- 刪除所有 清空
- 將一個集合的內(nèi)容添加到當前集合中
- 取兩個集合的交集
- 訪問某個對象在集合里面的索引 第一次 / 最后一次 出現(xiàn)的位置
- 將ArrayList 轉(zhuǎn)化為普通數(shù)組
- 獲取某個范圍的子集合
代碼示例:
ArrayList<Integer> score = new ArrayList<>();
score.add(2);
score.add(3); //在末尾添加
score.add(0,1);//在指定位置插入
System.out.println(score);
//訪問指定元素
score.get(1);
//修改一個元素
score.set(0,0);
System.out.println(score);
//刪除
score.remove(0); //刪除指定位置的元素
System.out.println(score);
score.remove((Integer)2); //刪除指定的對象
System.out.println(score);
score.clear(); //刪除所有 清空
System.out.println(score);
ArrayList<Integer> a1 = new ArrayList<>();
a1.add(1);
a1.add(2);
a1.add(3);
//將一個集合的內(nèi)容添加到當前集合中
score.addAll(a1);
System.out.println(score);
ArrayList<Integer> a2 = new ArrayList<>();
a2.add(2);
a2.add(3);
score.retainAll(a2); //取兩個集合的交集
System.out.println(score);
//indexOf 訪問某個對象在集合里面的索引
ArrayList<Integer> a3 = new ArrayList<>();
a3.add(1);
a3.add(2);
a3.add(2);
a3.add(1);
System.out.println(a3.indexOf(2)); //第一次出現(xiàn)的位置
System.out.println(a3.lastIndexOf(2)); //最后一次出現(xiàn)的位置
//將ArrayList 轉(zhuǎn)化為普通數(shù)組
Integer[] objects = new Integer[a3.size()];
a3.toArray(objects);
for (Integer integer:objects){
System.out.print(integer);
}
System.out.println("\n----------------------------");
Object[] objects1 = a3.toArray();
for (Object ob:objects1){
System.out.print(ob);
}
System.out.println(" \n----------------------------");
Iterator iterator = a3.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next());
}
// 獲取某個范圍的子集合
List<Integer> integers = a3.subList(1,3);
System.out.println("\n"+integers);
四、Lambda表達式
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(2);
nums.add(3);
nums.add(4);
//Lambda表達式
nums.removeIf(element -> element % 2 == 0);
for (int i = 0;i < nums.size();i++){
Integer obj = nums.get(i);
if (obj % 2 == 0){
nums.remove(i);
i--; //管理好i的指向 防止跳過某個元素
}
}
System.out.println(nums);
使用方式:
1. 使用方式 定義一個類實現(xiàn)接口
ArrayClass ac = new ArrayClass();
int[] num = {1,2,3,4,5,6};
GYLClass gc = new GYLClass();
ac.test(num,gc);
//閉包 closure 把函數(shù)作為一個方法的參數(shù)
class ArrayClass{
public void test(int[] target,Show show){
for (int element:target){
show.customShow(element);
}
}
}
//必須是接口 這個接口里面只有一個方法
interface Show{
void customShow(int element);
}
class GYLClass implements Show{
@Override
public void customShow(int element) {
System.out.println(element);
}
}
2. 使用匿名類
ArrayClass ac = new ArrayClass();
int[] num = {1,2,3,4,5,6};
GYLClass gc = new GYLClass();
ac.test(num, new Show() {
@Override
public void customShow(int element) {
System.out.println(element);
}
});
3. 使用Lambda表達式
// 如果參數(shù)是一個接口對象 且接口里面只有一個方法
// 把這個方法作為參數(shù)傳遞 可以省略方法名
ArrayClass ac = new ArrayClass();
int[] num = {1,2,3,4,5,6};
ac.test(num,(int element) -> {System.out.println(element); } );
4. 如果只有一個參數(shù) 參數(shù)類型可以省略
ArrayClass ac = new ArrayClass();
int[] num = {1,2,3,4,5,6};
ac.test(num,element -> {System.out.println(element);});
5. 如果代碼塊里只有一行語句 大括號可以省略
ArrayClass ac = new ArrayClass();
int[] num = {1,2,3,4,5,6};
ac.test(num,element -> System.out.println(element));
Java集合框架:

Java集合框架