Skip to content

Commit

Permalink
Added U.associateBy(iterable, func)
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Sep 15, 2023
1 parent 45a74b4 commit 21bb5a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/github/underscore/U.java
Expand Up @@ -367,6 +367,11 @@ public <F> Chain<Map<F, List<T>>> groupBy(final Function<T, F> func) {
return new Chain<>(Underscore.groupBy(value(), func));
}

@Override
public <F> Chain<Map<F, T>> associateBy(final Function<T, F> func) {
return new Chain<>(Underscore.associateBy(value(), func));
}

@Override
public <F> Chain<Map<F, Optional<T>>> groupBy(
final Function<T, F> func, final BinaryOperator<T> binaryOperator) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/github/underscore/Underscore.java
Expand Up @@ -1046,6 +1046,21 @@ public <K, E> Map<K, Optional<E>> groupBy(
return groupBy((Iterable<E>) iterable, func, binaryOperator);
}

public static <K, E> Map<K, E> associateBy(
final Iterable<E> iterable, final Function<E, K> func) {
final Map<K, E> retVal = newLinkedHashMap();
for (E e : iterable) {
final K key = func.apply(e);
retVal.putIfAbsent(key, e);
}
return retVal;
}

@SuppressWarnings("unchecked")
public <K, E> Map<K, E> associateBy(final Function<E, K> func) {
return associateBy((Iterable<E>) iterable, func);
}

@SuppressWarnings("unchecked")
public static <K, E> Map<K, List<E>> indexBy(
final Iterable<E> iterable, final String property) {
Expand Down Expand Up @@ -3000,6 +3015,10 @@ public <F> Chain<Map<F, List<T>>> groupBy(final Function<T, F> func) {
return new Chain<>(Underscore.groupBy(list, func));
}

public <F> Chain<Map<F, T>> associateBy(final Function<T, F> func) {
return new Chain<>(Underscore.associateBy(list, func));
}

public <F> Chain<Map<F, Optional<T>>> groupBy(
final Function<T, F> func, final BinaryOperator<T> binaryOperator) {
return new Chain<>(Underscore.groupBy(list, func, binaryOperator));
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/github/underscore/CollectionsTest.java
Expand Up @@ -1436,6 +1436,26 @@ void groupBy() {
assertEquals("{1.0=[1.3], 2.0=[2.1, 2.4]}", resultChain.toString());
}

/*
_.associateBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
=> {1: [1.3], 2: [2.1, 2.4]}
*/
@Test
@SuppressWarnings("unchecked")
void associateBy() {
final Map<Double, Double> result =
Underscore.associateBy(asList(1.3, 2.1, 2.4), Math::floor);
assertEquals("{1.0=1.3, 2.0=2.1}", result.toString());
final Map<Double, Double> resultObj =
new Underscore(asList(1.3, 2.1, 2.4))
.associateBy((Function<Double, Double>) Math::floor);
assertEquals("{1.0=1.3, 2.0=2.1}", resultObj.toString());
final Map<Double, Double> resultChain =
(Map<Double, Double>)
Underscore.chain(asList(1.3, 2.1, 2.4)).associateBy(Math::floor).item();
assertEquals("{1.0=1.3, 2.0=2.1}", resultChain.toString());
}

/*
_.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
=> {1: [1.3], 2: [2.1, 2.4]}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/github/underscore/LodashTest.java
Expand Up @@ -1780,6 +1780,7 @@ void chain() {
U.chain(new Integer[] {0}).sortBy(value -> value);
U.chain(new LinkedHashMap<Integer, Integer>().entrySet()).sortBy("");
U.chain(new Integer[] {0}).groupBy(value -> value);
U.chain(new Integer[] {0}).associateBy(value -> value);
U.chain(new Integer[] {0}).groupBy(num -> num, (a, b) -> a);
U.chain(new Integer[] {0}).indexBy("");
U.chain(new Integer[] {0}).countBy(value -> value);
Expand Down

0 comments on commit 21bb5a0

Please sign in to comment.