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

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?