-1

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?

hossein ketabi
  • 418
  • 6
  • 14
  • Did you supply a `.equals()` method, and if so, did you exclude the `.id` field from the equality test? – Alnitak Aug 06 '14 at 07:44
  • Best algo is as used by merge sort. 1 sort both the list. 2 use two pointers say i and j that point to first element of both list. 3 use smaller of both the item pointed by i and j and increment the one that is selected. 4 if both value pointed by i and j are same choose what you want and increment both. – hcl Aug 06 '14 at 07:44

2 Answers2

2

well the best solution would be to override

hashCode() 
equals()

ofcourse, you need to implement what it means for two object to be equal.i.e. not only property but also some other attributes of your object?

after this you can simply use something like:

set.retailAll(set1);
nafas
  • 5,004
  • 1
  • 23
  • 47
  • please use this link for overriding those two method: [Overrideing equals and hashcode method](http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java) – nafas Aug 06 '14 at 07:47
1

If you want a collection of objects that does not contain duplicates a List is the wrong kind of collection. You should use a Set. Create a new Set that contains all the objects in the first list, then add all the objects in the second List. The elimination of duplicates is automatic.

Raedwald
  • 40,290
  • 35
  • 127
  • 207
  • that's right , but my objects aren't equal in fact. in my project's concept , my objects are equal if name property of them are equal, not all property. I can use Set when my objects are equal entirely. – hossein ketabi Aug 06 '14 at 12:58