生活随笔
收集整理的這篇文章主要介紹了
第三次学JAVA再学不好就吃翔(part76)--Collection类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
學(xué)習(xí)筆記,僅供參考,有錯必糾
集合
Collection類
Collection類是java.util包下的一個類,使用該類時需要導(dǎo)包。它是一個接口,所以,不能被實例化。
成員方法
boolean add(E e
)
boolean remove(Object o
)
void clear()
boolean contains(Object o
)
boolean isEmpty()
int size()
boolean addAll(Collection c
)
boolean removeAll(Collection c
)
boolean containsAll(Collection c
)
boolean retainAll(Collection c
)
package com
.guiyang
.restudy3
;import java
.util
.ArrayList
;
import java
.util
.Collection
;import com
.guiyang
.bean
.Student
;@SuppressWarnings({ "rawtypes", "unchecked" })public class D1ArrayList {public static void main(String
[] args
) {Collection c
= new ArrayList(); boolean b1
= c
.add("abc");boolean b2
= c
.add(true); boolean b3
= c
.add(100);boolean b4
= c
.add(new Student("張三",23)); boolean b5
= c
.add("abc");System
.out
.println(b1
);System
.out
.println(b2
);System
.out
.println(b3
);System
.out
.println(b4
);System
.out
.println(b5
);System
.out
.println(c
.toString());}}
輸出:
true
true
true
true
true
[abc, true, 100, Student [name=張三, age=23], abc]
package com
.guiyang
.restudy3
;import java
.util
.ArrayList
;
import java
.util
.Collection
;@SuppressWarnings({ "rawtypes", "unchecked" })public class D1ArrayList {public static void main(String
[] args
) {Collection c
= new ArrayList(); c
.add("a");c
.add("b");c
.add("c");c
.add("d");System
.out
.println(c
); System
.out
.println(c
.size()); c
.remove("b"); System
.out
.println(c
); System
.out
.println(c
.contains("b")); c
.clear(); System
.out
.println(c
.isEmpty());}
}
輸出:
[a, b, c, d]
4
[a, c, d]
false
true
package com
.guiyang
.restudy3
;import java
.util
.ArrayList
;
import java
.util
.Collection
;@SuppressWarnings({ "rawtypes", "unchecked" })public class D2ArrayList {public static void main(String
[] args
) {Demo1();System
.out
.println("-------");Demo2();System
.out
.println("-------");Demo3();System
.out
.println("-------");Collection c1
= new ArrayList();c1
.add("a");c1
.add("b");c1
.add("c");c1
.add("d");Collection c2
= new ArrayList();c2
.add("a");c2
.add("b");c2
.add("z");boolean b
= c1
.containsAll(c2
); System
.out
.println(b
);}private static void Demo3() {Collection c1
= new ArrayList();c1
.add("a");c1
.add("b");c1
.add("c");c1
.add("d");Collection c2
= new ArrayList();c2
.add("a");c2
.add("b");c2
.add("z");boolean b
= c1
.removeAll(c2
); System
.out
.println(b
);System
.out
.println(c1
);}private static void Demo2() {Collection c1
= new ArrayList();c1
.add("a");c1
.add("b");c1
.add("c");c1
.add("d");Collection c2
= new ArrayList(); c2
.add("a");c2
.add("b");c2
.add("c");c2
.add("d");c1
.addAll(c2
);
System
.out
.println(c1
);}private static void Demo1() {Collection c1
= new ArrayList();c1
.add("a");c1
.add("b");c1
.add("c");c1
.add("d");Collection c2
= new ArrayList();c2
.add("a");c2
.add("b");c2
.add("c");boolean b1
= c1
.retainAll(c2
); System
.out
.println(b1
);System
.out
.println(c1
);boolean b2
= c1
.retainAll(c2
); System
.out
.println(b2
);System
.out
.println(c2
);}
}
輸出:
true
[a, b, c]
false
[a, b, c]
-------
[a, b, c, d, a, b, c, d]
-------
true
[c, d]
-------
false
總結(jié)
以上是生活随笔為你收集整理的第三次学JAVA再学不好就吃翔(part76)--Collection类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。