Table of Contents
ToggleJava Collectors groupingby Method
提供了與 SQL 中的 GROUP BY 語句類似的功能,根據一個或多個屬性對集合中的元素進行分組,並將最終結果儲存在 Map 中,除了傳入分類函數,還可以傳入 collector 參數,實現多級分組, GroupingBy Java Collectors 介紹常見的 groupingBy 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
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) {
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- collector
| +- CollectorsGroupingByTest.java
單元測試
Collectors GroupingBy Java 提供對集合中的元素進行分組。
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();
}
}
withMaxBy
Collectors GroupingBy Java 建立一個 List ,增加五個元素,根據元素屬性進行分組,取得最大值。
@Test
public void withMaxBy() {
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Guava", "Kiwifruit");
Map<Integer, Optional<String>> result = list.stream()
.collect(Collectors.groupingBy(String::length, Collectors.maxBy(String::compareTo)));
System.out.println(result);
assertEquals("Fig", result.get(3).get());
assertEquals("Melon", result.get(5).get());
assertEquals("Kiwifruit", result.get(9).get());
List<Fruit> fruitList = 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, Optional<Fruit>> fruitResult = fruitList.stream().collect(
Collectors.groupingBy(Fruit::getType, Collectors.maxBy(Comparator.comparing(Fruit::getQuantity))));
System.out.println(fruitResult);
assertEquals("Blueberry", fruitResult.get(1).get().getName());
assertEquals("Guava", fruitResult.get(2).get().getName());
assertEquals("Kiwifruit", fruitResult.get(3).get().getName());
}
{3=Optional[Fig], 5=Optional[Melon], 9=Optional[Kiwifruit]}
{1=Optional[{"name":"Blueberry","quantity":1.7976931348623157E308,"type":1}], 2=Optional[{"name":"Guava","quantity":4.0,"type":2}], 3=Optional[{"name":"Kiwifruit","quantity":5.0,"type":3}]}
withMinBy
Collectors GroupingBy Java 建立一個 List ,增加五個元素,根據元素屬性進行分組,取得最小值, Java Collectors groupingBy 提供範例參考。
@Test
public void withMinBy() {
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Guava", "Kiwifruit");
Map<Integer, Optional<String>> result = list.stream()
.collect(Collectors.groupingBy(String::length, Collectors.minBy(String::compareTo)));
System.out.println(result);
assertEquals("Fig", result.get(3).get());
assertEquals("Guava", result.get(5).get());
assertEquals("Blueberry", result.get(9).get());
List<Fruit> fruitList = 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, Optional<Fruit>> fruitResult = fruitList.stream().collect(
Collectors.groupingBy(Fruit::getType, Collectors.minBy(Comparator.comparing(Fruit::getQuantity))));
System.out.println(fruitResult);
assertEquals("Fig", fruitResult.get(1).get().getName());
assertEquals("Guava", fruitResult.get(2).get().getName());
assertEquals("Melon", fruitResult.get(3).get().getName());
}
{3=Optional[Fig], 5=Optional[Guava], 9=Optional[Blueberry]}
{1=Optional[{"name":"Fig","quantity":3.0,"type":1}], 2=Optional[{"name":"Guava","quantity":4.0,"type":2}], 3=Optional[{"name":"Melon","quantity":-1.0,"type":3}]}
withMapping
Collectors GroupingBy Java 建立一個 List ,增加五個元素,根據屬性對元素分組,再轉換成元素屬性,傳回 Map , Java Collectors groupingBy 提供範例參考。
@Test
public void withMapping() {
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Guava", "Kiwifruit");
Map<Integer, List<String>> result = list.stream().collect(Collectors.groupingBy(String::length, TreeMap::new,
Collectors.mapping(e -> e.toUpperCase(), Collectors.toList())));
System.out.println(result);
assertEquals(1, result.get(3).size());
assertEquals(2, result.get(5).size());
assertEquals(2, result.get(9).size());
List<Fruit> fruitList = 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>> fruitResult = fruitList.stream().collect(Collectors.groupingBy(Fruit::getType,
TreeMap::new, Collectors.mapping(Fruit::getQuantity, Collectors.toList())));
System.out.println(fruitResult);
assertEquals(3, fruitResult.size());
}
{3=[FIG], 5=[MELON, GUAVA], 9=[BLUEBERRY, KIWIFRUIT]}
{1=[1.7976931348623157E308, 3.0], 2=[4.0], 3=[-1.0, 5.0]}
withJoining
Collectors GroupingBy Example 建立一個 List ,增加五個元素,根據屬性對元素分組,將元素連接成字串, Java Collectors groupingBy 提供範例參考。
@Test
public void withJoining() {
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Guava", "Kiwifruit");
Map<Integer, String> result = list.stream()
.collect(Collectors.groupingBy(String::length, Collectors.joining(", ", "(", ")")));
System.out.println(result);
assertEquals(3, result.size());
List<Fruit> fruitList = 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, String> fruitResult = fruitList.stream().collect(Collectors.groupingBy(Fruit::getType,
Collectors.mapping(Fruit::getName, Collectors.joining(", ", "(", ")"))));
System.out.println(fruitResult);
assertEquals(3, fruitResult.size());
}
{3=(Fig), 5=(Melon, Guava), 9=(Blueberry, Kiwifruit)}
{1=(Blueberry, Fig), 2=(Guava), 3=(Melon, Kiwifruit)}
CollectorsGroupingByTest.java
Collectors GroupingBy Example 新增單元測試,驗證 Java Collectors GroupingBy 是否符合預期。
package org.ruoxue.java_147.collector;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;
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 CollectorsGroupingByTest {
@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 withMaxBy() {
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Guava", "Kiwifruit");
Map<Integer, Optional<String>> result = list.stream()
.collect(Collectors.groupingBy(String::length, Collectors.maxBy(String::compareTo)));
System.out.println(result);
assertEquals("Fig", result.get(3).get());
assertEquals("Melon", result.get(5).get());
assertEquals("Kiwifruit", result.get(9).get());
List<Fruit> fruitList = 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, Optional<Fruit>> fruitResult = fruitList.stream().collect(
Collectors.groupingBy(Fruit::getType, Collectors.maxBy(Comparator.comparing(Fruit::getQuantity))));
System.out.println(fruitResult);
assertEquals("Blueberry", fruitResult.get(1).get().getName());
assertEquals("Guava", fruitResult.get(2).get().getName());
assertEquals("Kiwifruit", fruitResult.get(3).get().getName());
}
@Test
public void withMinBy() {
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Guava", "Kiwifruit");
Map<Integer, Optional<String>> result = list.stream()
.collect(Collectors.groupingBy(String::length, Collectors.minBy(String::compareTo)));
System.out.println(result);
assertEquals("Fig", result.get(3).get());
assertEquals("Guava", result.get(5).get());
assertEquals("Blueberry", result.get(9).get());
List<Fruit> fruitList = 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, Optional<Fruit>> fruitResult = fruitList.stream().collect(
Collectors.groupingBy(Fruit::getType, Collectors.minBy(Comparator.comparing(Fruit::getQuantity))));
System.out.println(fruitResult);
assertEquals("Fig", fruitResult.get(1).get().getName());
assertEquals("Guava", fruitResult.get(2).get().getName());
assertEquals("Melon", fruitResult.get(3).get().getName());
}
@Test
public void withMapping() {
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Guava", "Kiwifruit");
Map<Integer, List<String>> result = list.stream().collect(Collectors.groupingBy(String::length, TreeMap::new,
Collectors.mapping(e -> e.toUpperCase(), Collectors.toList())));
System.out.println(result);
assertEquals(1, result.get(3).size());
assertEquals(2, result.get(5).size());
assertEquals(2, result.get(9).size());
List<Fruit> fruitList = 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>> fruitResult = fruitList.stream().collect(Collectors.groupingBy(Fruit::getType,
TreeMap::new, Collectors.mapping(Fruit::getQuantity, Collectors.toList())));
System.out.println(fruitResult);
assertEquals(3, fruitResult.size());
}
@Test
public void withJoining() {
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Guava", "Kiwifruit");
Map<Integer, String> result = list.stream()
.collect(Collectors.groupingBy(String::length, Collectors.joining(", ", "(", ")")));
System.out.println(result);
assertEquals(3, result.size());
List<Fruit> fruitList = 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, String> fruitResult = fruitList.stream().collect(Collectors.groupingBy(Fruit::getType,
Collectors.mapping(Fruit::getName, Collectors.joining(", ", "(", ")"))));
System.out.println(fruitResult);
assertEquals(3, fruitResult.size());
}
}
心得分享
Java Collectors GroupingBy 在 Collectors 類中定義,屬於 java.util.stream ,按給定的特定屬性對集合中的元素進行分組,熟悉 Collectors GroupingBy Example 這些方法的操作,例如: groupingBy 等方法,提供更清晰、更易讀且更靈活的方式來對集合分組。