I have two lists containing the same objects. How do I change one list without changing the other?

user101

I first noticed this problem when I only put the objects in listOfRates and then created inverseListOfRates by copying it. But even using this method, I can't alter one list without altering the other.

How do I solve this issue?

List<HistoricRate> listOfRates = new ArrayList<HistoricRate>();
List<HistoricRate> inverseListOfRates = new ArrayList<HistoricRate>();

for (HistoricRate rate : rates){
    listOfRates.add(rate);
    inverseListOfRates.add(rate);
}

inverseListOfRates.forEach(r -> r.setMid(1 / r.getMid()));
Radouane ROUFID

The two lists are referencing the same object. So if you change the first, the second will change also.

The solution is to clone the object (Creating a copy of it into a new instance) before adding it into the second list.

To clone the object you can either use one of the following suggestions:

1- A constructor by copy :

class HistoricRate {
  private String field;

  public HistoricRate (HistoricRate another) {
    this.field= another.field; // you can access  
  }
}

2- HistoricRate must implement Cloneable interface

Implement the method clone to copy the object.

3- Use org.apache.commons.lang.SerializationUtils as below :

for (HistoricRate rate : rates){
    listOfRates.add(rate);
    inverseListOfRates.add(SerializationUtils.clone(rate));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I compare two lists as being equal and containing the same objects, in Dart?

From Dev

Javascript - How can I change one list without modifying the other

From Dev

Using LINQ I have a list of lists, how do I select all objects that exist in every list?

From Dev

How do I bind two objects from two different lists into one ListView controller in Xaml?

From Dev

I'm getting a list of lists that each have one tuple. How do I get one list with several tuples?

From Dev

How do I establish a relationship between two objects where both need to have many of the other object?

From Dev

How do I make two objects of the same class communicate /w each other?

From Dev

How do I scale tkinter canvas window without changing objects

From Dev

How can I check if two javascript objects have the same fields?

From Dev

In a responsive site design, how do i put a list below other list that contains a sub-list without overlap the main lists

From Dev

How can I compare two lists in prolog, returning true if the second list is made of every other element of list one?

From Dev

C# Lists containing lists and how do I check if the list in the list contains a given string?

From Dev

How do I get a component from a ever changing List of objects?

From Dev

How do I change an inputs border colour without changing the style?

From Dev

How do I change the text of a cell without changing the formula?

From Dev

How can I combine two lists into one long list?

From Dev

How do I only change one specific line in a foriegn batch file, without affecting any other line?

From Dev

I have a practice project i did that i need to compare two Lists . How do i make the comparison?

From Dev

How do I merge two List of String into one List without duplicate in Java8 stream

From Dev

How can I make changes to some Copied list that contains reference-type objects without changing the original one

From Dev

How do I concatenate two lists inside nested list Python?

From Dev

How i can have two effects for my data-dismiss="modal" ;one to reload the parent page , while the other without loading

From Dev

In R, how do I test that two functions have the same definition?

From Dev

How do I implement two interfaces that have methods with the same name?

From Dev

In R, how do I test that two functions have the same definition?

From Dev

php mysql If I do not have the same data between two tables, can I print the other table data?

From Dev

How do I determine if two objects are of the same type (typescript)

From Dev

How do I differentiate between two json objects with same key?

From Java

r If I have two seperate groups that have the same contents, how can I delete one of them?

Related Related

  1. 1

    How do I compare two lists as being equal and containing the same objects, in Dart?

  2. 2

    Javascript - How can I change one list without modifying the other

  3. 3

    Using LINQ I have a list of lists, how do I select all objects that exist in every list?

  4. 4

    How do I bind two objects from two different lists into one ListView controller in Xaml?

  5. 5

    I'm getting a list of lists that each have one tuple. How do I get one list with several tuples?

  6. 6

    How do I establish a relationship between two objects where both need to have many of the other object?

  7. 7

    How do I make two objects of the same class communicate /w each other?

  8. 8

    How do I scale tkinter canvas window without changing objects

  9. 9

    How can I check if two javascript objects have the same fields?

  10. 10

    In a responsive site design, how do i put a list below other list that contains a sub-list without overlap the main lists

  11. 11

    How can I compare two lists in prolog, returning true if the second list is made of every other element of list one?

  12. 12

    C# Lists containing lists and how do I check if the list in the list contains a given string?

  13. 13

    How do I get a component from a ever changing List of objects?

  14. 14

    How do I change an inputs border colour without changing the style?

  15. 15

    How do I change the text of a cell without changing the formula?

  16. 16

    How can I combine two lists into one long list?

  17. 17

    How do I only change one specific line in a foriegn batch file, without affecting any other line?

  18. 18

    I have a practice project i did that i need to compare two Lists . How do i make the comparison?

  19. 19

    How do I merge two List of String into one List without duplicate in Java8 stream

  20. 20

    How can I make changes to some Copied list that contains reference-type objects without changing the original one

  21. 21

    How do I concatenate two lists inside nested list Python?

  22. 22

    How i can have two effects for my data-dismiss="modal" ;one to reload the parent page , while the other without loading

  23. 23

    In R, how do I test that two functions have the same definition?

  24. 24

    How do I implement two interfaces that have methods with the same name?

  25. 25

    In R, how do I test that two functions have the same definition?

  26. 26

    php mysql If I do not have the same data between two tables, can I print the other table data?

  27. 27

    How do I determine if two objects are of the same type (typescript)

  28. 28

    How do I differentiate between two json objects with same key?

  29. 29

    r If I have two seperate groups that have the same contents, how can I delete one of them?

HotTag

Archive