如何对两个对象使用集合方法(removeAll()和retainAll())。 (对象是父子关系)
我预计结果如下,但实际上没有。我想知道如何显示两个集合之间的差异。 (对象是父子关系) 在这种情况下,我可以使用标准方法(如removeAll())还是您可以推荐另一种方法(如使用apache-commons)。谢谢。
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;
}
}
I expected to result below but actually not. I would like to know how to show the differences between two Collections. (objects are parent and child relationship)
In this case, can I use standard method like removeAll() or can you recommend another approach like using 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 集合依赖于
equals
和hashCode
方法(后者由HashMap
、HashSet
等使用)。如果您希望能够使用 Java 集合的数据结构功能(例如
removeAll
、retainAll
等),您需要为对象提供的正确实现>等于
和hashCode
。如果您无法修改
Item
类,则可以使用自己的equals
实现编写一个包装类:为每个对象创建一个新的
ItemWrapper
原始Item
,将ItemWrapper
存储在 Java 集合中,并使用所需的方法 (removeAll
/retainAll
)。然后迭代生成的集合并通过调用每个ItemWrapper
的getItem()
方法来检索Item
。您的另一个选择是子类化 ArrayList,但这似乎是一个更复杂的解决方案。
另一种选择是不使用 Java 集合来执行删除/保留逻辑,而是自己实现它们。
Java collections rely on the
equals
andhashCode
methods (the latter is used byHashMap
s,HashSet
s and others).If you want to be able to use the data structure capabilities of Java collections (such as
removeAll
,retainAll
etc.), you need to supply objects with proper implementations ofequals
andhashCode
.If you can't modify the
Item
class, you can write a wrapper class with your own implementation ofequals
:Create a new
ItemWrapper
for each originalItem
, store theItemWrapper
s in Java collections, and use the required methods (removeAll
/retainAll
). Then iterate over the resulting collection and retrieve theItem
s by calling eachItemWrapper
'sgetItem()
method.Your other option is to subclass
ArrayList
, but it seems like a more convoluted solution.Yet another option is not to use Java collections for the remove/retain logic, implementing them yourself instead.