Skip to content

Commit

Permalink
Add toImmutableMap to RichIterable.
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Raab <Donald.Raab@bnymellon.com>
  • Loading branch information
donraab committed May 11, 2021
1 parent 43b9c67 commit 7572d68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.eclipse.collections.api.factory.SortedSets;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.MutableMapIterable;
Expand Down Expand Up @@ -1875,6 +1876,18 @@ default <V extends Comparable<? super V>> ImmutableSortedBag<T> toImmutableSorte
return this.toImmutableSortedBag(Comparator.comparing(function));
}

/**
* Converts the collection to an ImmutableMap implementation using the specified key and value functions.
*
* @since 11.0
*/
default <NK, NV> ImmutableMap<NK, NV> toImmutableMap(
Function<? super T, ? extends NK> keyFunction,
Function<? super T, ? extends NV> valueFunction)
{
return this.<NK, NV>toMap(keyFunction, valueFunction).toImmutable();
}

/**
* Returns a lazy (deferred) iterable, most likely implemented by calling LazyIterate.adapt(this).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.eclipse.collections.api.factory.SortedBags;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
Expand Down Expand Up @@ -1715,6 +1716,19 @@ public void toMap()
Assert.assertEquals(UnifiedMap.newWithKeysValues("1", "1", "2", "2", "3", "3", "4", "4"), map);
}

@Test
public void toImmutableMap()
{
RichIterable<Integer> integers = this.newWith(1, 2, 3, 4);
ImmutableMap<String, String> map =
integers.toImmutableMap(Object::toString, Object::toString);
Assert.assertEquals(UnifiedMap.newWithKeysValues("1", "1", "2", "2", "3", "3", "4", "4"), map);
RichIterable<Integer> empty = this.newWith();
ImmutableMap<String, String> emptyMap =
empty.toImmutableMap(Object::toString, Object::toString);
Assert.assertSame(Maps.immutable.empty(), emptyMap);
}

@Test
public void toMapTarget()
{
Expand Down

0 comments on commit 7572d68

Please sign in to comment.