Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Streams;

/**
Expand Down Expand Up @@ -63,6 +64,20 @@ public static <K, V> MapStream<K, V> of(Map<K, V> map) {
return new MapStream<>(map.entrySet().stream());
}

/**
* Returns a stream over all the entries in the multimap.
* <p>
* This will typically create a stream with duplicate keys.
*
* @param <K> the key type
* @param <V> the value type
* @param multimap the multimap to wrap
* @return a stream over the entries in the multimap
*/
public static <K, V> MapStream<K, V> of(Multimap<K, V> multimap) {
return new MapStream<>(multimap.entries().stream());
}

/**
* Returns a stream of map entries where the values are taken from a collection
* and the keys are created by applying a function to each value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ListMultimap;
import com.opengamma.strata.collect.tuple.Pair;

Expand All @@ -36,6 +37,7 @@ public class MapStreamTest {

private final Map<String, Integer> map = ImmutableMap.of("one", 1, "two", 2, "three", 3, "four", 4);

//-------------------------------------------------------------------------
public void keys() {
List<String> result = MapStream.of(map).keys().collect(toImmutableList());
assertThat(result).isEqualTo(ImmutableList.of("one", "two", "three", "four"));
Expand Down Expand Up @@ -279,6 +281,13 @@ public void forEach() {
assertThat(mutableMap).isEqualTo(map);
}

//-------------------------------------------------------------------------
public void ofMultimap() {
ImmutableMultimap<String, Integer> input = ImmutableMultimap.of("one", 1, "two", 2, "one", 3);
assertThat(MapStream.of(input)).containsExactlyInAnyOrder(entry("one", 1), entry("two", 2), entry("one", 3));
assertThat(MapStream.of(input).toMap(Integer::sum)).containsOnly(entry("one", 4), entry("two", 2));
}

public void ofCollection() {
List<String> letters = ImmutableList.of("a", "b", "c");
Map<String, String> expected = ImmutableMap.of("A", "a", "B", "b", "C", "c");
Expand Down