How to use Collections (removeAll () and retainAll ()) methods for two objects. (objects are parent-child relationships)

I expected to get lower, but not really. I would like to know how to show the differences between the two collections. (objects are parent and child relationships) In this case, I can use a standard method, for example removeAll (), or you can recommend a different approach, for example, use apache-commons. Thanks.

CONSTRAINT ------------------------------ 1.Item.class is unmodifiable(eg. I can not add equals method) 2.If id is same between two objects, they are assumed as same things. ------------------------------ EXPECTED ------------------------------ removed object are: 2 same object are: 1 3 add object are: 4 ------------------------------ ACTUAL ------------------------------ removed object are: 1 2 3 same object are: add object are: 1 3 4 ------------------------------ package com.javastudy; import java.util.ArrayList; import java.util.List; public class CollectionCompareToObjects { public static void main(String[] args) { List<Item> before = new ArrayList<Item>(); List<ItemEx> after = new ArrayList<ItemEx>(); before.add(new Item(1L)); before.add(new Item(2L)); // delete before.add(new Item(3L)); after.add(new ItemEx(1L)); after.add(new ItemEx(3L)); after.add(new ItemEx(4L)); // added List<Item> removed = new ArrayList<Item>(before); removed.removeAll(after); System.out.println("removed objects are:"); for(Item item : removed){ System.out.println(item.getId()); } List<Item> same = new ArrayList<Item>(before); same.retainAll(after); System.out.println("same objects are:"); for(Item item : same){ System.out.println(item.getId()); } List<Item> added = new ArrayList<Item>(after); added.removeAll(before); System.out.println("add objects are:"); for(Item item : added){ System.out.println(item.getId()); } } } package com.javastudy; public class Item { private Long id; public Item(Long id) { this.id = id; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } } package com.javastudy; public class ItemEx extends Item { private String name; public ItemEx(Long id) { super(id); } public String getName() { return name; } public void setName(String name) { this.name = name; } } 
+4
source share
1 answer

Java collections rely on equals and hashCode methods (the latter are used by HashMap s, HashSet and others).

If you want to use the capabilities of the data structure in Java sets (e.g. removeAll , retainAll , etc.), you need to provide objects with the correct implementations equals and hashCode .

If you cannot change the Item class, you can write a wrapper class with your own equals implementation:

 public class ItemWrapper { private final Item item; public ItemWrapper(Item item) { this.item = item; } public Item getItem() { return item; } @Override public boolean equals(Object obj) { return obj instanceof ItemWrapper && item.getId().equals(((ItemWrapper) obj).item.getId()); } @Override public int hashCode() { return item.getId().hashCode(); } } 

Create a new ItemWrapper for each original Item , save the ItemWrapper in Java collections and use the necessary methods ( removeAll / retainAll ). Then ItemWrapper over the resulting set and retrieve the Item by calling each ItemWrapper getItem() method.

Another option is to subclass ArrayList , but it seems to be a more confusing solution.

Another option is to not use Java collections for the remove / keep logic, instead implementing them yourself.

+9
source

Source: https://habr.com/ru/post/1332368/


All Articles