how to clone information in an ArrayList to another if i don t have only primitives? [duplicate]












0
















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?










share|improve this 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. is Value class immutable?

    – Andreas
    Nov 19 '18 at 22:14


















0
















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?










share|improve this 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. is Value class immutable?

    – Andreas
    Nov 19 '18 at 22:14
















0












0








0









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?










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. 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



















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














1 Answer
1






active

oldest

votes


















0














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());





share|improve this answer
























  • can you please explain me what does the last line do?

    – aki
    Nov 19 '18 at 22:22


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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());





share|improve this answer
























  • can you please explain me what does the last line do?

    – aki
    Nov 19 '18 at 22:22
















0














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());





share|improve this answer
























  • can you please explain me what does the last line do?

    – aki
    Nov 19 '18 at 22:22














0












0








0







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());





share|improve this answer













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());






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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





Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)