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.*;
public class ListExample {
public static void main(String[] args) {
System.out.println("Implementation using a LinkedList.");
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);
System.out.println("Remove an entry.");
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);
System.out.println("Adding a duplicate (creates a new entry).");
list.add(new MyObject(UUID.fromString("299971c6-31b8-11ec-8d3d-0242ac130003"), "A", 0));
System.out.println(list);
System.out.println("Remove duplicate (removes the first entry but not the last).");
list.remove(new MyObject(UUID.fromString("299971c6-31b8-11ec-8d3d-0242ac130003"), "A", 0));
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 +
'}';
}
}
}
Output:
Implementation using a LinkedList.
[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}]
Remove an entry.
[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}]
Adding a duplicate (creates a new entry).
[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}, MyObject{uuid=299971c6-31b8-11ec-8d3d-0242ac130003, string1='A', int1=0}]
Remove duplicate (removes the first entry but not the last).
[MyObject{uuid=31e77904-31b8-11ec-8d3d-0242ac130003, string1='B', int1=1}, MyObject{uuid=9b21edea-31b9-11ec-8d3d-0242ac130003, string1='D', int1=3}, MyObject{uuid=299971c6-31b8-11ec-8d3d-0242ac130003, string1='A', int1=0}]
The main issue here is that duplicates can be created (because we use a list)
If you need a way of preserving the order and exclude duplicates, use a LinkedHashSet
import java.util.*;
public class SetExample {
public static void main(String[] args) {
System.out.println("Implementation using a LinkedHashSet");
Set<MyObject> set = new LinkedHashSet<>();
set.add(new MyObject(UUID.fromString("299971c6-31b8-11ec-8d3d-0242ac130003"), "A", 0));
set.add(new MyObject(UUID.fromString("31e77904-31b8-11ec-8d3d-0242ac130003"), "B", 1));
set.add(new MyObject(UUID.fromString("368b0214-31b8-11ec-8d3d-0242ac130003"), "C", 2));
System.out.println(set);
System.out.println("Remove an entry.");
set.remove(new MyObject(UUID.fromString("368b0214-31b8-11ec-8d3d-0242ac130003")));
System.out.println(set);
set.add(new MyObject(UUID.fromString("9b21edea-31b9-11ec-8d3d-0242ac130003"), "D", 3));
System.out.println(set);
System.out.println("Adding a duplicate (does not create a new entry).");
set.add(new MyObject(UUID.fromString("299971c6-31b8-11ec-8d3d-0242ac130003"), "A", 0));
System.out.println(set);
}
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 +
'}';
}
}
Output:
Implementation using a LinkedHashSet
[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}]
Remove an entry.
[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}]
Adding a duplicate (does not create a new entry).
[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}]