I would like to know if it's possible to directly update an object in List by its id. Here is my object structure :
public class MyObject {
public UUID uuid;
public String string1;
public int int1;
...
}
In another Object I've an List of MyObject and I would like to update one of them by object id. Is there any other solution besides the following code ?
List<MyObject> list = new ArrayList<>();
// Add many object into my list
MyObject newObject = new MyObject(...);
list.removeIf(o -> o.getUuid().equals(newObject.getUuid()));
list.add(newObject);
CodePudding user response:
You could iterate over the list and manually check the UUID each time if you really want to stick with using a List
.
List<MyObject> list = new ArrayList<>();
for (int i = 0; i < list.size(), i ) {
if (list.get(i).getUuid().equals(newObject.getUuid())) {
list.set(i, newObject());
// Probably want to stop the loop here.
}
}
Using UUID
as a key for a Map
makes it much simpler.
List<MyObject> list = new ArrayList<>();
Map<UUID, MyObject> map = list.stream().collect(Collecotrs.toMap(o -> o.getUuid(), o -> o));
map.put(newObject.getUuid(), newObject);
list = new ArrayList<>(map.values());
CodePudding user response:
compact, but hacky...
List<MyObject> list2 =
list.stream().map(item->item.getUuid().equals(newObject.getUuid()) ? newObject : item)
.collect(Collectors.toList();
CodePudding user response:
I think that the easiest way is to implement hashcode/equals in such a way that it will target the UUID. Objects that match the UUID will be added and removed from the list when add() and remove() are called.
Here is an example:
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
public class ListExample {
public static void main(String[] args) {
List<MyObject> list = new ArrayList<>();
list.add(new MyObject(UUID.fromString("299971c6-31b8-11ec-8d3d-0242ac130003"), "A", 0));
list.add(new MyObject(UUID.fromString("31e77904-31b8-11ec-8d3d-0242ac130003"), "B", 1));
list.add(new MyObject(UUID.fromString("368b0214-31b8-11ec-8d3d-0242ac130003"), "C", 2));
System.out.println(list);
list.remove(new MyObject(UUID.fromString("368b0214-31b8-11ec-8d3d-0242ac130003")));
System.out.println(list);
list.add(new MyObject(UUID.fromString("9b21edea-31b9-11ec-8d3d-0242ac130003"), "D", 3));
System.out.println(list);
}
public static class MyObject {
public UUID uuid;
public String string1;
public int int1;
public MyObject(UUID uuid) {
this.uuid = uuid;
}
public MyObject(UUID uuid, String string1, int int1) {
this.uuid = uuid;
this.string1 = string1;
this.int1 = int1;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyObject myObject = (MyObject) o;
return Objects.equals(uuid, myObject.uuid);
}
@Override
public int hashCode() {
return Objects.hash(uuid);
}
@Override
public String toString() {
return "MyObject{"
"uuid=" uuid
", string1='" string1 '\''
", int1=" int1
'}';
}
}
}
Here is the output:
[MyObject{uuid=299971c6-31b8-11ec-8d3d-0242ac130003, string1='A', int1=0}, MyObject{uuid=31e77904-31b8-11ec-8d3d-0242ac130003, string1='B', int1=1}, MyObject{uuid=368b0214-31b8-11ec-8d3d-0242ac130003, string1='C', int1=2}]
[MyObject{uuid=299971c6-31b8-11ec-8d3d-0242ac130003, string1='A', int1=0}, MyObject{uuid=31e77904-31b8-11ec-8d3d-0242ac130003, string1='B', int1=1}]
[MyObject{uuid=299971c6-31b8-11ec-8d3d-0242ac130003, string1='A', int1=0}, MyObject{uuid=31e77904-31b8-11ec-8d3d-0242ac130003, string1='B', int1=1}, MyObject{uuid=9b21edea-31b9-11ec-8d3d-0242ac130003, string1='D', int1=3}]
CodePudding user response:
Use something like this:
List<MyObject> list = new ArrayList<MyObject>();
int id = 4; //the UUID of the Object you are searching for
MyObject found = list.stream()
.filter(MyObject -> (MyObject.getUUID()==id))
.findAny()
.orElse(null);
int i = list.indexOf(found);
int newUUID = 3;
list.get(i).setUUID(newUUID);