generating map from list of objects having a map using java lambda8
up vote
9
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
add a comment |
up vote
9
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
add a comment |
up vote
9
down vote
favorite
up vote
9
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
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
edited Dec 5 at 23:41
Aomine
36.5k72961
36.5k72961
asked Dec 5 at 23:33
sar
492
492
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
6
down vote
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject1List().forEach(o2 -> map.putAll(o2.getCustomData())));
1
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
Dec 6 at 4:33
add a comment |
up vote
4
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
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject1List().forEach(o2 -> map.putAll(o2.getCustomData())));
1
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
Dec 6 at 4:33
add a comment |
up vote
6
down vote
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject1List().forEach(o2 -> map.putAll(o2.getCustomData())));
1
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
Dec 6 at 4:33
add a comment |
up vote
6
down vote
up vote
6
down vote
Here's a way with lambdas and Java 8:
Map<String, String> map = new LinkedHashMap<>();
object1List.forEach(o1 ->
o1.getObject1List().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.getObject1List().forEach(o2 -> map.putAll(o2.getCustomData())));
edited Dec 6 at 9:53
answered Dec 6 at 0:37
Federico Peralta Schaffner
21.8k43369
21.8k43369
1
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
Dec 6 at 4:33
add a comment |
1
weird naming convention in the question, but I guess you meantgetObject1List()
– nullpointer
Dec 6 at 4:33
1
1
weird naming convention in the question, but I guess you meant
getObject1List()– nullpointer
Dec 6 at 4:33
weird naming convention in the question, but I guess you meant
getObject1List()– nullpointer
Dec 6 at 4:33
add a comment |
up vote
4
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
4
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
4
down vote
up vote
4
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 Dec 6 at 4:42
nullpointer
38.7k1074147
38.7k1074147
add a comment |
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 Dec 5 at 23:44
answered Dec 5 at 23:37
Aomine
36.5k72961
36.5k72961
add a comment |
add a comment |
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