Collection集合

- collection集合作為List集合和Set集合共有的父類擁有著List集合和Set集合共有的屬性和方法,這里總結(jié)常用的方法。
- add(E e):添加單個元素
- addAll(Collection<? extends E> c):將指定的Collection中的元素一次性添加到此Collection中
- clear():清除collection中的所有元素。
- contains(Object o):判斷該集合是否包含某個元素返回值是Boolean類型
- remove(Object o):移除某個指定的元素返回值boolean類型
- isEmpty():判斷該集合是否為空返回值Boolean類型
- iterator() :集合迭代器用來遍歷集合(這個很常用)
- toArray():將集合轉(zhuǎn)為數(shù)組進行返回
public class Demo_Collection {
public static void main(String[] args) {
Collection collection = new ArrayList();
Demo_add(collection);
Demo_isEmpty(collection);
Demo_contains(collection,"胡歌");
Demo_remove(collection,"楊紫");
Demo_iterator(collection);
Demo_toArray(collection);
Demo_clear(collection);
}
private static void Demo_add(Collection collection) {
collection.add("胡歌");
collection.add("迪麗熱巴");
System.out.println(collection);
String[] str = {"張一山", "楊紫"};
collection.addAll(Arrays.asList(str));
System.out.println(collection);
}
private static void Demo_isEmpty(Collection collection) {
boolean empty = collection.isEmpty();
if (empty){
System.out.println("集合為空");
}else {
System.out.println("集合不為空");
}
}
private static void Demo_contains(Collection collection, String value) {
boolean b = collection.contains(value);
if (b){
System.out.println("找到"+value);
}else {
System.out.println("集合中不存在"+value);
}
}
private static void Demo_remove(Collection collection, String value) {
boolean b = collection.remove(value);
if (b){
System.out.println("成功移除"+value);
System.out.println(collection);
}
}
private static void Demo_iterator(Collection collection) {
//這里利用迭代器對集合進行遍歷
Iterator iterator = collection.iterator();
System.out.println("-------迭代器進行遍歷------");
while (iterator.hasNext()){
String value = (String) iterator.next();
System.out.println(value);
}
//對集合進行遍歷的第二種方法
System.out.println("-----增強for循環(huán)進行遍歷-----");
for (Object o : collection) {
System.out.println(o);
}
}
private static void Demo_toArray(Collection collection) {
Object[] array = collection.toArray();
System.out.println("-----for循環(huán)遍歷該數(shù)組------");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println("------增強for遍歷該數(shù)組------");
for (Object o : array) {
System.out.println(o);
}
}
private static void Demo_clear(Collection collection) {
collection.clear();
System.out.println(collection);
}
}
- 其中Collection集合包括其下的List和Set集合都是單列的,而且也是無序的
- Collection集合是一個抽象類,它有一個Collections的工具類里面是一些List和Set集合都能使用的方法。
- Collections類:此類完全由在 collection 上進行操作或返回 collection 的靜態(tài)方法組成。它包含在 collection 上操作的多態(tài)算法,即“包裝器”,包裝器返回由指定 collection 支持的新 collection,以及少數(shù)其他內(nèi)容。 (注:此類所以的方法都是靜態(tài)方法,都是使用Collections.XXX()直接調(diào)用,<u>如果()內(nèi)所傳入的Collection對象或類為null程序會拋出NullPointerException</u>)
- shuffle(List<?> list):打亂元素的順序
- sort(List<?> list):對元素進行默認升序排序
- copy(List<? super T> dest, List<? extends T> src): 將所有元素從一個列表復制到另一個列表
public class Demo_Collections {
public static void main(String[] args) {
Collection<Integer> collection = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
collection.add(i);
}
System.out.println("原始數(shù)據(jù):" + collection);
Collections.shuffle( (List<Integer>)collection);
System.out.println("打亂之后的數(shù)據(jù):" + collection);
Collections.sort((List<Integer>) collection);
System.out.println("排序之后的數(shù)據(jù):" + collection);
Collection<Integer> collection1 = new ArrayList<>();
collection1.add(10);
collection1.add(11);
System.out.println(collection1);
Collections.copy((List<Integer>)collection,(List<Integer>)collection1);
System.out.println(collection);
}
}
? 這里有一個問題,自定義的數(shù)據(jù)如何進排序。這里看一下官方文檔給的sort函數(shù)的說明:
? “根據(jù)元素的自然順序 對指定列表按升序進行排序。列表中的所有元素都必須實現(xiàn) Comparable 接口。此外,列表中的所有元素都必須是可相互比較的(也就是說,對于列表中的任何 e1 和 e2 元素,e1.compareTo(e2) 不得拋出 ClassCastException)?!?/strong>
意思就是要重寫Comparable接口
public class Person implements Comparable<Person>{
private int age;
private String name;
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Person o) {
//這里根據(jù)年齡判斷
return this.getAge()-o.getAge();//這里this-O代表的是升序,反之代表的是降序
}
}
List集合
官方解釋:有序的 collection(也稱為序列)。此接口的用戶可以對列表中每個元素的插入位置進行精確地控制。用戶可以根據(jù)元素的整數(shù)索引(在列表中的位置)訪問元素,并搜索列表中的元素。
特點:
- List集合是有序的,即元素的存儲順序是一致的
- List集合是允許重復的
- List集合是有索引的,所以有一些帶有索引的方法
- add(int index,E e):在指定位置插入元素
- get(int index):返回指定位置的元素
- remove(int,index):移除指定位置的元素
- set(int index,E e):更改指定位置的元素
public class Demo_ArrayList {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
String[] strings={"胡歌","迪麗熱巴","張一山","楊紫"};
arrayList.addAll(Arrays.asList(strings));
System.out.println(arrayList);
Demo_add(arrayList);
Demo_get(arrayList,2);
Demo_remove(arrayList,2);
Demo_set(arrayList,2,"趙麗穎");
}
private static void Demo_set(ArrayList<String> arrayList, int i, String name) {
String s = arrayList.set(i, name);
System.out.println(s+"被更改為:"+name);
System.out.println(arrayList);
}
private static void Demo_remove(ArrayList<String> arrayList, int i) {
String s = arrayList.remove(i);
System.out.println(s+"被刪除");
System.out.println(arrayList);
}
private static void Demo_add(ArrayList<String> arrayList) {
arrayList.add(2,"古力娜扎");
System.out.println(arrayList);
}
private static void Demo_get(ArrayList<String> arrayList, int i) {
String s = arrayList.get(i);
System.out.println(i+"號元素是:"+s);
}
}
ArrayList和Vector集合
著兩個集合功能和用法差不多,他們底層都是由可變的數(shù)組實現(xiàn)的,所以這兩個集合的查詢速度快,而至善速度相對較慢。Vector集合是JDK1.0版本開始存在的,后面就慢慢被ArrayList所代替了,其中ArrayList是不不同步的。
LinkedList集合
List 接口的鏈接列表實現(xiàn)。實現(xiàn)所有可選的列表操作,并且允許所有元素(包括 null)。除了實現(xiàn) List 接口外,LinkedList 類還為在列表的開頭及結(jié)尾 get、remove 和 insert 元素提供了統(tǒng)一的命名方法。這些操作允許將鏈接列表用作堆棧、隊列或雙端隊列。
(注意,此實現(xiàn)不是同步的,ArrayList也是不同步的。)
特點:
- LinkedList集合底層是基于鏈表實現(xiàn)的,和數(shù)組有相反的特性,查詢慢,增刪快。
- 因為鏈表的實現(xiàn)所以該集合包含了大量的首位操作的方法。
- 使用LinkedList集合特有的方法時不能使用多態(tài)。
常用方法:
- public void addFirst(E e):將指定元素插入此列表的開頭。
- public void addLast(E e):將指定元素添加到此列表的結(jié)尾。
- public void push(E e):將元素推入此列表所表示的堆棧。
- public E getFirst():返回此列表的第一個元素。
- public E getLast():返回此列表的最后一個元素。
- public E removeFirst():移除并返回此列表的第一個元素。
- public E removeLast():移除并返回此列表的最后一個元素。
- public E pop():從此列表所表示的堆棧處彈出一個元素。
- public boolean isEmpty():如果列表不包含元素,則返回true。
public class Demo_LinkedList {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
String[] strings = {"胡歌", "古天樂", "張一山", "楊紫"};
linkedList.addAll(Arrays.asList(strings));
System.out.println("原始數(shù)據(jù):" + linkedList);
Demo_addFirst(linkedList);
Demo_addLast(linkedList);
Demo_pop(linkedList);
Demo_getFirstAndLast(linkedList);
Demo_remove(linkedList);
Demo_isEmpty(linkedList);
}
private static void Demo_isEmpty(LinkedList<String> linkedList) {
boolean b = linkedList.isEmpty();
if (b){
System.out.println("集合為空");
}else {
System.out.println("集合不為空");
}
}
private static void Demo_remove(LinkedList<String> linkedList) {
String first = linkedList.removeFirst();
String last = linkedList.removeLast();
System.out.println("移除First:"+first);
System.out.println("移除Last:"+last);
System.out.println("Demo_remove:"+linkedList);
}
private static void Demo_getFirstAndLast(LinkedList<String> linkedList) {
String first = linkedList.getFirst();
String last = linkedList.getLast();
System.out.println("First:" + first);
System.out.println("Last:" + last);
}
private static void Demo_pop(LinkedList<String> linkedList) {
String s = linkedList.pop();
System.out.println("Demo_pop:" + s);
System.out.println("Demo_pop:" + linkedList);
}
private static void Demo_addLast(LinkedList<String> linkedList) {
linkedList.addLast("趙麗穎");
System.out.println("Demo_addLast:" + linkedList);
}
private static void Demo_addFirst(LinkedList<String> linkedList) {
linkedList.addFirst("迪麗熱巴");
System.out.println("Demo_addFirst:" + linkedList);
}
}
Set集合
官方定義:一個不包含重復元素的 collection。更確切地講,set 不包含滿足 e1.equals(e2) 的元素對 e1 和 e2,并且最多包含一個 null 元素。正如其名稱所暗示的,此接口模仿了數(shù)學上的 set 抽象。
特點:
- 無序的,set集合是一個無序的集合,其下的子集HashSet,TreeSet等都是無序的,但是LinkedHashSet是一個有序的集合。
- 不允許重復的元素。這里判斷重復的元素是顧具對象的hashcode值和equals方法進行判斷的。所以在存儲自定義對象的時候要重新對象的hashcode和equal方法。
- 因為沒有索引,所以不能使用普通的方法進行集合的遍歷。
特有方法:set集合基本上沒有自己的特有的方法,都是從collection接口繼承過來的一些方法。
public class Demo_Set {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("胡歌");
set.add("迪麗熱巴");
set.add("古力娜扎");
set.add("馬兒扎哈");
//set集合的兩種遍歷方式
System.out.println("====增強for遍歷====");
for (String s : set) {
System.out.print(s+" ");
}
System.out.println();
System.out.println("====iterator迭代器遍歷====");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
String s = iterator.next();
System.out.print(s+" ");
}
}
}
?
HashSet和TreeSet集合
HashSet:此類實現(xiàn) Set 接口,由哈希表(實際上是一個 HashMap 實例)支持。它不保證 set 的迭代順序;特別是它不保證該順序恒久不變。此類允許使用 null 元素。 注意,此實現(xiàn)不是同步的。
HashSet的底層結(jié)構(gòu):哈希表=數(shù)組+鏈表(注:JDK1.8之后就是數(shù)組+鏈表/紅黑樹)
具體過程:
TreeSet:基于 TreeMap 的 NavigableSet 實現(xiàn)。使用元素的自然順序對元素進行排序,或者根據(jù)創(chuàng)建 set 時提供的 Comparator 進行排序,具體取決于使用的構(gòu)造方法。注意,此實現(xiàn)不是同步的
LinkedHashSet集合
具有可預知迭代順序的 Set 接口的哈希表和鏈接列表實現(xiàn)。此實現(xiàn)與 HashSet 的不同之外在于,后者維護著一個運行于所有條目的雙重鏈接列表。此鏈接列表定義了迭代順序,即按照將元素插入到 set 中的順序(插入順序)進行迭代。注意,插入順序不 受在 set 中重新插入的 元素的影響。(如果在 s.contains(e) 返回 true 后立即調(diào)用 s.add(e),則元素 e 會被重新插入到 set s 中。) 注意,此實現(xiàn)不是同步的
特點:
- 有序的,多了一條鏈表來記錄元素存儲的順序
- 不允許重復的
- 用法和其他set集合一樣。