Updating an Object based on the Previous one contained in a List, and replacing the Previous one with a New one

Viewed 57

I have a list of objects. Each object has few integer fields (id and some values: value1, value2, value3, value4).

id property is unique. There's only one object having a particular id in the list.

Each time I create a new object which has the same id as an existing object, I want to add each value property of the old object to the corresponding value of the new-one and then delete the old-one from the list.

I've tried to add objects to a Set but even though ids are the same, their values are different, so it didn't work.

My current code:

while (true) {
  String input = scanner.nextLine();
  int id;
  int value1;
  int value2;
  int value3;
  int value4;
  List<SomeObject> list = new ArrayList<>();
  SomeObject record = new SomeObject(id, value1, value2, value3, value4);
  list.add(record);

  for (SomeObject s : list) {
 
     if (s.getId() == id) {
        s.update1(value1);
        s.update2(value2);
        s.update3(value3);
        s.update4(value4);
        break;          
     }       
   }
  list.removeIf(n -> n.getId() == id);
}

SomeObject class:

public class SomeObject {
    private int id;
    private int value1;
    private int value2;
    private int value3;
    private int value4;
    
    // getters, constructor
    
    public int update1(int x) {
        return this.value1 = value1 + x;
    }
    
    public int update2(int x) {
        return this.value2 = value2 + x;
    }
    
    public int update3(int x) {
        return this.value3 = value3 + x;
    }
    
    public int update4(int x) {
        return this.value4 = value4 + x;
    }
}

Input example:

id=1243245 1 1 1 1 // old object

id=1243245 2 2 2 2 // new object

Current output:

id=1243245 2 2 2 2 // <- values of the new object

Desired output:

id=1243245 3 3 3 3 // <- combined 
1 Answers

Assuming that there's at most one instance of SomeObject having the given id in the list, you can iterate until this object has been found (or until the list is fully discovered), and if the object with the target id exists update the new objects using properties of the old-one.

Then remove the old-one break, break the loop, and finally add the new object.

Note: to avoid iterating over the list twice, which would be the case if to remove the old object we would use methods remove(Object) or removeIf(Predicate) (like in the original code), we can use either:

  • traditional index-based for-loop and remove(int), which removes object by index (but still it might result in additional iteration if list implementation would be a LinkedList and not ArrayList);
  • the second option would be to use Iterator and it's method remove(), which removes the element at the current position. For all implementation of the List (that support removal of elements) it would be done in a constant time O(1).

That's how it can be implemented using iterator:

public static void replaceById(List<SomeObject> list,
                               int id, int value1, int value2, int value3, int value4) {
    
    SomeObject record = new SomeObject(id, value1, value2, value3, value4);
    Iterator<SomeObject> iter = list.iterator();
    
    while (iter.hasNext()) {
        SomeObject next = iter.next();

        if (next.getId() == id) {
            record.update1(next.getValue1()); // updating new object
            record.update2(next.getValue2());
            record.update3(next.getValue3());
            record.update4(next.getValue4());

            iter.remove();                    // removing old object
            break;
        }
    }
    
    list.add(record); // adding new object to the list
}

Sidenote: in order to be able to use your objects in hash-based collections, you need the equals/hashCode contract to be properly overridden.

Related