Skip to content

Commit 93c1d3a

Browse files
committed
Switch HashMap to TreeMap
1 parent a733f97 commit 93c1d3a

2 files changed

Lines changed: 58 additions & 21 deletions

File tree

core/src/main/java/fj/data/List.java

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,27 +1238,57 @@ public final A mode(final Ord<A> o) {
12381238
}
12391239

12401240
/**
1241-
* Groups the elements of this list by a given keyFunction into a {@link HashMap}.
1241+
* Groups the elements of this list by a given keyFunction into a {@link TreeMap}.
1242+
* The ordering of the keys is determined by {@link fj.Ord#hashOrd()}.
12421243
*
12431244
* @param keyFunction The function to select the keys for the map.
1244-
* @return A HashMap containing the keys with the accumulated list of matched elements.
1245+
* @return A TreeMap containing the keys with the accumulated list of matched elements.
12451246
*/
1246-
public final <B> HashMap<B, List<A>> groupBy(final F<A, B> keyFunction) {
1247-
return groupBy(keyFunction, Function.identity());
1247+
public final <B> TreeMap<B, List<A>> groupBy(final F<A, B> keyFunction) {
1248+
return groupBy(keyFunction, Ord.hashOrd());
12481249
}
12491250

12501251
/**
1251-
* Groups the elements of this list by a given keyFunction into a {@link HashMap} and transforms
1252-
* the matching elements with the given valueFunction.
1252+
* Groups the elements of this list by a given keyFunction into a {@link TreeMap}.
1253+
*
1254+
* @param keyFunction The function to select the keys for the map.
1255+
* @param keyOrd An order for the keys of the tree map.
1256+
* @return A TreeMap containing the keys with the accumulated list of matched elements.
1257+
*/
1258+
public final <B> TreeMap<B, List<A>> groupBy(final F<A, B> keyFunction, final Ord<B> keyOrd) {
1259+
return groupBy(keyFunction, Function.identity(), keyOrd);
1260+
}
1261+
1262+
/**
1263+
* Groups the elements of this list by a given keyFunction into a {@link TreeMap} and transforms
1264+
* the matching elements with the given valueFunction. The ordering of the keys is determined by
1265+
* {@link fj.Ord#hashOrd()}.
12531266
*
12541267
* @param keyFunction The function to select the keys for the map.
12551268
* @param valueFunction The function to apply on each matching value.
1256-
* @return A HashMap containing the keys with the accumulated list of matched and mapped elements.
1269+
* @return A TreeMap containing the keys with the accumulated list of matched and mapped elements.
12571270
*/
1258-
public final <B, C> HashMap<B, List<C>> groupBy(
1271+
public final <B, C> TreeMap<B, List<C>> groupBy(
12591272
final F<A, B> keyFunction,
12601273
final F<A, C> valueFunction) {
1261-
return this.groupBy(keyFunction, valueFunction, List.<C>nil(), List::cons);
1274+
return this.groupBy(keyFunction, valueFunction, Ord.hashOrd());
1275+
}
1276+
1277+
/**
1278+
* Groups the elements of this list by a given keyFunction into a {@link TreeMap} and transforms
1279+
* the matching elements with the given valueFunction. The ordering of the keys is determined by
1280+
* the keyOrd parameter.
1281+
*
1282+
* @param keyFunction The function to select the keys for the map.
1283+
* @param valueFunction The function to apply on each matching value.
1284+
* @param keyOrd An order for the keys of the tree map.
1285+
* @return A TreeMap containing the keys with the accumulated list of matched and mapped elements.
1286+
*/
1287+
public final <B, C> TreeMap<B, List<C>> groupBy(
1288+
final F<A, B> keyFunction,
1289+
final F<A, C> valueFunction,
1290+
final Ord<B> keyOrd) {
1291+
return this.groupBy(keyFunction, valueFunction, List.<C>nil(), List::cons, keyOrd);
12621292
}
12631293

12641294
/**
@@ -1270,23 +1300,29 @@ public final <B, C> HashMap<B, List<C>> groupBy(
12701300
* @param valueFunction The function to apply on each element.
12711301
* @param groupingIdentity The identity, or start value, for the grouping.
12721302
* @param groupingAcc The accumulator to apply on each matching value.
1273-
* @return A HashMap containing the keys with the accumulated result of matched and mapped
1303+
* @param keyOrd An order for the keys of the tree map.
1304+
* @return A TreeMap containing the keys with the accumulated result of matched and mapped
12741305
* elements.
12751306
*/
1276-
public final <B, C, D> HashMap<B, D> groupBy(
1307+
public final <B, C, D> TreeMap<B, D> groupBy(
12771308
final F<A, B> keyFunction,
1278-
final F<A, C> valueFunction, final D groupingIdentity, final F2<C, D, D> groupingAcc) {
1309+
final F<A, C> valueFunction,
1310+
final D groupingIdentity,
1311+
final F2<C, D, D> groupingAcc,
1312+
final Ord<B> keyOrd) {
12791313
return this.foldLeft(map -> element -> {
12801314
final B key = keyFunction.f(element);
12811315
final C value = valueFunction.f(element);
12821316
map.set(key, map.get(key)
12831317
.map(existing -> groupingAcc.f(value, existing))
12841318
.orSome(groupingAcc.f(value, groupingIdentity)));
12851319
return map;
1286-
}, HashMap.<B, D>hashMap()
1320+
}, TreeMap.<B, D>empty(keyOrd)
12871321
);
12881322
}
12891323

1324+
1325+
12901326
/**
12911327
* Returns whether or not all elements in the list are equal according to the given equality test.
12921328
*

demo/src/main/java/fj/demo/List_groupBy.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package fj.demo;
22

33
import fj.Function;
4-
import fj.data.HashMap;
4+
import fj.Ord;
55
import fj.data.List;
6+
import fj.data.TreeMap;
67

78
import static fj.data.List.list;
89

@@ -17,30 +18,30 @@ public static void main(final String... args) {
1718
private static void keyDemo() {
1819
System.out.println("KeyDemo");
1920
final List<String> words = list("Hello", "World", "how", "are", "your", "doing");
20-
final HashMap<Integer, List<String>> lengthMap = words.groupBy(String::length);
21+
final TreeMap<Integer, List<String>> lengthMap = words.groupBy(String::length);
2122

22-
lengthMap.foreach(entry -> {
23+
lengthMap.forEach(entry -> {
2324
System.out.println(String.format("Words with %d chars: %s", entry._1(), entry._2()));
2425
});
2526
}
2627

2728
private static void keyValueDemo() {
2829
System.out.println("KeyValueDemo");
2930
final List<Integer> xs = list(1, 2, 3, 4, 5, 6, 7, 8, 9);
30-
final HashMap<Integer, List<String>> result = xs.groupBy(x -> x % 3, Integer::toBinaryString);
31+
final TreeMap<Integer, List<String>> result = xs.groupBy(x -> x % 3, Integer::toBinaryString);
3132

32-
result.foreach(entry -> {
33+
result.forEach(entry -> {
3334
System.out.println(String.format("Numbers with reminder %d are %s", entry._1(), entry._2()));
3435
});
3536
}
3637

3738
private static void keyValueAccDemo() {
3839
System.out.println("KeyValueAccDemo");
3940
final List<String> words = list("Hello", "World", "how", "are", "your", "doing");
40-
final HashMap<Integer, Integer> lengthCounts =
41-
words.groupBy(String::length, Function.identity(), 0, (word, sum) -> sum + 1);
41+
final TreeMap<Integer, Integer> lengthCounts =
42+
words.groupBy(String::length, Function.identity(), 0, (word, sum) -> sum + 1, Ord.hashOrd());
4243

43-
lengthCounts.foreach(entry -> {
44+
lengthCounts.forEach(entry -> {
4445
System.out.println(String.format("Words with %d chars: %s", entry._1(), entry._2()));
4546
});
4647
}

0 commit comments

Comments
 (0)