@@ -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 *
0 commit comments