Java 8 Stream to convert Object inside Object of Set to MapHow do I efficiently iterate over each entry in a Java Map?How do I read / convert an InputStream into a String in Java?How to convert a Map to List in Java?Converting array to list in JavaHow to convert an Array to a Set in JavaHow do I convert a String to an int in Java?Java 8 List<V> into Map<K, V>How to Convert a Java 8 Stream to an Array?Convert Iterable to Stream using Java 8 JDKWhat's the difference between map and flatMap methods in Java 8?
Why linear regression uses "vertical" distance to the best-fit-line, instead of actual distance?
Too many spies!
Do native speakers use ZVE or CPU?
How are "soeben" and "eben" different from one another?
Meaning of slash chord without anything left of the slash
Doing research in academia and not liking competition
Construct a pentagon avoiding compass use
How did Southern slaveholders in the United States relate to the Caribbean and Latin America?
Why does the trade federation become so alarmed upon learning the ambassadors are Jedi Knights?
Could the crash sites of the Apollo 11 and 16 LMs be seen by the LRO?
Ragged justification of captions depending on odd/even page
Replacing URI when using dynamic hosts in Nginx reverse proxy
Nested-Loop-Join: How many comparisons and how many pages-accesses?
Crab Nebula short story from 1960s or '70s
Should you avoid redundant information after dialogue?
Align by center of symbol
Postgresql numeric and decimal is automatically rounding off
Are villager price increases due to killing them temporary?
Why is "dark" an adverb in this sentence?
3D-Plot with an inequality condition for parameter values
Remove intersect line for one circle using venndiagram2sets
Why use null function instead of == []
Concatenation using + and += operator in Python
Integral clarification
Java 8 Stream to convert Object inside Object of Set to Map
How do I efficiently iterate over each entry in a Java Map?How do I read / convert an InputStream into a String in Java?How to convert a Map to List in Java?Converting array to list in JavaHow to convert an Array to a Set in JavaHow do I convert a String to an int in Java?Java 8 List<V> into Map<K, V>How to Convert a Java 8 Stream to an Array?Convert Iterable to Stream using Java 8 JDKWhat's the difference between map and flatMap methods in Java 8?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have an object which has Set
. Each Set
of object has another object where that also has Set
of object.
class GroupVO
int id;
Set<MasterVO> master; // This should be changed as Map<masterId, MasterVO>
//getter, setter
class MasterVO
int masterId;
Set<SlaveVO> slave; // This should be changed as Map<slaveId, SlaveVO>
//getter, setter
class SlaveVO
int slaveId;
String title;
String description;
//getter, setter
And I want to convert these Set
to Map
.
I'm able to convert it for the first map level. But, I don't know how to get the second 'Set' to Map
conversion.
GroupVO groupVO = new GroupVO();
Map<Integer, MasterVO> masterMap = groupVO.getMaster().stream()
.collect(Collectors.toMap(MasterVO::getMasterId, Function.identity()));
Instead of Function.identity()
, I need to access the mastervo
object and in that Object I have to convert all the Set<Slave>
to SlaveMap
Please suggest.
I need it like,Map<Integre, MasterVO>
. If I get the MasterVO
object like masterMap.get(101)
, then it should have the SlaveMap
inside.
java java-8 java-stream
add a comment |
I have an object which has Set
. Each Set
of object has another object where that also has Set
of object.
class GroupVO
int id;
Set<MasterVO> master; // This should be changed as Map<masterId, MasterVO>
//getter, setter
class MasterVO
int masterId;
Set<SlaveVO> slave; // This should be changed as Map<slaveId, SlaveVO>
//getter, setter
class SlaveVO
int slaveId;
String title;
String description;
//getter, setter
And I want to convert these Set
to Map
.
I'm able to convert it for the first map level. But, I don't know how to get the second 'Set' to Map
conversion.
GroupVO groupVO = new GroupVO();
Map<Integer, MasterVO> masterMap = groupVO.getMaster().stream()
.collect(Collectors.toMap(MasterVO::getMasterId, Function.identity()));
Instead of Function.identity()
, I need to access the mastervo
object and in that Object I have to convert all the Set<Slave>
to SlaveMap
Please suggest.
I need it like,Map<Integre, MasterVO>
. If I get the MasterVO
object like masterMap.get(101)
, then it should have the SlaveMap
inside.
java java-8 java-stream
1
Why can't you do the same as you did for MasterVo here?
– Ravindra Ranwala
Mar 26 at 7:07
add a comment |
I have an object which has Set
. Each Set
of object has another object where that also has Set
of object.
class GroupVO
int id;
Set<MasterVO> master; // This should be changed as Map<masterId, MasterVO>
//getter, setter
class MasterVO
int masterId;
Set<SlaveVO> slave; // This should be changed as Map<slaveId, SlaveVO>
//getter, setter
class SlaveVO
int slaveId;
String title;
String description;
//getter, setter
And I want to convert these Set
to Map
.
I'm able to convert it for the first map level. But, I don't know how to get the second 'Set' to Map
conversion.
GroupVO groupVO = new GroupVO();
Map<Integer, MasterVO> masterMap = groupVO.getMaster().stream()
.collect(Collectors.toMap(MasterVO::getMasterId, Function.identity()));
Instead of Function.identity()
, I need to access the mastervo
object and in that Object I have to convert all the Set<Slave>
to SlaveMap
Please suggest.
I need it like,Map<Integre, MasterVO>
. If I get the MasterVO
object like masterMap.get(101)
, then it should have the SlaveMap
inside.
java java-8 java-stream
I have an object which has Set
. Each Set
of object has another object where that also has Set
of object.
class GroupVO
int id;
Set<MasterVO> master; // This should be changed as Map<masterId, MasterVO>
//getter, setter
class MasterVO
int masterId;
Set<SlaveVO> slave; // This should be changed as Map<slaveId, SlaveVO>
//getter, setter
class SlaveVO
int slaveId;
String title;
String description;
//getter, setter
And I want to convert these Set
to Map
.
I'm able to convert it for the first map level. But, I don't know how to get the second 'Set' to Map
conversion.
GroupVO groupVO = new GroupVO();
Map<Integer, MasterVO> masterMap = groupVO.getMaster().stream()
.collect(Collectors.toMap(MasterVO::getMasterId, Function.identity()));
Instead of Function.identity()
, I need to access the mastervo
object and in that Object I have to convert all the Set<Slave>
to SlaveMap
Please suggest.
I need it like,Map<Integre, MasterVO>
. If I get the MasterVO
object like masterMap.get(101)
, then it should have the SlaveMap
inside.
java java-8 java-stream
java java-8 java-stream
edited Mar 26 at 7:20
Shakthi
asked Mar 26 at 6:39
ShakthiShakthi
2211 gold badge3 silver badges18 bronze badges
2211 gold badge3 silver badges18 bronze badges
1
Why can't you do the same as you did for MasterVo here?
– Ravindra Ranwala
Mar 26 at 7:07
add a comment |
1
Why can't you do the same as you did for MasterVo here?
– Ravindra Ranwala
Mar 26 at 7:07
1
1
Why can't you do the same as you did for MasterVo here?
– Ravindra Ranwala
Mar 26 at 7:07
Why can't you do the same as you did for MasterVo here?
– Ravindra Ranwala
Mar 26 at 7:07
add a comment |
1 Answer
1
active
oldest
votes
If I understand the question correctly, you want a Map<Integer,Map<Integer,SlaveVO>>
, where the key of the outer Map
is a master ID and the key of the inner Map
is a slave ID.
I suggest to create that Map
by using flatMap
to obtain a Stream
of all the MasterVO,SlaveVO pairs. Then you can group the elements of this Stream
by master ID and use Collectors.toMap
to create the inner Map
s.
Map<Integer,Map<Integer,SlaveVO>>
= groupVO .getMaster()
.stream()
.flatMap(m -> m.getSlave()
.stream()
.map(s -> new SimpleEntry<MasterVO,SlaveVO>(m,s)))
.collect(Collectors.groupingBy(e -> e.getKey().getID(),
Collectors.toMap(e -> e.getValue().getID(),
Map.Entry::getValue)));
Hi. I wantMap<Integre, MasterVO>
only. If I get theMasterVO
object likemasterMap.get(101)
, then it should have theSlaveMap
inside.
– Shakthi
Mar 26 at 7:03
@Shakthi But yourMasterVO
class has noMap<Integer,SlaveVO>
property. Do you intend to change that class?
– Eran
Mar 26 at 7:05
Can't we create dynamically in the new Map object which I create is? I don't have Map in the MasterVO also. But, still we are convertingSet
asMap
. Similarly, why can't we create aMap
object in the child and can assign it to theMaster
?
– Shakthi
Mar 26 at 7:10
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',
autoActivateHeartbeat: false,
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%2f55351140%2fjava-8-stream-to-convert-object-inside-object-of-set-to-map%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand the question correctly, you want a Map<Integer,Map<Integer,SlaveVO>>
, where the key of the outer Map
is a master ID and the key of the inner Map
is a slave ID.
I suggest to create that Map
by using flatMap
to obtain a Stream
of all the MasterVO,SlaveVO pairs. Then you can group the elements of this Stream
by master ID and use Collectors.toMap
to create the inner Map
s.
Map<Integer,Map<Integer,SlaveVO>>
= groupVO .getMaster()
.stream()
.flatMap(m -> m.getSlave()
.stream()
.map(s -> new SimpleEntry<MasterVO,SlaveVO>(m,s)))
.collect(Collectors.groupingBy(e -> e.getKey().getID(),
Collectors.toMap(e -> e.getValue().getID(),
Map.Entry::getValue)));
Hi. I wantMap<Integre, MasterVO>
only. If I get theMasterVO
object likemasterMap.get(101)
, then it should have theSlaveMap
inside.
– Shakthi
Mar 26 at 7:03
@Shakthi But yourMasterVO
class has noMap<Integer,SlaveVO>
property. Do you intend to change that class?
– Eran
Mar 26 at 7:05
Can't we create dynamically in the new Map object which I create is? I don't have Map in the MasterVO also. But, still we are convertingSet
asMap
. Similarly, why can't we create aMap
object in the child and can assign it to theMaster
?
– Shakthi
Mar 26 at 7:10
add a comment |
If I understand the question correctly, you want a Map<Integer,Map<Integer,SlaveVO>>
, where the key of the outer Map
is a master ID and the key of the inner Map
is a slave ID.
I suggest to create that Map
by using flatMap
to obtain a Stream
of all the MasterVO,SlaveVO pairs. Then you can group the elements of this Stream
by master ID and use Collectors.toMap
to create the inner Map
s.
Map<Integer,Map<Integer,SlaveVO>>
= groupVO .getMaster()
.stream()
.flatMap(m -> m.getSlave()
.stream()
.map(s -> new SimpleEntry<MasterVO,SlaveVO>(m,s)))
.collect(Collectors.groupingBy(e -> e.getKey().getID(),
Collectors.toMap(e -> e.getValue().getID(),
Map.Entry::getValue)));
Hi. I wantMap<Integre, MasterVO>
only. If I get theMasterVO
object likemasterMap.get(101)
, then it should have theSlaveMap
inside.
– Shakthi
Mar 26 at 7:03
@Shakthi But yourMasterVO
class has noMap<Integer,SlaveVO>
property. Do you intend to change that class?
– Eran
Mar 26 at 7:05
Can't we create dynamically in the new Map object which I create is? I don't have Map in the MasterVO also. But, still we are convertingSet
asMap
. Similarly, why can't we create aMap
object in the child and can assign it to theMaster
?
– Shakthi
Mar 26 at 7:10
add a comment |
If I understand the question correctly, you want a Map<Integer,Map<Integer,SlaveVO>>
, where the key of the outer Map
is a master ID and the key of the inner Map
is a slave ID.
I suggest to create that Map
by using flatMap
to obtain a Stream
of all the MasterVO,SlaveVO pairs. Then you can group the elements of this Stream
by master ID and use Collectors.toMap
to create the inner Map
s.
Map<Integer,Map<Integer,SlaveVO>>
= groupVO .getMaster()
.stream()
.flatMap(m -> m.getSlave()
.stream()
.map(s -> new SimpleEntry<MasterVO,SlaveVO>(m,s)))
.collect(Collectors.groupingBy(e -> e.getKey().getID(),
Collectors.toMap(e -> e.getValue().getID(),
Map.Entry::getValue)));
If I understand the question correctly, you want a Map<Integer,Map<Integer,SlaveVO>>
, where the key of the outer Map
is a master ID and the key of the inner Map
is a slave ID.
I suggest to create that Map
by using flatMap
to obtain a Stream
of all the MasterVO,SlaveVO pairs. Then you can group the elements of this Stream
by master ID and use Collectors.toMap
to create the inner Map
s.
Map<Integer,Map<Integer,SlaveVO>>
= groupVO .getMaster()
.stream()
.flatMap(m -> m.getSlave()
.stream()
.map(s -> new SimpleEntry<MasterVO,SlaveVO>(m,s)))
.collect(Collectors.groupingBy(e -> e.getKey().getID(),
Collectors.toMap(e -> e.getValue().getID(),
Map.Entry::getValue)));
edited Mar 26 at 7:02
answered Mar 26 at 6:56
EranEran
303k39 gold badges516 silver badges590 bronze badges
303k39 gold badges516 silver badges590 bronze badges
Hi. I wantMap<Integre, MasterVO>
only. If I get theMasterVO
object likemasterMap.get(101)
, then it should have theSlaveMap
inside.
– Shakthi
Mar 26 at 7:03
@Shakthi But yourMasterVO
class has noMap<Integer,SlaveVO>
property. Do you intend to change that class?
– Eran
Mar 26 at 7:05
Can't we create dynamically in the new Map object which I create is? I don't have Map in the MasterVO also. But, still we are convertingSet
asMap
. Similarly, why can't we create aMap
object in the child and can assign it to theMaster
?
– Shakthi
Mar 26 at 7:10
add a comment |
Hi. I wantMap<Integre, MasterVO>
only. If I get theMasterVO
object likemasterMap.get(101)
, then it should have theSlaveMap
inside.
– Shakthi
Mar 26 at 7:03
@Shakthi But yourMasterVO
class has noMap<Integer,SlaveVO>
property. Do you intend to change that class?
– Eran
Mar 26 at 7:05
Can't we create dynamically in the new Map object which I create is? I don't have Map in the MasterVO also. But, still we are convertingSet
asMap
. Similarly, why can't we create aMap
object in the child and can assign it to theMaster
?
– Shakthi
Mar 26 at 7:10
Hi. I want
Map<Integre, MasterVO>
only. If I get the MasterVO
object like masterMap.get(101)
, then it should have the SlaveMap
inside.– Shakthi
Mar 26 at 7:03
Hi. I want
Map<Integre, MasterVO>
only. If I get the MasterVO
object like masterMap.get(101)
, then it should have the SlaveMap
inside.– Shakthi
Mar 26 at 7:03
@Shakthi But your
MasterVO
class has no Map<Integer,SlaveVO>
property. Do you intend to change that class?– Eran
Mar 26 at 7:05
@Shakthi But your
MasterVO
class has no Map<Integer,SlaveVO>
property. Do you intend to change that class?– Eran
Mar 26 at 7:05
Can't we create dynamically in the new Map object which I create is? I don't have Map in the MasterVO also. But, still we are converting
Set
as Map
. Similarly, why can't we create a Map
object in the child and can assign it to the Master
?– Shakthi
Mar 26 at 7:10
Can't we create dynamically in the new Map object which I create is? I don't have Map in the MasterVO also. But, still we are converting
Set
as Map
. Similarly, why can't we create a Map
object in the child and can assign it to the Master
?– Shakthi
Mar 26 at 7:10
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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.
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%2f55351140%2fjava-8-stream-to-convert-object-inside-object-of-set-to-map%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
1
Why can't you do the same as you did for MasterVo here?
– Ravindra Ranwala
Mar 26 at 7:07