assign values of object of class A based on values of object of class compacted A in Java
Say I have two classes, CompactedA is A's compact version:
class A {
private String id = null;
private List<B> listB = new ArrayList<B>;
private String name = null;
private String age = null;
}
class CompactedA {
private String id = null;
private List<CompactedB> listCompactedB = new ArrayList<CompactedB>;
}
class B {
private String bId = null;
private String bName = null;
private String bAge = null;
}
class CompactedB {
private String bId = null;
}
now I got an object of CompactedA (called ca
), jsonfied as following:
{
id : '001', // Id
[
{ bId : '001b'},
{ bId : '002b'},
{ bId : '003b'},
] // listCompactedB
}
How Can I new an A object, say a
, based on the values of ca
?
The result of a
should be:
{
id: '001',
[
{ bId: '001b', bName: null, bAge: null},
{ bId: '002b', bName: null, bAge: null},
{ bId: '003b', bName: null, bAge: null},
],
name: null,
age: null
}
java object copy
|
show 2 more comments
Say I have two classes, CompactedA is A's compact version:
class A {
private String id = null;
private List<B> listB = new ArrayList<B>;
private String name = null;
private String age = null;
}
class CompactedA {
private String id = null;
private List<CompactedB> listCompactedB = new ArrayList<CompactedB>;
}
class B {
private String bId = null;
private String bName = null;
private String bAge = null;
}
class CompactedB {
private String bId = null;
}
now I got an object of CompactedA (called ca
), jsonfied as following:
{
id : '001', // Id
[
{ bId : '001b'},
{ bId : '002b'},
{ bId : '003b'},
] // listCompactedB
}
How Can I new an A object, say a
, based on the values of ca
?
The result of a
should be:
{
id: '001',
[
{ bId: '001b', bName: null, bAge: null},
{ bId: '002b', bName: null, bAge: null},
{ bId: '003b', bName: null, bAge: null},
],
name: null,
age: null
}
java object copy
Java doesn't provide a particular way for this. You have to write functions to convert from one class to the other by copying and processing each variable.
– Michael Butscher
Nov 16 '18 at 2:50
@MichaelButscher, I searched and find some third party things offer functions likeCopyProperties()
. compactedA and A have different numbers of variables but variables both in them are using the same names, still no easier way?
– Spike
Nov 16 '18 at 3:00
There may be libraries which provide such functionality but I don't know one.
– Michael Butscher
Nov 16 '18 at 3:08
1
you could use reflection, but writing a converter is much better, as suggested by @MichaelButscher
– Kartik
Nov 16 '18 at 3:08
if you wanted to go fromA
toCompactedA
json instead of the other way round, there is a much better way
– Kartik
Nov 16 '18 at 3:09
|
show 2 more comments
Say I have two classes, CompactedA is A's compact version:
class A {
private String id = null;
private List<B> listB = new ArrayList<B>;
private String name = null;
private String age = null;
}
class CompactedA {
private String id = null;
private List<CompactedB> listCompactedB = new ArrayList<CompactedB>;
}
class B {
private String bId = null;
private String bName = null;
private String bAge = null;
}
class CompactedB {
private String bId = null;
}
now I got an object of CompactedA (called ca
), jsonfied as following:
{
id : '001', // Id
[
{ bId : '001b'},
{ bId : '002b'},
{ bId : '003b'},
] // listCompactedB
}
How Can I new an A object, say a
, based on the values of ca
?
The result of a
should be:
{
id: '001',
[
{ bId: '001b', bName: null, bAge: null},
{ bId: '002b', bName: null, bAge: null},
{ bId: '003b', bName: null, bAge: null},
],
name: null,
age: null
}
java object copy
Say I have two classes, CompactedA is A's compact version:
class A {
private String id = null;
private List<B> listB = new ArrayList<B>;
private String name = null;
private String age = null;
}
class CompactedA {
private String id = null;
private List<CompactedB> listCompactedB = new ArrayList<CompactedB>;
}
class B {
private String bId = null;
private String bName = null;
private String bAge = null;
}
class CompactedB {
private String bId = null;
}
now I got an object of CompactedA (called ca
), jsonfied as following:
{
id : '001', // Id
[
{ bId : '001b'},
{ bId : '002b'},
{ bId : '003b'},
] // listCompactedB
}
How Can I new an A object, say a
, based on the values of ca
?
The result of a
should be:
{
id: '001',
[
{ bId: '001b', bName: null, bAge: null},
{ bId: '002b', bName: null, bAge: null},
{ bId: '003b', bName: null, bAge: null},
],
name: null,
age: null
}
java object copy
java object copy
edited Nov 16 '18 at 2:56
Spike
asked Nov 16 '18 at 2:32
SpikeSpike
11212
11212
Java doesn't provide a particular way for this. You have to write functions to convert from one class to the other by copying and processing each variable.
– Michael Butscher
Nov 16 '18 at 2:50
@MichaelButscher, I searched and find some third party things offer functions likeCopyProperties()
. compactedA and A have different numbers of variables but variables both in them are using the same names, still no easier way?
– Spike
Nov 16 '18 at 3:00
There may be libraries which provide such functionality but I don't know one.
– Michael Butscher
Nov 16 '18 at 3:08
1
you could use reflection, but writing a converter is much better, as suggested by @MichaelButscher
– Kartik
Nov 16 '18 at 3:08
if you wanted to go fromA
toCompactedA
json instead of the other way round, there is a much better way
– Kartik
Nov 16 '18 at 3:09
|
show 2 more comments
Java doesn't provide a particular way for this. You have to write functions to convert from one class to the other by copying and processing each variable.
– Michael Butscher
Nov 16 '18 at 2:50
@MichaelButscher, I searched and find some third party things offer functions likeCopyProperties()
. compactedA and A have different numbers of variables but variables both in them are using the same names, still no easier way?
– Spike
Nov 16 '18 at 3:00
There may be libraries which provide such functionality but I don't know one.
– Michael Butscher
Nov 16 '18 at 3:08
1
you could use reflection, but writing a converter is much better, as suggested by @MichaelButscher
– Kartik
Nov 16 '18 at 3:08
if you wanted to go fromA
toCompactedA
json instead of the other way round, there is a much better way
– Kartik
Nov 16 '18 at 3:09
Java doesn't provide a particular way for this. You have to write functions to convert from one class to the other by copying and processing each variable.
– Michael Butscher
Nov 16 '18 at 2:50
Java doesn't provide a particular way for this. You have to write functions to convert from one class to the other by copying and processing each variable.
– Michael Butscher
Nov 16 '18 at 2:50
@MichaelButscher, I searched and find some third party things offer functions like
CopyProperties()
. compactedA and A have different numbers of variables but variables both in them are using the same names, still no easier way?– Spike
Nov 16 '18 at 3:00
@MichaelButscher, I searched and find some third party things offer functions like
CopyProperties()
. compactedA and A have different numbers of variables but variables both in them are using the same names, still no easier way?– Spike
Nov 16 '18 at 3:00
There may be libraries which provide such functionality but I don't know one.
– Michael Butscher
Nov 16 '18 at 3:08
There may be libraries which provide such functionality but I don't know one.
– Michael Butscher
Nov 16 '18 at 3:08
1
1
you could use reflection, but writing a converter is much better, as suggested by @MichaelButscher
– Kartik
Nov 16 '18 at 3:08
you could use reflection, but writing a converter is much better, as suggested by @MichaelButscher
– Kartik
Nov 16 '18 at 3:08
if you wanted to go from
A
to CompactedA
json instead of the other way round, there is a much better way– Kartik
Nov 16 '18 at 3:09
if you wanted to go from
A
to CompactedA
json instead of the other way round, there is a much better way– Kartik
Nov 16 '18 at 3:09
|
show 2 more comments
0
active
oldest
votes
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%2f53330620%2fassign-values-of-object-of-class-a-based-on-values-of-object-of-class-compacted%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53330620%2fassign-values-of-object-of-class-a-based-on-values-of-object-of-class-compacted%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
Java doesn't provide a particular way for this. You have to write functions to convert from one class to the other by copying and processing each variable.
– Michael Butscher
Nov 16 '18 at 2:50
@MichaelButscher, I searched and find some third party things offer functions like
CopyProperties()
. compactedA and A have different numbers of variables but variables both in them are using the same names, still no easier way?– Spike
Nov 16 '18 at 3:00
There may be libraries which provide such functionality but I don't know one.
– Michael Butscher
Nov 16 '18 at 3:08
1
you could use reflection, but writing a converter is much better, as suggested by @MichaelButscher
– Kartik
Nov 16 '18 at 3:08
if you wanted to go from
A
toCompactedA
json instead of the other way round, there is a much better way– Kartik
Nov 16 '18 at 3:09