Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy java.util.list Collection

Tags:

java

clone

I am implementing a Java class responsible for ordering java.util.List. The problem comes when I use this class. I'm able to ordering the list but I want to copy the "original" list without modification so that I could register every change made on the original list. The sorted list contains object and one of its fields stores a classification id, and this id it is updated with the index value of the list.

I tried to use clone method and it keeps the list unsorted but the changes made on original list are updated in the clone list too.

Is there any way to accomplish it?

My Code:

List<Torero> listaTorero = tbTlgTorerolHome.findByExample(new Torero()); List<Torero> listaToreroTemp = ((List<Torero>) ((ArrayList<Torero>) listaTorero).clone());   Clasificacion clasificacion = new Clasificacion();  Iterator<Torero> iterTorero = clasificacion.getClasificacion(listaTorero, torero).iterator(); //Sorting List 

A Clasificacion method:

public List<Torero> getClasificacion(List<Torero> listaToreroTemp, Torero torero) {      List<Torero> listaTorero = new ArrayList<Torero>();      Collections.sort(listaToreroTemp,new ToreroClasifiacionComparator());      Iterator<Torero> iterTorero = listaToreroTemp.iterator();     int index=1;     while(iterTorero.hasNext())     {         Torero toreroTemp = iterTorero.next();         toreroTemp.setNumClasificacion(index);         listaTorero.add(toreroTemp);         index=index+1;     }     return listaTorero; } 
like image 313
user1260893 Avatar asked May 04 '12 22:05

user1260893


People also ask

How do you duplicate a list in Java?

To clone a list, one can iterate through the original list and use the clone method to copy all the list elements and use the add method to append them to the list. Approach: Create a cloneable class, which has the clone method overridden. Create a list of the class objects from an array using the asList method.

Can you copy an ArrayList in Java?

The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object.

How will you copy collection into another collection?

The copy() method of java. util. Collections class is used to copy all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list.

How do you copy or clone an ArrayList?

Example 1: Make a Copy of ArrayList Here, number. clone() - returns a copy of the object number. (ArrayList<Integer>) - converts value returned by clone() into an arraylist of Integer type (To learn more, visit Java Typecasting)


1 Answers

You may create a new list with an input of a previous list like so:

List one = new ArrayList() //... add data, sort, etc List two = new ArrayList(one); 

This will allow you to modify the order or what elemtents are contained independent of the first list.

Keep in mind that the two lists will contain the same objects though, so if you modify an object in List two, the same object will be modified in list one.

example:

MyObject value1 = one.get(0); MyObject value2 = two.get(0); value1 == value2 //true value1.setName("hello"); value2.getName(); //returns "hello" 

Edit

To avoid this you need a deep copy of each element in the list like so:

List<Torero> one = new ArrayList<Torero>(); //add elements  List<Torero> two = new Arraylist<Torero>(); for(Torero t : one){     Torero copy = deepCopy(t);     two.add(copy); } 

with copy like the following:

public Torero deepCopy(Torero input){     Torero copy = new Torero();     copy.setValue(input.getValue());//.. copy primitives, deep copy objects again      return copy; } 
like image 165
John Ericksen Avatar answered Sep 21 '22 10:09

John Ericksen