Given that we have the following function:
public Map<String, List<String>> mapListIt(List<Map<String, String>> input) {
Map<String, List<String>> results = new HashMap<>();
List<String> things = Arrays.asList("foo", "bar", "baz");
for (String thing : things) {
results.put(thing, input.stream()
.map(element -> element.get("id"))
.collect(Collectors.toList()));
}
return results;
}
Is there some way I could clean this up by binding "id" to a Map::get method reference?
Is there a more stream-y way to write this functionality?
As far as I can tell what you are intending is that this function returns a map from a defined list of strings to a list of all elements with key "id" in a list of input maps. Is that correct?
If so it could be significantly simplified as the value for all keys will be the same:
public Map<String, List<String>> weirdMapFunction(List<Map<String, String>> inputMaps) {
List<String> ids = inputMaps.stream()
.map(m -> m.get("id")).collect(Collectors.toList());
return Stream.of("foo", "bar", "baz")
.collect(Collectors.toMap(Function.identity(), s -> ids));
}
If you wish to use a method reference (which is my interpretation of your question about 'binding') then you will need a separate method to reference:
private String getId(Map<String, String> map) {
return map.get("id");
}
public Map<String, List<String>> weirdMapFunction(List<Map<String, String>> inputMaps) {
List<String> ids = inputMaps.stream()
.map(this::getId).collect(Collectors.toList());
return Stream.of("foo", "bar", "baz")
.collect(Collectors.toMap(Function.identity(), s -> ids));
}
However I'm guessing that you intended to use the items in the list as the keys (rather than "id") in which case:
public Map<String, List<String>> weirdMapFunction(List<Map<String, String>> inputMaps) {
return Stream.of("foo", "bar", "baz")
.collect(Collectors.toMap(Function.identity(), s -> inputMaps.stream()
.map(m -> m.get(s)).collect(Collectors.toList())));
}
Related
I have a class named ConfigKey
public class ConfigKey {
String code;
String key;
String value;
//omit setter and getter
}
I want to convert List<ConfigKey> to Map<String, Map<String, Object>>, here is my method definition
public Map<String, Map<String, Object> convert (List<ConfigKey> list) {
return list.stream().collect(Collectors.groupingBy(ConfigKey::getCode,
Collectors.toMap(ConfigKey::getKey, ConfigKey::getValue)));
}
however I want to do some changes, for each ConfigKey put another key to the map, e.g.
{ "code": "code1","key", "key1", "value": "value1"}
to Map
{"code1": {"key1":"value1", "prefix_key1": "value1" }
is there any API to do it like bellow:
public Map<String, Map<String, Object> convert (List<ConfigKey> list) {
return list.stream().collect(Collectors.groupingBy(ConfigKey::getCode,
Collectors.toMap("prefix_" + ConfigKey::getKey, ConfigKey::getValue))
Collectors.toMap(ConfigKey::getKey, ConfigKey::getValue)));
}
You can make use of the Collector.of() factory method, which allows you to create your own collector:
public Map<String, Map<String, Object> convert (List<ConfigKey> list) {
return list.stream().collect(Collectors.groupingBy(ConfigKey::getCode, Collector.of(
HashMap::new, (m, c) -> {
m.put(c.getKey(), c.getValue());
m.put("prefix_" + c.getKey(), c.getValue());
}, (a, b) -> {
a.putAll(b);
return b;
}
)));
}
But honestly that seems a bit messy, and maybe a normal loop would've been better. The streams intention was to provide an api which does things in a more readable manner, but when you have to hackaround that construct, by introducing some extremely unreadable logic then it is almost always the better option to just do it the old way:
public Map<String, Map<String, Object> convert (List<ConfigKey> list) {
Map<String, Map<String, Object>> map = new HashMap<>();
for (ConfigKey ck : list) {
Map<String, Object> inner = map.computeIfAbsent(ck.getCode(), k -> new HashMap<>());
inner.put(ck.getKey(), ck.getValue());
inner.put("prefix_" + ck.getKey(), ck.getValue());
}
return map;
}
You can first add the new entries to the map and then group them:
private Map<String, Map<String, Object>> convert(List<ConfigKey> list) {
new ArrayList<>(list).stream().map(configKey -> new ConfigKey(configKey.getCode(), "prefix_" + configKey.getKey(), configKey.getValue())).forEachOrdered(list::add);
return list.stream().collect(Collectors.groupingBy(ConfigKey::getCode,
Collectors.toMap(ConfigKey::getKey, ConfigKey::getValue)));
}
I cloned the list (in order to prevent ConcurrentModificationException), then changed the keys to the "new" ones (with map) and added them to the original list - forEachOrdered(list::add).
Because the 'code' field was not changed, both entries will use it which results in 2 entries in the map
I am trying to merge a Stream<Map<String, Map<String, String>>> object into a single map with keys in all the Streams.
For example,
final Map<String, someOtherObjectToProcess> someObject;
final List<Map<String, Map<String, String>>> list = someObject.entrySet()
.stream()
.flatMap(this::getInfoStream)
.collect(Collectors.toList());
The signature for getInfoStream is
public Stream<Map<String, Map<String, String>>> getInfoStream(Map.Entry<String, someOtherObjectToProcess> entry)
if I use (Collectors.toList()) I am able to get a list of these Map objects.
Sample output if I use the above code:
[{
"name" : {
"property":"value"
}
},
{
"name2" : {
"property":"value"
}
}]
But I want to collect into a Map with the structure
{
"name" : {
"property":"value"
},
"name2" : {
"property":"value"
}
}
Provided that the keys will be unique.
How can I do this with Collectors.toMap() or any other alternative way?
When you have
Stream<Map<String, Map<String, String>>> stream = ...
(which I am assuming is result of .flatMap(this::getInfoStream)) you can call
.flatMap(map -> map.entrySet().stream())
to create stream of entries from all maps which will produce Stream<Map.Entry<String, Map<String, String>>>.
Now from that stream all you need to do is collect key and value from each entry into map. Assuming each key will be unique across all maps you could use
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
but if keys are not unique you need to decide what value should be placed in new map for same key. We can do it by filling ... part in
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (vOld, vNew) -> ...));
// ^^^
where vOld holds value currently held in result map under same key, and vNew holds new value (from current stream "iteration").
For instance if you want to ignore new value you can simply return old/currently held by (vOld, vNew) -> vOld
So in short (assuming unique keys):
Map<String, Map<String, String>> combinedMap =
/*your Stream<Map<String, Map<String, String>>>*/
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Another way to solve this would be not using a collector(toList()) but the other overloaded .collect() method with Supplier, Accumulator, and Combiner:
Stream<Map<String, Map<String, String>>> stream = ...
Map<String, Map<String, String>> result = stream
.collect(HashMap::new, HashMap::putAll, HashMap::putAll);
The most readable way in my opinion is to map everything to a Map.Entry and then collect everything back to a Map using Collectors::toMap
import static java.util.stream.Collectors.toMap;
// ...
someObject.entrySet()
.stream()
.flatMap(this::getInfoStream)
.flatMap(map -> map.entrySet().stream())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (one, two) -> one));
(one, two) -> one is the merge function, basically if you have duplicates, you just arbitrarely take the first one to come up
TL;DR:
var merged = Stream.of(map1, map2, ..., mapN).reduce(new HashMap<>(), (a, b) -> {
a.putAll(b);
return a;
});
You can use reduce to combine a stream of Map<String, Map<String, String>> elements into one:
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
alternative1();
alternative2();
}
// Use reduce without an initial identity value
public static void alternative1() {
Map<String, Map<String, String>> m1 = new HashMap<>();
m1.put("name", Map.of("property", "value"));
Map<String, Map<String, String>> m2 = new HashMap<>();
m2.put("name2", Map.of("property", "value"));
Stream<Map<String, Map<String, String>>> mapStream = Stream.of(m1, m2);
Map<String, Map<String, String>> m3 = mapStream.reduce((a, b) -> {
Map<String, Map<String, String>> temp = new HashMap<>();
temp.putAll(a);
temp.putAll(b);
return temp;
}).orElseThrow();
System.out.println(m3);
}
// Use reduce with an initial empty map as the identity value
public static void alternative2() {
Map<String, Map<String, String>> m1 = new HashMap<>();
m1.put("name", Map.of("property", "value"));
Map<String, Map<String, String>> m2 = new HashMap<>();
m2.put("name2", Map.of("property", "value"));
Stream<Map<String, Map<String, String>>> mapStream = Stream.of(m1, m2);
Map<String, Map<String, String>> m3 = mapStream.reduce(new HashMap<>(), (a, b) -> {
a.putAll(b);
return a;
});
System.out.println(m3);
}
}
Output:
{name={property=value}, name2={property=value}}
{name={property=value}, name2={property=value}}
But beware that these solutions assume keys (name and name2) are unique, otherwise duplicate keys would make map entries overwrite each other.
The same logic with a more modern syntax:
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
alternative1();
alternative2();
}
// Use reduce without an initial identity value
public static void alternative1() {
var m1 = Map.of("name", Map.of("property", "value"));
var m2 = Map.of("name2", Map.of("property", "value"));
var m3 = Stream.of(m1, m2).reduce((a, b) -> {
var temp = new HashMap<String, Map<String, String>>();
temp.putAll(a);
temp.putAll(b);
return temp;
}).orElseThrow();
System.out.println(m3);
}
// Use reduce with an initial empty map as the identity value
public static void alternative2() {
var m1 = Map.of("name", Map.of("property", "value"));
var m2 = Map.of("name2", Map.of("property", "value"));
var m3 = Stream.of(m1, m2).reduce(new HashMap<>(), (a, b) -> {
a.putAll(b);
return a;
});
System.out.println(m3);
}
}
I have two List Map:
orders
[
{
item_id=1,
item=item-1,
user_id=1
},
{
item_id=2,
item=item-2,
user_id=2
},
{
item_id=3,
item=item-3,
user_id=3
}
]
users
[
{
user_id=1,
name=abh,
email=abh#bit.com
},
{
user_id=2,
name=pol,
email=pol#bit.com
},
{
user_id=3,
name=tre,
email=tre#bit.com
}
]
They are initialized as
List<Map<String, String>> data
I want to do an sql equivalent inner join on this List Maps using Streams.
I tried this:
List<Map<String, String>> collect = leftData.stream().flatMap(t1 -> rightData.stream())
.filter(t -> t.get(joinColumnTableLeft).equals(t.get(joinColumnTableRight)))
.collect(Collectors.toList());
This gives me a result of size size(users) * size(orders), which is 9.
And the collect has orders.
But I want both the Map to merged into single and then create a list out of it.
Cannot use any library as of now.
Assuming that you don't have duplicate entries (by the merge column key), you can use a method like this to merge.
This creates a map of the mergeColumn key to the full map by row in one of the lists, then uses that for lookup when merging by iterating through the other map.
static List<Map<String, String>> merge(List<Map<String, String>> left,
List<Map<String, String>> right, String joinColumnTableLeft,
String joinColumnTableRight) {
Map<String, Map<String, String>> rightById = right.stream()
.collect(Collectors.toMap(m -> m.get(joinColumnTableRight),
Function.identity()));
return left.stream()
.filter(e -> rightById.containsKey(e.get(joinColumnTableLeft)))
.map(l -> {
Map<String, String> all = new HashMap<>();
all.putAll(l);
all.putAll(rightById.get(l.get(joinColumnTableLeft)));
return all;
})
.collect(Collectors.toList());
}
As a test:
Map<String, String> left1 = new HashMap<>(), right1 = new HashMap<>();
left1.put("a", "A");
left1.put("b", "B");
left1.put("c", "C");
right1.put("a", "A");
right1.put("d", "B");
Map<String, String> left2 = new HashMap<>(), right2 = new HashMap<>();
left2.put("a", "AA");
left2.put("b", "BB");
left2.put("c", "CC");
right2.put("a", "AA");
right2.put("d", "BB");
System.out.println(merge(Arrays.asList(left1, left2),
Arrays.asList(right1, right2), "a", "a"));
The output is: [{a=A, b=B, c=C, d=B}, {a=AA, b=BB, c=CC, d=BB}]
The order of entries isn't important, though. Just note that this assumes that there are no overlapping keys other than the join column. Otherwise, you may want to collect pairs of maps instead of calling putAll on a new map.
The following will support duplicate join keys (and will produce a cartesian product for all entries per key):
static List<Map<String, String>> merge(List<Map<String, String>> left,
List<Map<String, String>> right,
String joinColumnTableLeft, String joinColumnTableRight) {
Map<String, List<Map<String, String>>> rightById = right.stream()
.collect(Collectors.groupingBy(m -> m.get(joinColumnTableRight)));
return left.stream()
.filter(e -> rightById.containsKey(e.get(joinColumnTableLeft)))
.flatMap(l -> rightById.get(l.get(joinColumnTableLeft)).stream()
.map(r -> {
Map<String, String> all = new HashMap<>();
all.putAll(l);
all.putAll(r);
return all;
}
)
).collect(Collectors.toList());
}
Given list of strings like this:
"Y:Yes",
"N:No",
"A:Apple"
I have something like
Map updated = values.stream().map(v -> v.split(":")).collect(Collectors.toMap(v1 -> v1[0],v1->v1.length>1?v1[1]:v1[0]));
But this gives me map as:
{
"Y":"Yes",
"N":"No",
"A":"Apple"
}
How can I get a list of maps as such:
[
{
name:"Y",
display:"Yes"
},
{
name:"N",
display:"No"
},
{
name:"A",
display:"Apple"
}
]
If you are using Java 9, you can use the new immutable map static factory methods, as follows:
List<Map<String, String>> updated = values.stream()
.map(v -> v.split(":"))
.map(a -> Map.of("name", a[0], "display", a[1]))
.collect(Collectors.toList());
As you want to get a List, not a map, your last function call cannot be Collectors.toMap, needs to be Collectors.toList. Now, each invocation to the map method should generate a new Map, so something like this would do:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
Map<String, String> map = new HashMap<>();
map.put("name", parts[0]);
map.put("display", parts[1]);
return map;
)
.collect(Collectors.toList());
Some people would prefer:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
return new HashMap<>() {{
put("name", parts[0]);
put("display", parts[1]);
}};
)
.collect(Collectors.toList());
which creates an extra helper class. Or if you can use Guava:
List updated = values.stream()
.map(v -> {
String[] parts = v.split(":");
return ImmutableMap.of("name", parts[0], "display", parts[1]);
)
.collect(Collectors.toList());
BTW: In the examples I used Listbut the complete type of what you describe would be List<Map<String, String>>.
You can use following if you're still using Java8, if you happen to use Java9 then have a look at Federicos answer:
final List<Map<String,String>> updated = values.stream()
.map(v -> v.split(":"))
.map(arr -> {
Map<String, String> map = new HashMap<>();
map.put("name", arr[0]);
map.put("display", arr[1]);
return map;
})
.collect(Collectors.toList());
I want to convert a Map<String, List<MyObject>> to List<Map<String, MyObject>>
{<key1,[myObject1, myObject2]>, <key2,[myObject3, myObject4]>}
will be converted to
[{<key1,myObject1>, <key2,myObject3>}, {<key1,myObject2>, <key2, myObject4>}]
where myObject1 and myObject3 have a same unique id and so do myObject2 and myObject4.
my implementation is below but is there a more optimal way of doing this.
private List<Map<String, MyObject>> getObjectMapList( Map<String, List<MyObject>> objectMap)
{
List<Map<String, MyObject>> objectMapList = new ArrayList<Map<String,MyObject>>();
for(MyObject myObject : objectMap.get("key1")) {// there will be default key1 whose value is known
Map<String, MyObject> newMap= new HashMap<String, MyObject>();
for (String key : objectMap.keySet()) {
newMap.put(key, objectMap.get(key).stream()
.filter(thisObject -> thisObject.getId().equals(myObject.getId()))
.collect(Collectors.toList()).get(0));
}
objectMapList.add(newMap);
}
return objectMapList;
}
Here's a 1-liner without any curly brackets:
private List<Map<String, MyObject>> getObjectMapList( Map<String, List<MyObject>> objectMap) {
return map.entrySet().stream()
.map(e -> e.getValue().stream()
.map(o -> Collections.singletonMap(e.getKey(), o))
.collect(Collections.toList())
.flatMap(List::stream)
.collect(Collections.toList());
}
The main "trick" here is the use of Collections.singletonMap() to allow a blockless in-line create-and-populate of a map.
Disclaimer: Code may not compile or work as it was thumbed in on my phone (but there's a reasonable chance it will work)
This stream should return you the desired result. With my old Eclipse version, I had some trouble with types. You might have to break it up into single steps, or add some types in the lambdas, but I wanted to keep it short.
Map<String, List<MyObject>> objectMap = new HashMap<>();
objectMap.keySet()
.stream()
.flatMap(key -> objectMap.get(key)
.stream()
.map(obj -> new AbstractMap.SimpleEntry<>(key, obj)))
.collect(groupingBy(pair -> pair.getValue().getId()))
.values()
.stream()
.map(listOfSameIds -> listOfSameIds.stream()
.collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue)))
.collect(toList());
What I do is:
Pair all the objects in all your input's values with their keys and put them in one long list (flatMap(key -> streamOfKeyObjectPairs)).
Separate those pairs by the IDs of the objects (collect(groupingBy)).
Take each of those groups and convert the lists of pairs into maps (map(list -> toMap))
Put all those maps into a list