Skip to content

Commit f24e210

Browse files
committed
Update Iterables.
1 parent f2b797a commit f24e210

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,8 @@ public static <T> Iterable<T> filter(Iterable<T> iterable, Predicate<? super T>
11411141
public static <T, R> Iterable<R> mapAll(Iterable<T> iterable, Function<? super T, ? extends R> transform) { ... }
11421142
public static <T> T firstOf(Iterable<T> iterable) { ... }
11431143
public static boolean isEmpty(Iterable<?> iterable) { ... }
1144+
1145+
public static <K, T> Function<Iterable<T>, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) { ... }
11441146
```
11451147

11461148
These are provided as a less complex alternative to similar methods defined by the `java.util.stream.Stream` class:
@@ -1169,6 +1171,12 @@ var values = listOf();
11691171
var result = isEmpty(values); // true
11701172
```
11711173

1174+
```java
1175+
var values = listOf("a", "b", "ab", "bc", "abc");
1176+
1177+
var result = map(values, groupingBy(String::length)); // 1: a, b; 2: ab, bc; 3: abc
1178+
```
1179+
11721180
`Iterables` also provides the following method, which coerces a value to a given type:
11731181

11741182
```java

kilo-client/src/main/java/org/httprpc/kilo/util/Iterables.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
import org.httprpc.kilo.beans.BeanAdapter;
1818

19+
import java.util.ArrayList;
1920
import java.util.Iterator;
21+
import java.util.LinkedHashMap;
22+
import java.util.List;
23+
import java.util.Map;
2024
import java.util.NoSuchElementException;
2125
import java.util.function.Function;
2226
import java.util.function.Predicate;
@@ -181,6 +185,37 @@ public static boolean isEmpty(Iterable<?> iterable) {
181185
return !iterable.iterator().hasNext();
182186
}
183187

188+
/**
189+
* Returns a function that groups elements by a classification function.
190+
*
191+
* @param <K>
192+
* The key type.
193+
*
194+
* @param <T>
195+
* The element type.
196+
*
197+
* @param classifier
198+
* The classification function.
199+
*
200+
* @return
201+
* The grouping function.
202+
*/
203+
public static <K, T> Function<Iterable<T>, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) {
204+
if (classifier == null) {
205+
throw new IllegalArgumentException();
206+
}
207+
208+
return iterable -> {
209+
var map = new LinkedHashMap<K, List<T>>();
210+
211+
for (var element : iterable) {
212+
map.computeIfAbsent(classifier.apply(element), key -> new ArrayList<>()).add(element);
213+
}
214+
215+
return map;
216+
};
217+
}
218+
184219
/**
185220
* Returns a function that coerces a value to a given type.
186221
*

kilo-client/src/test/java/org/httprpc/kilo/util/IterablesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.httprpc.kilo.util.Collections.*;
2020
import static org.httprpc.kilo.util.Iterables.*;
21+
import static org.httprpc.kilo.util.Optionals.*;
2122
import static org.junit.jupiter.api.Assertions.*;
2223

2324
public class IterablesTest {
@@ -64,6 +65,19 @@ public void testIsEmpty() {
6465
assertFalse(isEmpty(listOf(1, 2, 3)));
6566
}
6667

68+
@Test
69+
public void testGroupingBy() {
70+
var values = listOf("a", "b", "ab", "bc", "abc");
71+
72+
var result = map(values, groupingBy(String::length)); // 1: a, b; 2: ab, bc; 3: abc
73+
74+
assertEquals(mapOf(
75+
entry(1, listOf("a", "b")),
76+
entry(2, listOf("ab", "bc")),
77+
entry(3, listOf("abc"))
78+
), result);
79+
}
80+
6781
@Test
6882
public void testToType() {
6983
var strings = listOf("1", "2", "3");

0 commit comments

Comments
 (0)