Table of Contents
ToggleDifference Between Collection and Collections in Java
接口 Collection 屬於 java.util.package ,用於將物件分組為單一單元,類似 C++ 語言中的容器, List 、 Set 、 Queue 是集合接口的主要子接口, Map 也是 Java 集合框架的一部分,但它並沒有繼承 Collection ,而工具類別 Collections 也是屬於 java.util.package ,定義了一些實用方法,例如:用於對集合進行操作的排序和搜尋,所有靜態方法,為開發人員提供了急需的便利,能夠有效地使用 Collection Framework , Difference Between Collections and Collection in Java 提供這兩種接口及類別的應用方式,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- collection
| +- DifferenceCollectionCollectionsTest.java
單元測試
Difference Between Collection and Collections in Java 提供集合元素等操作。
addAll
Difference Between Collection and Collections 建立兩個 Collection ,使用給定的 Collection 物件,初始化三個元素。
@Test
public void addAll() {
int expectedSize = 3;
Collection<String> collection = Lists.newArrayList("Apple", "Banana", "Cherry");
Collection<String> result = new ArrayList<>();
result.addAll(collection);
System.out.println(result);
assertEquals(expectedSize, result.size());
Collections.addAll(result, "Mango", "Orange", "Peach");
System.out.println(result);
assertEquals(6, result.size());
}
[Apple, Banana, Cherry]
[Apple, Banana, Cherry, Mango, Orange, Peach]
addAllInteger
Difference Between Collection and Collections 建立兩個 Collection ,使用給定的 Collection 物件,初始化三個元素。
@Test
public void addAllInteger() {
int expectedSize = 3;
Collection<Integer> collection = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
Collection<Integer> result = new ArrayList<>();
result.addAll(collection);
System.out.println(result);
assertEquals(expectedSize, result.size());
Collections.addAll(result, 10, 20, 30);
System.out.println(result);
assertEquals(6, result.size());
}
[2147483647, -1, 3]
[2147483647, -1, 3, 10, 20, 30]
sort
Difference Between Collection and Collections 建立一個 List ,增加三個元素,使用 list sort 及 Collections sort 方法,依照預設比較器,由小到大,升序對集合進行排序。
@Test
public void sort() {
List<String> list = Arrays.asList("Banana", "Apple", "Cherry");
System.out.println(list);
list.sort(null);
System.out.println(list);
assertThat(list).containsExactly("Apple", "Banana", "Cherry");
list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
assertThat(list).containsExactly("Mango", "Orange", "Peach");
}
[Banana, Apple, Cherry]
[Apple, Banana, Cherry]
[Orange, Mango, Peach]
[Mango, Orange, Peach]
sortDouble
Difference Between Collection and Collections 建立一個 List ,增加三個元素,使用 list sort 及 Collections sort 方法,依照預設比較器,由小到大,升序對集合進行排序。
@Test
public void sortDouble() {
List<Double> list = Lists.newArrayList(Double.MAX_VALUE, -1d, 3d);
System.out.println(list);
list.sort(null);
System.out.println(list);
assertThat(list).containsExactly(-1d, 3d, Double.MAX_VALUE);
list = Lists.newArrayList(Double.MAX_VALUE, -1d, 3d);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
assertThat(list).containsExactly(-1d, 3d, Double.MAX_VALUE);
}
[1.7976931348623157E308, -1.0, 3.0]
[-1.0, 3.0, 1.7976931348623157E308]
[1.7976931348623157E308, -1.0, 3.0]
[-1.0, 3.0, 1.7976931348623157E308]
min
Difference Between Collections and Collection 建立一個 Collection ,內有三個元素,取最小元素。
@Test
public void min() {
Collection<String> collection = Lists.newArrayList("Apple", "Banana", "Cherry");
String result = Collections.min(collection);
System.out.println(result);
assertThat(result).isEqualTo("Apple");
Collection<Integer> intCollection = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
Integer intResult = Collections.min(intCollection);
System.out.println(intResult);
assertThat(intResult).isEqualTo(-1);
}
Apple
-1
max
Difference Between Collections and Collection 建立一個 Collection ,內有三個元素,取最大元素。
@Test
public void max() {
Collection<String> collection = Lists.newArrayList("Apple", "Banana", "Cherry");
String result = Collections.max(collection);
System.out.println(result);
assertThat(result).isEqualTo("Cherry");
Collection<Integer> intCollection = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
Integer intResult = Collections.max(intCollection);
System.out.println(intResult);
assertThat(intResult).isEqualTo(Integer.MAX_VALUE);
}
Cherry
2147483647
replaceAll
Difference Between Collections and Collection 建立一個 Collection ,內有三個元素,取代指定元素。
@Test
public void replaceAll() {
List<String> list = Lists.newArrayList("Apple", "Banana", "Cherry", "Apple");
System.out.println(list);
boolean result = Collections.replaceAll(list, "Apple", "Mango");
System.out.println(list);
System.out.println(result);
assertThat(result).isTrue();
List<Integer> intList = Lists.newArrayList(Integer.MAX_VALUE, -1, 3, Integer.MAX_VALUE);
System.out.println(intList);
boolean intResult = Collections.replaceAll(intList, Integer.MAX_VALUE, Integer.MIN_VALUE);
System.out.println(intList);
System.out.println(intResult);
assertThat(intResult).isTrue();
}
[Apple, Banana, Cherry, Apple]
[Mango, Banana, Cherry, Mango]
true
[2147483647, -1, 3, 2147483647]
[-2147483648, -1, 3, -2147483648]
true
enumeration
Difference Between Collection and Collections Interface in Java 建立一個 Collection ,增加三個元素,使用 iterator 及 enumeration 遍歷元素,輸出在 console 上。
@Test
public void enumeration() {
Collection<String> collection = Lists.newArrayList("Apple", "Banana", "Cherry");
Iterator<String> it = collection.iterator();
while (it.hasNext()) {
String e = it.next();
System.out.println(e);
}
Enumeration<String> en = Collections.enumeration(collection);
while (en.hasMoreElements()) {
String e = en.nextElement();
System.out.println(e);
}
}
Apple
Banana
Cherry
Apple
Banana
Cherry
list
Difference Between Collection and Collections Interface in Java 建立一個 Vector ,增加三個元素,使用 list 轉換成 ArrayList 。
@Test
public void list() {
int expectedSize = 3;
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
List<String> result = Collections.list(vector.elements());
System.out.println(result);
assertEquals(expectedSize, result.size());
Vector<Integer> intVector = new Vector<>();
intVector.add(Integer.MAX_VALUE);
intVector.add(-1);
intVector.add(3);
List<Integer> intResult = Collections.list(intVector.elements());
System.out.println(intResult);
assertEquals(expectedSize, intResult.size());
}
[Apple, Banana, Cherry]
[2147483647, -1, 3]
DifferenceCollectionCollectionsTest.java
Difference Between Collections and Collection 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.collection;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.junit.Test;
import com.google.common.collect.Lists;
public class DifferenceCollectionCollectionsTest {
@Test
public void addAll() {
int expectedSize = 3;
Collection<String> collection = Lists.newArrayList("Apple", "Banana", "Cherry");
Collection<String> result = new ArrayList<>();
result.addAll(collection);
System.out.println(result);
assertEquals(expectedSize, result.size());
Collections.addAll(result, "Mango", "Orange", "Peach");
System.out.println(result);
assertEquals(6, result.size());
}
@Test
public void addAllWithInteger() {
int expectedSize = 3;
Collection<Integer> collection = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
Collection<Integer> result = new ArrayList<>();
result.addAll(collection);
System.out.println(result);
assertEquals(expectedSize, result.size());
Collections.addAll(result, 10, 20, 30);
System.out.println(result);
assertEquals(6, result.size());
}
@Test
public void sort() {
List<String> list = Arrays.asList("Banana", "Apple", "Cherry");
System.out.println(list);
list.sort(null);
System.out.println(list);
assertThat(list).containsExactly("Apple", "Banana", "Cherry");
list = Arrays.asList("Orange", "Mango", "Peach");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
assertThat(list).containsExactly("Mango", "Orange", "Peach");
}
@Test
public void sortWithDouble() {
List<Double> list = Lists.newArrayList(Double.MAX_VALUE, -1d, 3d);
System.out.println(list);
list.sort(null);
System.out.println(list);
assertThat(list).containsExactly(-1d, 3d, Double.MAX_VALUE);
list = Lists.newArrayList(Double.MAX_VALUE, -1d, 3d);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
assertThat(list).containsExactly(-1d, 3d, Double.MAX_VALUE);
}
@Test
public void min() {
Collection<String> collection = Lists.newArrayList("Apple", "Banana", "Cherry");
String result = Collections.min(collection);
System.out.println(result);
assertThat(result).isEqualTo("Apple");
Collection<Integer> intCollection = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
Integer intResult = Collections.min(intCollection);
System.out.println(intResult);
assertThat(intResult).isEqualTo(-1);
}
@Test
public void max() {
Collection<String> collection = Lists.newArrayList("Apple", "Banana", "Cherry");
String result = Collections.max(collection);
System.out.println(result);
assertThat(result).isEqualTo("Cherry");
Collection<Integer> intCollection = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
Integer intResult = Collections.max(intCollection);
System.out.println(intResult);
assertThat(intResult).isEqualTo(Integer.MAX_VALUE);
}
@Test
public void replaceAll() {
List<String> list = Lists.newArrayList("Apple", "Banana", "Cherry", "Apple");
System.out.println(list);
boolean result = Collections.replaceAll(list, "Apple", "Mango");
System.out.println(list);
System.out.println(result);
assertThat(result).isTrue();
List<Integer> intList = Lists.newArrayList(Integer.MAX_VALUE, -1, 3, Integer.MAX_VALUE);
System.out.println(intList);
boolean intResult = Collections.replaceAll(intList, Integer.MAX_VALUE, Integer.MIN_VALUE);
System.out.println(intList);
System.out.println(intResult);
assertThat(intResult).isTrue();
}
@Test
public void enumeration() {
Collection<String> collection = Lists.newArrayList("Apple", "Banana", "Cherry");
Iterator<String> it = collection.iterator();
while (it.hasNext()) {
String e = it.next();
System.out.println(e);
}
Enumeration<String> en = Collections.enumeration(collection);
while (en.hasMoreElements()) {
String e = en.nextElement();
System.out.println(e);
}
}
@Test
public void list() {
int expectedSize = 3;
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
List<String> result = Collections.list(vector.elements());
System.out.println(result);
assertEquals(expectedSize, result.size());
Vector<Integer> intVector = new Vector<>();
intVector.add(Integer.MAX_VALUE);
intVector.add(-1);
intVector.add(3);
List<Integer> intResult = Collections.list(intVector.elements());
System.out.println(intResult);
assertEquals(expectedSize, intResult.size());
}
}
心得分享
Difference Between Collection and Collections Interface in Java 的區別:
Collection Interface | Collections Class |
---|---|
宣告為接口 | 實用工具類別 |
用於將物件分組為單一單元 | 定義用於操作集合的實用方法 |
從 Java 8 開始包含靜態方法的接口、抽象方法和預設方法 | 只包含靜態方法 |
繼承 Iterable 接口 | 繼承 Object 物件 |