how to clone information in an ArrayList to another if i don t have only primitives? [duplicate]
This question already has an answer here:
How to clone ArrayList and also clone its contents?
17 answers
Java: recommended solution for deep cloning/copying an instance
8 answers
I am new to java and I would really appreciate some suggestions.
I have these 2 classes:
public class Value {
public Integer importance;
public Float price;
public String name;
}
public class Attribute {
public String type;
public String name;
public Value value;
}
Now I have this ArrayList
of Attribute
objects:
ArrayList<Attribute> attributes
What I want is to deep copy elements (not only references) in attributes into this other ArrayList
:
ArrayList<Attribute> newAttributes
I saw that this isn't working:
for(Attribute attribute : atributes) {
newAtributes.add(attribute);
}
What should I do?
java
marked as duplicate by Progman, Community♦ Nov 21 '18 at 17:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to clone ArrayList and also clone its contents?
17 answers
Java: recommended solution for deep cloning/copying an instance
8 answers
I am new to java and I would really appreciate some suggestions.
I have these 2 classes:
public class Value {
public Integer importance;
public Float price;
public String name;
}
public class Attribute {
public String type;
public String name;
public Value value;
}
Now I have this ArrayList
of Attribute
objects:
ArrayList<Attribute> attributes
What I want is to deep copy elements (not only references) in attributes into this other ArrayList
:
ArrayList<Attribute> newAttributes
I saw that this isn't working:
for(Attribute attribute : atributes) {
newAtributes.add(attribute);
}
What should I do?
java
marked as duplicate by Progman, Community♦ Nov 21 '18 at 17:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
That depends. E.g. isValue
class immutable?
– Andreas
Nov 19 '18 at 22:14
add a comment |
This question already has an answer here:
How to clone ArrayList and also clone its contents?
17 answers
Java: recommended solution for deep cloning/copying an instance
8 answers
I am new to java and I would really appreciate some suggestions.
I have these 2 classes:
public class Value {
public Integer importance;
public Float price;
public String name;
}
public class Attribute {
public String type;
public String name;
public Value value;
}
Now I have this ArrayList
of Attribute
objects:
ArrayList<Attribute> attributes
What I want is to deep copy elements (not only references) in attributes into this other ArrayList
:
ArrayList<Attribute> newAttributes
I saw that this isn't working:
for(Attribute attribute : atributes) {
newAtributes.add(attribute);
}
What should I do?
java
This question already has an answer here:
How to clone ArrayList and also clone its contents?
17 answers
Java: recommended solution for deep cloning/copying an instance
8 answers
I am new to java and I would really appreciate some suggestions.
I have these 2 classes:
public class Value {
public Integer importance;
public Float price;
public String name;
}
public class Attribute {
public String type;
public String name;
public Value value;
}
Now I have this ArrayList
of Attribute
objects:
ArrayList<Attribute> attributes
What I want is to deep copy elements (not only references) in attributes into this other ArrayList
:
ArrayList<Attribute> newAttributes
I saw that this isn't working:
for(Attribute attribute : atributes) {
newAtributes.add(attribute);
}
What should I do?
This question already has an answer here:
How to clone ArrayList and also clone its contents?
17 answers
Java: recommended solution for deep cloning/copying an instance
8 answers
java
java
edited Nov 19 '18 at 22:10
aki
asked Nov 19 '18 at 22:06
akiaki
12
12
marked as duplicate by Progman, Community♦ Nov 21 '18 at 17:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Progman, Community♦ Nov 21 '18 at 17:28
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
That depends. E.g. isValue
class immutable?
– Andreas
Nov 19 '18 at 22:14
add a comment |
That depends. E.g. isValue
class immutable?
– Andreas
Nov 19 '18 at 22:14
That depends. E.g. is
Value
class immutable?– Andreas
Nov 19 '18 at 22:14
That depends. E.g. is
Value
class immutable?– Andreas
Nov 19 '18 at 22:14
add a comment |
1 Answer
1
active
oldest
votes
First you have create copy of the object using Cloneable
or Copy Constructor
:
public class Value {
public Integer importance;
public Float price;
public String name;
public Value(Value value) {
importance = value.importance;
price = value.price;
name = value.name;
}
}
public class Attribute {
public String type;
public String name;
public Value value;
public Attribute(Attribute attribute) {
type = attribute.type;
name = attribute.name;
value = new Value(attribute.value);
}
}
Now you're ready to create deep array copy:
List<Attribute> attributes = new ArrayList<>();
List<Attribute> newAttributes = attributes.stream().map(attribute -> new Attribute(attribute)).collect(Collector.toList());
can you please explain me what does the last line do?
– aki
Nov 19 '18 at 22:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First you have create copy of the object using Cloneable
or Copy Constructor
:
public class Value {
public Integer importance;
public Float price;
public String name;
public Value(Value value) {
importance = value.importance;
price = value.price;
name = value.name;
}
}
public class Attribute {
public String type;
public String name;
public Value value;
public Attribute(Attribute attribute) {
type = attribute.type;
name = attribute.name;
value = new Value(attribute.value);
}
}
Now you're ready to create deep array copy:
List<Attribute> attributes = new ArrayList<>();
List<Attribute> newAttributes = attributes.stream().map(attribute -> new Attribute(attribute)).collect(Collector.toList());
can you please explain me what does the last line do?
– aki
Nov 19 '18 at 22:22
add a comment |
First you have create copy of the object using Cloneable
or Copy Constructor
:
public class Value {
public Integer importance;
public Float price;
public String name;
public Value(Value value) {
importance = value.importance;
price = value.price;
name = value.name;
}
}
public class Attribute {
public String type;
public String name;
public Value value;
public Attribute(Attribute attribute) {
type = attribute.type;
name = attribute.name;
value = new Value(attribute.value);
}
}
Now you're ready to create deep array copy:
List<Attribute> attributes = new ArrayList<>();
List<Attribute> newAttributes = attributes.stream().map(attribute -> new Attribute(attribute)).collect(Collector.toList());
can you please explain me what does the last line do?
– aki
Nov 19 '18 at 22:22
add a comment |
First you have create copy of the object using Cloneable
or Copy Constructor
:
public class Value {
public Integer importance;
public Float price;
public String name;
public Value(Value value) {
importance = value.importance;
price = value.price;
name = value.name;
}
}
public class Attribute {
public String type;
public String name;
public Value value;
public Attribute(Attribute attribute) {
type = attribute.type;
name = attribute.name;
value = new Value(attribute.value);
}
}
Now you're ready to create deep array copy:
List<Attribute> attributes = new ArrayList<>();
List<Attribute> newAttributes = attributes.stream().map(attribute -> new Attribute(attribute)).collect(Collector.toList());
First you have create copy of the object using Cloneable
or Copy Constructor
:
public class Value {
public Integer importance;
public Float price;
public String name;
public Value(Value value) {
importance = value.importance;
price = value.price;
name = value.name;
}
}
public class Attribute {
public String type;
public String name;
public Value value;
public Attribute(Attribute attribute) {
type = attribute.type;
name = attribute.name;
value = new Value(attribute.value);
}
}
Now you're ready to create deep array copy:
List<Attribute> attributes = new ArrayList<>();
List<Attribute> newAttributes = attributes.stream().map(attribute -> new Attribute(attribute)).collect(Collector.toList());
answered Nov 19 '18 at 22:14
oleg.cherednikoleg.cherednik
6,92921118
6,92921118
can you please explain me what does the last line do?
– aki
Nov 19 '18 at 22:22
add a comment |
can you please explain me what does the last line do?
– aki
Nov 19 '18 at 22:22
can you please explain me what does the last line do?
– aki
Nov 19 '18 at 22:22
can you please explain me what does the last line do?
– aki
Nov 19 '18 at 22:22
add a comment |
That depends. E.g. is
Value
class immutable?– Andreas
Nov 19 '18 at 22:14