generating map from list of objects having a map using java lambda8
up vote
8
down vote
favorite
I've an object,
class Object2{
String name;
String id;
Map<String, String> customData;
}
class Object1{
List<Object2> obj1List;
}
I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.
java lambda java-8 java-stream
New contributor
add a comment |
up vote
8
down vote
favorite
I've an object,
class Object2{
String name;
String id;
Map<String, String> customData;
}
class Object1{
List<Object2> obj1List;
}
I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.
java lambda java-8 java-stream
New contributor
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I've an object,
class Object2{
String name;
String id;
Map<String, String> customData;
}
class Object1{
List<Object2> obj1List;
}
I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.
java lambda java-8 java-stream
New contributor
I've an object,
class Object2{
String name;
String id;
Map<String, String> customData;
}
class Object1{
List<Object2> obj1List;
}
I want to convert this customData Map in the List of object1 into one single map, I am ok with over-writing the values if the key already exists.
java lambda java-8 java-stream
java lambda java-8 java-stream
New contributor
New contributor
edited 6 hours ago
Aomine
34.1k62859
34.1k62859
New contributor
asked 6 hours ago
sar
411
411
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
5
down vote
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
1 hour ago
add a comment |
up vote
3
down vote
Use flatMap
and toMap
as follows:
List<Object1> source = ...
Map<String, String> result =
source.stream()
.flatMap(e -> e.getObj1List().stream()
.flatMap(a -> a.getCustomData().entrySet().stream()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
or if you're dealing with a single Object1
object:
Object1 myObj = ...
Map<String, String> result =
myObj.getObj1List()
.stream()
.flatMap(a -> a.getCustomData().entrySet().stream())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
add a comment |
up vote
3
down vote
Alternatively, you can perform Stream.flatMap
and then use Map.putAll
as
List<Object1> object1s = new ArrayList<>(); // initialise as you would
Map<String, String> finalCustomData = new LinkedHashMap<>();
object1s.stream() // Stream<Object1>
.flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
.map(Object2::getCustomData) // Stream<Map<String, String>>
.forEach(finalCustomData::putAll);
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
1 hour ago
add a comment |
up vote
5
down vote
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
1 hour ago
add a comment |
up vote
5
down vote
up vote
5
down vote
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject2List().forEach(o2 -> map.putAll(o2.getCustomData())));
answered 5 hours ago
Federico Peralta Schaffner
21.7k33369
21.7k33369
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
1 hour ago
add a comment |
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
1 hour ago
weird naming convention in the question, but I guess you meant
getObject1List()
– nullpointer
1 hour ago
weird naming convention in the question, but I guess you meant
getObject1List()
– nullpointer
1 hour ago
add a comment |
up vote
3
down vote
Use flatMap
and toMap
as follows:
List<Object1> source = ...
Map<String, String> result =
source.stream()
.flatMap(e -> e.getObj1List().stream()
.flatMap(a -> a.getCustomData().entrySet().stream()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
or if you're dealing with a single Object1
object:
Object1 myObj = ...
Map<String, String> result =
myObj.getObj1List()
.stream()
.flatMap(a -> a.getCustomData().entrySet().stream())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
add a comment |
up vote
3
down vote
Use flatMap
and toMap
as follows:
List<Object1> source = ...
Map<String, String> result =
source.stream()
.flatMap(e -> e.getObj1List().stream()
.flatMap(a -> a.getCustomData().entrySet().stream()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
or if you're dealing with a single Object1
object:
Object1 myObj = ...
Map<String, String> result =
myObj.getObj1List()
.stream()
.flatMap(a -> a.getCustomData().entrySet().stream())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
add a comment |
up vote
3
down vote
up vote
3
down vote
Use flatMap
and toMap
as follows:
List<Object1> source = ...
Map<String, String> result =
source.stream()
.flatMap(e -> e.getObj1List().stream()
.flatMap(a -> a.getCustomData().entrySet().stream()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
or if you're dealing with a single Object1
object:
Object1 myObj = ...
Map<String, String> result =
myObj.getObj1List()
.stream()
.flatMap(a -> a.getCustomData().entrySet().stream())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
Use flatMap
and toMap
as follows:
List<Object1> source = ...
Map<String, String> result =
source.stream()
.flatMap(e -> e.getObj1List().stream()
.flatMap(a -> a.getCustomData().entrySet().stream()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
or if you're dealing with a single Object1
object:
Object1 myObj = ...
Map<String, String> result =
myObj.getObj1List()
.stream()
.flatMap(a -> a.getCustomData().entrySet().stream())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (l, r) -> r));
edited 6 hours ago
answered 6 hours ago
Aomine
34.1k62859
34.1k62859
add a comment |
add a comment |
up vote
3
down vote
Alternatively, you can perform Stream.flatMap
and then use Map.putAll
as
List<Object1> object1s = new ArrayList<>(); // initialise as you would
Map<String, String> finalCustomData = new LinkedHashMap<>();
object1s.stream() // Stream<Object1>
.flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
.map(Object2::getCustomData) // Stream<Map<String, String>>
.forEach(finalCustomData::putAll);
add a comment |
up vote
3
down vote
Alternatively, you can perform Stream.flatMap
and then use Map.putAll
as
List<Object1> object1s = new ArrayList<>(); // initialise as you would
Map<String, String> finalCustomData = new LinkedHashMap<>();
object1s.stream() // Stream<Object1>
.flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
.map(Object2::getCustomData) // Stream<Map<String, String>>
.forEach(finalCustomData::putAll);
add a comment |
up vote
3
down vote
up vote
3
down vote
Alternatively, you can perform Stream.flatMap
and then use Map.putAll
as
List<Object1> object1s = new ArrayList<>(); // initialise as you would
Map<String, String> finalCustomData = new LinkedHashMap<>();
object1s.stream() // Stream<Object1>
.flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
.map(Object2::getCustomData) // Stream<Map<String, String>>
.forEach(finalCustomData::putAll);
Alternatively, you can perform Stream.flatMap
and then use Map.putAll
as
List<Object1> object1s = new ArrayList<>(); // initialise as you would
Map<String, String> finalCustomData = new LinkedHashMap<>();
object1s.stream() // Stream<Object1>
.flatMap(o1 -> o1.getObj1List().stream()) // Stream<Object2>
.map(Object2::getCustomData) // Stream<Map<String, String>>
.forEach(finalCustomData::putAll);
answered 1 hour ago
nullpointer
37.4k1072145
37.4k1072145
add a comment |
add a comment |
sar is a new contributor. Be nice, and check out our Code of Conduct.
sar is a new contributor. Be nice, and check out our Code of Conduct.
sar is a new contributor. Be nice, and check out our Code of Conduct.
sar is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53642421%2fgenerating-map-from-list-of-objects-having-a-map-using-java-lambda8%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown