Collections Class in Java with Examples - Java 147

Collections Class in Java with Examples – Java 147

Collections Class in Java with Examples

實用程式套件, 屬於 java.util ,包含集合框架所需的所有類別和接口,提供幾個操作方法,例如: fill 、 replaceAll 、 indexOfSubList 、 nCopies 、 rotate 等範例, Collections Class in Java 支援集合操作,分享了許多靜態方法運用,是集合框架中的工具類之一,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- collection
       |                   +- CollectionsWithExamplesTest.java   

單元測試

Class Collections Java 集合類別工具,提供填充、取代指定元素、變動位置等操作集合中的元素。

fill

建立一個 ArrayList ,內有三個元素,填充元素。

	@Test
	public void fill() {
		List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
		System.out.println(list);
		Collections.fill(list, "Orange");
		System.out.println(list);
		assertThat(list).containsExactly("Orange", "Orange", "Orange");

		List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
		System.out.println(intList);
		Collections.fill(intList, -1);
		System.out.println(intList);
		assertThat(intList).containsExactly(-1, -1, -1);
	}
[Mango, Orange, Peach]
[Orange, Orange, Orange]
[2147483647, -1, 3]
[-1, -1, -1]

replaceAll

Class Collections Java 建立一個 ArrayList ,內有三個元素,取代指定元素。

	@Test
	public void replaceAll() {
		List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
		System.out.println(list);
		Collections.replaceAll(list, "Peach", "Mango");
		System.out.println(list);
		assertThat(list).containsExactly("Mango", "Orange", "Mango");

		List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
		System.out.println(intList);
		Collections.replaceAll(intList, 3, Integer.MAX_VALUE);
		System.out.println(intList);
		assertThat(intList).containsExactly(Integer.MAX_VALUE, -1, Integer.MAX_VALUE);
	}
[Mango, Orange, Peach]
[Mango, Orange, Mango]
[2147483647, -1, 3]
[2147483647, -1, 2147483647]

indexOfSubList

Class Collections Java 建立一個 ArrayList ,內有三個元素,搜索子集合元素,傳回索引。

	@Test
	public void indexOfSubList() {
		int expected = 1;
		List<String> list = Arrays.asList("Mango", "Orange", "Peach");
		List<String> list2 = Arrays.asList("Orange", "Peach");
		int index = Collections.indexOfSubList(list, list2);
		System.out.println(index);
		assertThat(index).isEqualTo(expected);

		List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
		List<Integer> intList2 = Arrays.asList(-1, 3);
		int intIndex = Collections.indexOfSubList(intList, intList2);
		System.out.println(intIndex);
		assertThat(intIndex).isEqualTo(expected);
	}
1
1

lastIndexOfSubList

Class Collections Java 建立一個 ArrayList ,內有三個元素,從最後搜索子集合元素,傳回索引。

	@Test
	public void lastIndexOfSubList() {
		int expected = 0;
		List<String> list = Arrays.asList("Mango", "Orange", "Peach");
		List<String> list2 = Arrays.asList("Mango", "Orange");
		int index = Collections.lastIndexOfSubList(list, list2);
		System.out.println(index);
		assertThat(index).isEqualTo(expected);

		List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
		List<Integer> intList2 = Arrays.asList(Integer.MAX_VALUE, -1);
		int intIndex = Collections.lastIndexOfSubList(intList, intList2);
		System.out.println(intIndex);
		assertThat(intIndex).isEqualTo(expected);
	}
0
0

nCopies

Class Collections Java 建立一個 ArrayList ,內有三個元素,複製為多個集合。

	@Test
	public void nCopies() {
		int expectedSize = 2;
		List<String> list = Arrays.asList("Mango", "Orange", "Peach");
		System.out.println(list);
		List<List<String>> result = Collections.nCopies(2, list);
		System.out.println(result);
		assertThat(result).hasSize(expectedSize);

		List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
		System.out.println(intList);
		List<List<Integer>> intResult = Collections.nCopies(2, intList);
		System.out.println(intResult);
		assertThat(intResult).hasSize(expectedSize);
	}
[Mango, Orange, Peach]
[[Mango, Orange, Peach], [Mango, Orange, Peach]]
[2147483647, -1, 3]
[[2147483647, -1, 3], [2147483647, -1, 3]]

rotate

Collections Class 建立一個 ArrayList ,內有三個元素,變動位置。

	@Test
	public void rotate() {
		List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
		System.out.println(list);
		Collections.rotate(list, 2);
		System.out.println(list);
		assertThat(list).containsExactly("Orange", "Peach", "Mango");

		List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
		System.out.println(intList);
		Collections.rotate(intList, 2);
		System.out.println(intList);
		assertThat(intList).containsExactly(-1, 3, Integer.MAX_VALUE);
	}
[Mango, Orange, Peach]
[Orange, Peach, Mango]
[2147483647, -1, 3]
[-1, 3, 2147483647]

frequency

Collections Class 建立一個 ArrayList ,內有五個元素,計算元素個數。

	@Test
	public void frequency() {
		List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach", "Orange"));
		int result = Collections.frequency(list, "Orange");
		System.out.println(result);
		assertThat(result).isEqualTo(2);

		List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3, -1, 3));
		int intResult = Collections.frequency(intList, 3);
		System.out.println(intResult);
		assertThat(intResult).isEqualTo(2);
	}
2
2

CollectionsWithExamplesTest.java

Class Collections 新增單元測試,驗證是否符合預期。

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 CollectionsWithExamplesTest {

	@Test
	public void fill() {
		List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
		System.out.println(list);
		Collections.fill(list, "Orange");
		System.out.println(list);
		assertThat(list).containsExactly("Orange", "Orange", "Orange");

		List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
		System.out.println(intList);
		Collections.fill(intList, -1);
		System.out.println(intList);
		assertThat(intList).containsExactly(-1, -1, -1);
	}

	@Test
	public void replaceAll() {
		List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
		System.out.println(list);
		Collections.replaceAll(list, "Peach", "Mango");
		System.out.println(list);
		assertThat(list).containsExactly("Mango", "Orange", "Mango");

		List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
		System.out.println(intList);
		Collections.replaceAll(intList, 3, Integer.MAX_VALUE);
		System.out.println(intList);
		assertThat(intList).containsExactly(Integer.MAX_VALUE, -1, Integer.MAX_VALUE);
	}

	@Test
	public void indexOfSubList() {
		int expected = 1;
		List<String> list = Arrays.asList("Mango", "Orange", "Peach");
		List<String> list2 = Arrays.asList("Orange", "Peach");
		int index = Collections.indexOfSubList(list, list2);
		System.out.println(index);
		assertThat(index).isEqualTo(expected);

		List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
		List<Integer> intList2 = Arrays.asList(-1, 3);
		int intIndex = Collections.indexOfSubList(intList, intList2);
		System.out.println(intIndex);
		assertThat(intIndex).isEqualTo(expected);
	}

	@Test
	public void lastIndexOfSubList() {
		int expected = 0;
		List<String> list = Arrays.asList("Mango", "Orange", "Peach");
		List<String> list2 = Arrays.asList("Mango", "Orange");
		int index = Collections.lastIndexOfSubList(list, list2);
		System.out.println(index);
		assertThat(index).isEqualTo(expected);

		List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
		List<Integer> intList2 = Arrays.asList(Integer.MAX_VALUE, -1);
		int intIndex = Collections.lastIndexOfSubList(intList, intList2);
		System.out.println(intIndex);
		assertThat(intIndex).isEqualTo(expected);
	}

	@Test
	public void nCopies() {
		int expectedSize = 2;
		List<String> list = Arrays.asList("Mango", "Orange", "Peach");
		System.out.println(list);
		List<List<String>> result = Collections.nCopies(2, list);
		System.out.println(result);
		assertThat(result).hasSize(expectedSize);

		List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
		System.out.println(intList);
		List<List<Integer>> intResult = Collections.nCopies(2, intList);
		System.out.println(intResult);
		assertThat(intResult).hasSize(expectedSize);
	}

	@Test
	public void rotate() {
		List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach"));
		System.out.println(list);
		Collections.rotate(list, 2);
		System.out.println(list);
		assertThat(list).containsExactly("Orange", "Peach", "Mango");

		List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3));
		System.out.println(intList);
		Collections.rotate(intList, 2);
		System.out.println(intList);
		assertThat(intList).containsExactly(-1, 3, Integer.MAX_VALUE);
	}

	@Test
	public void frequency() {
		List<String> list = new ArrayList<>(Arrays.asList("Mango", "Orange", "Peach", "Orange"));
		int result = Collections.frequency(list, "Orange");
		System.out.println(result);
		assertThat(result).isEqualTo(2);

		List<Integer> intList = new ArrayList<>(Arrays.asList(Integer.MAX_VALUE, -1, 3, -1, 3));
		int intResult = Collections.frequency(intList, 3);
		System.out.println(intResult);
		assertThat(intResult).isEqualTo(2);
	}
}

心得分享

Collections Class 基本上使用靜態方法對集合進行操作,提供了幾種 Collections 常見方法的操作範例,Class Collections 傳遞給方法的集合或物件為 Null 時,此時會拋出 NullPointerException 。

發佈留言