Table of Contents
ToggleJava Collections Methods
是集合框架中的工具類之一,介紹常見的 addAll 、 binarySearch 、 copy 、 shuffle 等方法,Java Util Collections 支援集合操作,提供了許多靜態方法運用,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- collection
| +- CollectionsMethodsTest.java
單元測試
Collections Class Java 集合類別工具,提供反轉、搜尋、複製、隨機排列位置等操作集合中的元素。
addAll
建立一個 ArrayList ,內有三個元素,再增加三個元素。
@Test
public void addAll() {
int expectedSize = 6;
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
Collections.addAll(list, "Papaya", "Strawberry", "Watermelon");
System.out.println(list);
assertThat(list).hasSize(expectedSize);
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
Collections.addAll(intList, 10, 20, 30);
System.out.println(intList);
assertThat(intList).hasSize(expectedSize);
}
[Mango, Orange, Peach]
[Mango, Orange, Peach, Papaya, Strawberry, Watermelon]
[2147483647, -1, 3]
[2147483647, -1, 3, 10, 20, 30]
binarySearch
Collections Class Java 建立一個 ArrayList ,內有三個元素,搜尋特定元素,傳回 index。
@Test
public void binarySearch() {
int expectedIndex = 1;
List<String> list = Arrays.asList("Mango", "Orange", "Peach");
int result = Collections.binarySearch(list, "Orange");
System.out.println(result);
assertThat(result).isEqualTo(expectedIndex);
List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
result = Collections.binarySearch(intList, -1);
System.out.println(result);
assertThat(result).isEqualTo(expectedIndex);
}
1
1
copy
Collections Class Java 建立一個 ArrayList ,內有三個元素,複製另一個 List 到原 List 會覆蓋相同 index 元素。
@Test
public void copy() {
int expectedSize = 3;
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
List<String> list2 = new ArrayList<>(Arrays.asList("Papaya", "Strawberry"));
Collections.copy(list, list2);
System.out.println(list);
assertThat(list).hasSize(expectedSize);
assertThat(list).containsExactly("Papaya", "Strawberry", "Peach");
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
List<Integer> intList2 = new ArrayList<>(Arrays.asList(10, 20));
Collections.copy(intList, intList2);
System.out.println(intList);
assertThat(intList).hasSize(expectedSize);
assertThat(intList).containsExactly(10, 20, 3);
}
[Mango, Orange, Peach]
[Papaya, Strawberry, Peach]
[2147483647, -1, 3]
[10, 20, 3]
disjoint
Collections in Java 建立一個 ArrayList ,內有三個元素,比對另一個 List 內有兩個元素,若沒有共同元素,則返回 true。
@Test
public void disjoint() {
List<String> list = Arrays.asList("Mango", "Orange", "Peach");
List<String> list2 = Arrays.asList("Papaya", "Strawberry");
boolean result = Collections.disjoint(list, list2);
System.out.println(result);
assertThat(result).isTrue();
list2 = Arrays.asList("Mango", "Strawberry");
result = Collections.disjoint(list, list2);
System.out.println(result);
assertThat(result).isFalse();
List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
List<Integer> intList2 = Arrays.asList(10, 20);
result = Collections.disjoint(intList, intList2);
System.out.println(result);
assertThat(result).isTrue();
intList2 = Arrays.asList(Integer.MAX_VALUE, 20);
result = Collections.disjoint(intList, intList2);
System.out.println(result);
assertThat(result).isFalse();
}
true
false
true
false
shuffle
Collections in Java 建立一個 ArrayList ,內有三個元素,隨機排列位置。
@Test
public void shuffle() {
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
assertThat(list).contains("Mango", "Orange", "Peach");
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
Collections.shuffle(intList);
System.out.println(intList);
assertThat(intList).contains(Integer.MAX_VALUE, -1, 3);
}
[Mango, Orange, Peach]
[Peach, Mango, Orange]
[2147483647, -1, 3]
[3, -1, 2147483647]
reverse
Collections in Java 建立一個 ArrayList ,內有三個元素,反轉元素。
@Test
public void reverse() {
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
assertThat(list).containsExactly("Peach", "Orange", "Mango");
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
Collections.reverse(intList);
System.out.println(intList);
assertThat(intList).containsExactly(3, -1, Integer.MAX_VALUE);
}
[Mango, Orange, Peach]
[Peach, Orange, Mango]
[2147483647, -1, 3]
[3, -1, 2147483647]
swap
Collections in Java 建立一個 ArrayList ,內有三個元素,交換元素位置。
@Test
public void swap() {
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
Collections.swap(list, 0, 2);
System.out.println(list);
assertThat(list).containsExactly("Peach", "Orange", "Mango");
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
Collections.swap(intList, 0, 2);
System.out.println(intList);
assertThat(intList).containsExactly(3, -1, Integer.MAX_VALUE);
}
[Mango, Orange, Peach]
[Peach, Orange, Mango]
[2147483647, -1, 3]
[3, -1, 2147483647]
CollectionsMethodsTest.java
Collections in Java 新增單元測試,驗證 Collections Methods Java 是否符合預期。
package org.ruoxue.java_147.collection;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
public class CollectionsMethodsTest {
@Test
public void addAll() {
int expectedSize = 6;
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
Collections.addAll(list, "Papaya", "Strawberry", "Watermelon");
System.out.println(list);
assertThat(list).hasSize(expectedSize);
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
Collections.addAll(intList, 10, 20, 30);
System.out.println(intList);
assertThat(intList).hasSize(expectedSize);
}
@Test
public void binarySearch() {
int expectedIndex = 1;
List<String> list = Arrays.asList("Mango", "Orange", "Peach");
int result = Collections.binarySearch(list, "Orange");
System.out.println(result);
assertThat(result).isEqualTo(expectedIndex);
List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
result = Collections.binarySearch(intList, -1);
System.out.println(result);
assertThat(result).isEqualTo(expectedIndex);
}
@Test
public void copy() {
int expectedSize = 3;
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
List<String> list2 = new ArrayList<>(Arrays.asList("Papaya", "Strawberry"));
Collections.copy(list, list2);
System.out.println(list);
assertThat(list).hasSize(expectedSize);
assertThat(list).containsExactly("Papaya", "Strawberry", "Peach");
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
List<Integer> intList2 = new ArrayList<>(Arrays.asList(10, 20));
Collections.copy(intList, intList2);
System.out.println(intList);
assertThat(intList).hasSize(expectedSize);
assertThat(intList).containsExactly(10, 20, 3);
}
@Test
public void disjoint() {
List<String> list = Arrays.asList("Mango", "Orange", "Peach");
List<String> list2 = Arrays.asList("Papaya", "Strawberry");
boolean result = Collections.disjoint(list, list2);
System.out.println(result);
assertThat(result).isTrue();
list2 = Arrays.asList("Mango", "Strawberry");
result = Collections.disjoint(list, list2);
System.out.println(result);
assertThat(result).isFalse();
List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
List<Integer> intList2 = Arrays.asList(10, 20);
result = Collections.disjoint(intList, intList2);
System.out.println(result);
assertThat(result).isTrue();
intList2 = Arrays.asList(Integer.MAX_VALUE, 20);
result = Collections.disjoint(intList, intList2);
System.out.println(result);
assertThat(result).isFalse();
}
@Test
public void shuffle() {
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
assertThat(list).contains("Mango", "Orange", "Peach");
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
Collections.shuffle(intList);
System.out.println(intList);
assertThat(intList).contains(Integer.MAX_VALUE, -1, 3);
}
@Test
public void reverse() {
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
assertThat(list).containsExactly("Peach", "Orange", "Mango");
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
Collections.reverse(intList);
System.out.println(intList);
assertThat(intList).containsExactly(3, -1, Integer.MAX_VALUE);
}
@Test
public void swap() {
List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
System.out.println(list);
Collections.swap(list, 0, 2);
System.out.println(list);
assertThat(list).containsExactly("Peach", "Orange", "Mango");
List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
System.out.println(intList);
Collections.swap(intList, 0, 2);
System.out.println(intList);
assertThat(intList).containsExactly(3, -1, Integer.MAX_VALUE);
}
}
心得分享
Collections Methods Java 使用靜態方法對集合進行操作,當傳遞給方法的集合或物件為 Null 時,此時會拋出 NullPointerException , Collections in Java 提供了幾種 Collections 常見方法的操作範例,實現操作 List 、 Set 更加便利的方法。