Collectors groupingBy vs partitioningBy in Java - Java 147

Collectors groupingBy vs partitioningBy in Java – Java 147

Collectors groupingBy vs partitioningBy in Java

對 groupingBy 而言,是使用 Function 接口接受傳入參數 T ,執行 apply 方法後傳回 R , 而 partitioningBy 使用 Predicate 接口,接受傳入參數 T ,執行 test 方法後傳回 boolean , apply 和 test 方法都是提供 lambda 表達式, Collectors partitioningBy vs groupingBy in Java 本篇增加了範例,並透過單元測試來驗證產出結果。

public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) {
}

public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
		Collector<? super T, A, D> downstream) {
}

public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(
		Function<? super T, ? extends K> classifier, Supplier<M> mapFactory,
		Collector<? super T, A, D> downstream) {
}
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate) {
}

public static <T, D, A> Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate,
		Collector<? super T, A, D> downstream) {
}

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- collectors
       |                   +- CollectorsGroupingByPartitioningByTest.java   

單元測試

Java Collectors groupingBy vs partitioningBy 提供 groupingBy 、 partitioningBy 等操作。

Fruit

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

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	public static class 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();
		}
	}

groupingBy

Java Collectors groupingBy vs partitioningBy 建立一個 List ,增加五個元素,根據元素屬性進行分組。

	@Test
	public void groupingBy() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", Double.MAX_VALUE, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Integer, List<Fruit>> result = list.stream().collect(Collectors.groupingBy(Fruit::getType));
		System.out.println(result);
		assertEquals(3, result.size());
	}
{1=[{"name":"Blueberry","quantity":1.7976931348623157E308,"type":1}, {"name":"Fig","quantity":3.0,"type":1}], 2=[{"name":"Guava","quantity":4.0,"type":2}], 3=[{"name":"Melon","quantity":-1.0,"type":3}, {"name":"Kiwifruit","quantity":5.0,"type":3}]}

partitioningBy

Java Collectors groupingBy vs partitioningBy 建立一個 List ,增加五個元素,根據指定條件進行分區。

	@Test
	public void partitioningBy() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", 1, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Boolean, List<Fruit>> result = list.stream().collect(Collectors.partitioningBy(e -> e.type > 1));
		System.out.println(result);
		assertEquals(2, result.get(Boolean.FALSE).size());
		assertEquals(3, result.get(Boolean.TRUE).size());
	}
{false=[{"name":"Blueberry","quantity":1.0,"type":1}, {"name":"Fig","quantity":3.0,"type":1}], true=[{"name":"Melon","quantity":-1.0,"type":3}, {"name":"Guava","quantity":4.0,"type":2}, {"name":"Kiwifruit","quantity":5.0,"type":3}]}

groupingByWithMapping

Java Collectors partitioningBy vs groupingBy 建立一個 List ,增加五個元素,根據屬性對元素分組,再轉換成元素屬性,傳回 Map。

	@Test
	public void groupingByWithMapping() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", Double.MAX_VALUE, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Integer, Long> result = list.stream().collect(
				Collectors.groupingBy(Fruit::getType, Collectors.mapping(Function.identity(), Collectors.counting())));

		System.out.println(result);
		assertEquals(3, result.size());
	}
{1=2, 2=1, 3=2}

partitioningByWithMapping

Java Collectors groupingBy vs partitioningBy 建立一個 List ,增加五個元素,根據指定條件進行分區,再轉換成元素屬性,傳回 Map 。

	@Test
	public void partitioningByWithMapping() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", Double.MAX_VALUE, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Boolean, List<Double>> result = list.stream().collect(Collectors.partitioningBy(e -> e.type > 1,
				Collectors.mapping(Fruit::getQuantity, Collectors.toList())));
		System.out.println(result);
		assertEquals(2, result.size());
	}
{false=[1.7976931348623157E308, 3.0], true=[-1.0, 4.0, 5.0]}

groupingByWithSupplier

Java Collectors partitioningBy vs groupingBy 建立一個 List ,增加五個元素,根據元素屬性進行分組,轉換成元素屬性。

	@Test
	public void groupingByWithSupplier() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", Double.MAX_VALUE, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Integer, List<Double>> result = list.stream().collect(Collectors.groupingBy(Fruit::getType, LinkedMap::new,
				Collectors.mapping(Fruit::getQuantity, Collectors.toList())));
		System.out.println(result);
		assertEquals(3, result.size());
	}
{1=[1.7976931348623157E308, 3.0], 3=[-1.0, 5.0], 2=[4.0]}

測試結果

方法參數
groupingByFunction
partitioningByPredicate

CollectorsGroupingByPartitioningByTest.java

Java Collectors partitioningBy vs groupingBy 新增單元測試,驗證 Collectors groupingBy vs partitioningBy 是否符合預期。

package org.ruoxue.java_147.collector;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

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

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

public class CollectorsGroupingByPartitioningByTest {

	@NoArgsConstructor
	@Getter
	@Setter
	@Builder
	public static class 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();
		}
	}

	@Test
	public void groupingBy() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", Double.MAX_VALUE, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Integer, List<Fruit>> result = list.stream().collect(Collectors.groupingBy(Fruit::getType));
		System.out.println(result);
		assertEquals(3, result.size());
	}

	@Test
	public void partitioningBy() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", 1, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Boolean, List<Fruit>> result = list.stream().collect(Collectors.partitioningBy(e -> e.type > 1));
		System.out.println(result);
		assertEquals(2, result.get(Boolean.FALSE).size());
		assertEquals(3, result.get(Boolean.TRUE).size());
	}

	@Test
	public void groupingByWithMapping() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", Double.MAX_VALUE, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Integer, Long> result = list.stream().collect(
				Collectors.groupingBy(Fruit::getType, Collectors.mapping(Function.identity(), Collectors.counting())));

		System.out.println(result);
		assertEquals(3, result.size());
	}

	@Test
	public void partitioningByWithMapping() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", Double.MAX_VALUE, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Boolean, List<Double>> result = list.stream().collect(Collectors.partitioningBy(e -> e.type > 1,
				Collectors.mapping(Fruit::getQuantity, Collectors.toList())));
		System.out.println(result);
		assertEquals(2, result.size());
	}

	@Test
	public void groupingByWithSupplier() {
		List<Fruit> list = Arrays.asList(new Fruit("Blueberry", Double.MAX_VALUE, 1), new Fruit("Melon", -1, 3),
				new Fruit("Fig", 3, 1), new Fruit("Guava", 4, 2), new Fruit("Kiwifruit", 5, 3));
		Map<Integer, List<Double>> result = list.stream().collect(Collectors.groupingBy(Fruit::getType, LinkedMap::new,
				Collectors.mapping(Fruit::getQuantity, Collectors.toList())));
		System.out.println(result);
		assertEquals(3, result.size());
	}
}

心得分享

Collectors groupingBy vs partitioningBy 該如何區分 groupingBy 與 partitioningBy 的差異,這兩種方法可能會造成一些混淆, groupingBy 根據指定的屬性對集合中的元素進行分組,採用 Function 接口,而 partitioningBy 則可以按照不同條件,對 Stream 進行分區,傳回 Map ,使用 Predicate 接口, Java Collectors partitioningBy vs groupingBy 提供這兩個方法的區別與測試。

發佈留言