Collection vs Collections in Java - Java 147

Collection vs Collections in Java – Java 147

Collection vs Collections in Java

接口 Collection 提供 add 、 remove 、 clear 、 size 和 contains 等重要方法,是 Java 集合框架的根接口,將一組單獨的物件表示為一個單元,而工具類別 Collections 提供 addAll 、 sort 、 max 和 min 等重要方法,定義用於操作集合的實用方法,只包含靜態方法, Collections vs Collection in Java 提供這兩種接口及類別的操作使用方式,本篇增加了範例,並透過單元測試來驗證產出結果。

Collection InterfaceCollections Class
宣告為接口實用工具類別
用於將物件分組為單一單元定義用於操作集合的實用方法
從 Java 8 開始包含靜態方法的接口、抽象方法和預設方法只包含靜態方法
繼承 Iterable 接口繼承 Object 物件

檔案目錄

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

單元測試

Java Collection vs Collections 提供集合元素等操作。

Fruit

建立 Fruit 類別,覆寫 toString ,定義屬性和方法,用來建立一個物件。

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	public static class Fruit implements Comparable<Fruit> {

		private String name;
		private double quantity;
		private int type;

		public Fruit(String name, double quantity, int type) {
			this.name = name;
			this.quantity = quantity;
			this.type = type;
		}

		public String toString() {
			ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
			builder.appendSuper(super.toString());
			builder.append("name", name);
			builder.append("quantity", quantity);
			builder.append("type", type);
			return builder.toString();
		}

		@Override
		public int compareTo(Fruit o) {
			int result = this.name.compareTo(o.name);
			if (result == 0)
				result = Double.compare(this.quantity, o.quantity);
			return result;
		}
	}

addAll

Java Collection vs Collections 建立兩個 Collection ,使用給定的 Collection 物件,初始化三個元素。

	@Test
	public void addAll() {
		int expectedSize = 3;
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		Collection<Fruit> collection = Lists.newArrayList(apple, banana, cherry);
		Collection<Fruit> result = new ArrayList<>();
		result.addAll(collection);
		System.out.println(result);
		assertEquals(expectedSize, result.size());

		Fruit mango = new Fruit("Mango", Double.MAX_VALUE, 1);
		Fruit orange = new Fruit("Orange", -1, 3);
		Fruit peach = new Fruit("Peach", 3, 1);
		Collections.addAll(result, mango, orange, peach);
		System.out.println(result);
		assertEquals(6, result.size());
	}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}, {"name":"Mango","quantity":1.7976931348623157E308,"type":1}, {"name":"Orange","quantity":-1.0,"type":3}, {"name":"Peach","quantity":3.0,"type":1}]

sort

Java Collection vs Collections 建立一個 List ,增加三個元素,使用 list sort 及 Collections sort 方法,依照預設比較器,由小到大,升序對集合進行排序。

	@Test
	public void sort() {
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		List<Fruit> list = Lists.newArrayList(banana, apple, cherry);
		System.out.println(list);
		list.sort(null);
		System.out.println(list);
		assertThat(list).containsExactly(apple, banana, cherry);

		Fruit orange = new Fruit("Orange", -1, 3);
		Fruit mango = new Fruit("Mango", Double.MAX_VALUE, 1);
		Fruit peach = new Fruit("Peach", 3, 1);
		list = Lists.newArrayList(orange, mango, peach);
		System.out.println(list);
		Collections.sort(list);
		System.out.println(list);
		assertThat(list).containsExactly(mango, orange, peach);
	}
[{"name":"Banana","quantity":-1.0,"type":3}, {"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Cherry","quantity":3.0,"type":1}]
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
[{"name":"Orange","quantity":-1.0,"type":3}, {"name":"Mango","quantity":1.7976931348623157E308,"type":1}, {"name":"Peach","quantity":3.0,"type":1}]
[{"name":"Mango","quantity":1.7976931348623157E308,"type":1}, {"name":"Orange","quantity":-1.0,"type":3}, {"name":"Peach","quantity":3.0,"type":1}]

min

Java Collection vs Collections 建立一個 Collection ,內有三個元素,取最小元素。

	@Test
	public void min() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		Collection<Fruit> collection = Lists.newArrayList(apple, banana, cherry);
		Fruit result = Collections.min(collection, Comparator.comparing(Fruit::getQuantity));
		System.out.println(result);
		assertThat(result).isEqualTo(banana);
	}
{"name":"Banana","quantity":-1.0,"type":3}

max

Java Collections vs Collection 建立一個 Collection ,內有三個元素,取最大元素。

	@Test
	public void max() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		Collection<Fruit> collection = Lists.newArrayList(apple, banana, cherry);
		Fruit result = Collections.max(collection, Comparator.comparing(Fruit::getQuantity));
		System.out.println(result);
		assertThat(result).isEqualTo(apple);
	}
{"name":"Apple","quantity":1.7976931348623157E308,"type":1}

replaceAll

Java Collections vs Collection 建立一個 Collection ,內有三個元素,取代指定元素, Collection vs Collections 提供範例參考。

	@Test
	public void replaceAll() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		List<Fruit> list = Lists.newArrayList(apple, banana, cherry, apple);
		System.out.println(list);
		Fruit orange = new Fruit("Orange", -1, 3);
		boolean result = Collections.replaceAll(list, apple, orange);
		System.out.println(list);
		System.out.println(result);
		assertThat(result).isTrue();
	}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}, {"name":"Apple","quantity":1.7976931348623157E308,"type":1}]
[{"name":"Orange","quantity":-1.0,"type":3}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}, {"name":"Orange","quantity":-1.0,"type":3}]
true

enumeration

Collection vs Collections 建立一個 Collection ,增加三個元素,使用 iterator 及 enumeration 遍歷元素,輸出在 console 上。

	@Test
	public void enumeration() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		Collection<Fruit> collection = Lists.newArrayList(apple, banana, cherry);
		Iterator<Fruit> it = collection.iterator();
		while (it.hasNext()) {
			Fruit e = it.next();
			System.out.println(e);
		}

		Enumeration<Fruit> en = Collections.enumeration(collection);
		while (en.hasMoreElements()) {
			Fruit e = en.nextElement();
			System.out.println(e);
		}
	}
{"name":"Apple","quantity":1.7976931348623157E308,"type":1}
{"name":"Banana","quantity":-1.0,"type":3}
{"name":"Cherry","quantity":3.0,"type":1}
{"name":"Apple","quantity":1.7976931348623157E308,"type":1}
{"name":"Banana","quantity":-1.0,"type":3}
{"name":"Cherry","quantity":3.0,"type":1}

list

Collection vs Collections 建立一個 Vector ,增加三個元素,使用 list 轉換成 ArrayList 。

	@Test
	public void list() {
		int expectedSize = 3;
		Vector<Fruit> vector = new Vector<>();
		vector.add(new Fruit("Apple", Double.MAX_VALUE, 1));
		vector.add(new Fruit("Banana", -1, 3));
		vector.add(new Fruit("Cherry", 3, 1));
		List<Fruit> result = Collections.list(vector.elements());
		System.out.println(result);
		assertEquals(expectedSize, result.size());
	}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]

CollectionVSCollectionsTest.java

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

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.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;

import com.google.common.collect.Lists;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class CollectionVSCollectionsTest {

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	public static class Fruit implements Comparable<Fruit> {

		private String name;
		private double quantity;
		private int type;

		public Fruit(String name, double quantity, int type) {
			this.name = name;
			this.quantity = quantity;
			this.type = type;
		}

		public String toString() {
			ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
			builder.appendSuper(super.toString());
			builder.append("name", name);
			builder.append("quantity", quantity);
			builder.append("type", type);
			return builder.toString();
		}

		@Override
		public int compareTo(Fruit o) {
			int result = this.name.compareTo(o.name);
			if (result == 0)
				result = Double.compare(this.quantity, o.quantity);
			return result;
		}
	}

	@Test
	public void addAll() {
		int expectedSize = 3;
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		Collection<Fruit> collection = Lists.newArrayList(apple, banana, cherry);
		Collection<Fruit> result = new ArrayList<>();
		result.addAll(collection);
		System.out.println(result);
		assertEquals(expectedSize, result.size());

		Fruit mango = new Fruit("Mango", Double.MAX_VALUE, 1);
		Fruit orange = new Fruit("Orange", -1, 3);
		Fruit peach = new Fruit("Peach", 3, 1);
		Collections.addAll(result, mango, orange, peach);
		System.out.println(result);
		assertEquals(6, result.size());
	}

	@Test
	public void sort() {
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		List<Fruit> list = Lists.newArrayList(banana, apple, cherry);
		System.out.println(list);
		list.sort(null);
		System.out.println(list);
		assertThat(list).containsExactly(apple, banana, cherry);

		Fruit orange = new Fruit("Orange", -1, 3);
		Fruit mango = new Fruit("Mango", Double.MAX_VALUE, 1);
		Fruit peach = new Fruit("Peach", 3, 1);
		list = Lists.newArrayList(orange, mango, peach);
		System.out.println(list);
		Collections.sort(list);
		System.out.println(list);
		assertThat(list).containsExactly(mango, orange, peach);
	}

	@Test
	public void min() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		Collection<Fruit> collection = Lists.newArrayList(apple, banana, cherry);
		Fruit result = Collections.min(collection, Comparator.comparing(Fruit::getQuantity));
		System.out.println(result);
		assertThat(result).isEqualTo(banana);
	}

	@Test
	public void max() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		Collection<Fruit> collection = Lists.newArrayList(apple, banana, cherry);
		Fruit result = Collections.max(collection, Comparator.comparing(Fruit::getQuantity));
		System.out.println(result);
		assertThat(result).isEqualTo(apple);
	}

	@Test
	public void replaceAll() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		List<Fruit> list = Lists.newArrayList(apple, banana, cherry, apple);
		System.out.println(list);
		Fruit orange = new Fruit("Orange", -1, 3);
		boolean result = Collections.replaceAll(list, apple, orange);
		System.out.println(list);
		System.out.println(result);
		assertThat(result).isTrue();
	}

	@Test
	public void enumeration() {
		Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
		Fruit banana = new Fruit("Banana", -1, 3);
		Fruit cherry = new Fruit("Cherry", 3, 1);
		Collection<Fruit> collection = Lists.newArrayList(apple, banana, cherry);
		Iterator<Fruit> it = collection.iterator();
		while (it.hasNext()) {
			Fruit e = it.next();
			System.out.println(e);
		}

		Enumeration<Fruit> en = Collections.enumeration(collection);
		while (en.hasMoreElements()) {
			Fruit e = en.nextElement();
			System.out.println(e);
		}
	}

	@Test
	public void list() {
		int expectedSize = 3;
		Vector<Fruit> vector = new Vector<>();
		vector.add(new Fruit("Apple", Double.MAX_VALUE, 1));
		vector.add(new Fruit("Banana", -1, 3));
		vector.add(new Fruit("Cherry", 3, 1));
		List<Fruit> result = Collections.list(vector.elements());
		System.out.println(result);
		assertEquals(expectedSize, result.size());
	}
}

心得分享

Collection vs Collections 接口與工具類別的區別, Collection 是一個接口, 為 Collection Framework 的根接口,定義了一些公用方法,Collections 是一個工具類別,提供靜態方法操作集合搜尋排序等, Java Collections vs Collection 在應用上相當廣泛,熟悉這些方法的操作,能夠提高開發效率,節省維護上的成本。

發佈留言