I have two list with same object that have different id property value in them, but this items have other same property value(for example name property).
What I want is to join two list in one list with not duplicate items which have same property(expect id
).
a sample of my object:
A{
id:int
name:string
}
method that i want to implement it:
public List<A> join(List<A> list1, List<A> list2){
return a list with aggregated items of list1 and list2(but not with duplicate items which have same property name value)
}
because of different id property value I cant use list.retainAll
method or other similar methods in Java.(in fact two item with different id is not equals , but I want to denied id property).
The code that I write is this:
public List<Node> unionNodeList(List<Node> list1 , List<Node>list2) {
Set<Node> unionList = new HashSet<Node>();
Set<Node> temp = new HashSet<Node>();
for (Node t : list1) {
temp.add(t);
for (Node s : list2) {
if (!t.getPropertyValues().toString().equals(s.getPropertyValues().toString())) {
continue;
} else {
temp.remove(t);
}
}
unionList.addAll(temp);
temp.removeAll(list1);
}
unionList.addAll(list2);
List<Node> myNodes = new ArrayList<Node>(unionList);
return myNodes;
}
my question is that what best algorithm or pattern I can use for that?