Chitu
Chitu

Reputation:

How to compare and remove objects from a Set<E> based on condition

I have a Set named scripts. The class E has elements like id,squence and many more . I want to compare two rows of object E in scripts and remove only if id and sequence are same . how do I do it .I have a code that removes all the rows . Can anyone correct this code to remove only the ones that has id and sequence same .

        LinkedList<EditorScriptRow> lList = new LinkedList<EditorScriptRow>       (_scriptRows);

     for (ListIterator<EditorScriptRow> i = lList.listIterator(); i.hasNext();) {
           EditorScriptRow row = i.next();
         int k =0;
        for (ListIterator<EditorScriptRow> j = lList.listIterator(k+1) j.hasNext();) {

           EditorScriptRow row1 = j.next();

            if ((row.getTemplateRow().getId() != null) && 
               (row.getSequence() != null) && 
                (row.getEdit().getName() != null) ) {
                if ((row.getSequence().equals(row1.getSequence())) &&
                    (row.getTemplateRow().getId().equals(row1.getTemplateRow  ().getId      ())) && 
                    (row.getEdit().getName().equals(row1.getEdit().getName()))) {
                        _scriptRows.remove(row);
                }
        }
    }
k++;
}

Upvotes: 0

Views: 1534

Answers (4)

Dan Midwood
Dan Midwood

Reputation: 19444

I would compile a list of id+sequence and check against that. I've just scribbled this out so it may need some refinement but something like..

Set idSequence = new HashSet();
for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
{
  EditorScriptRow row = (EditorScriptRow) iterator.next();
  if ( ( row.getTemplateRow().getId() != null )
    && ( row.getSequence() != null )
    && ( row.getEdit().getName() != null ) )
  {
    String idAndSequence = row.getTemplateRow().getId() + row.getSequence();
    if ( idSequence.contains( idAndSequence ) )
    {
      iterator.remove();
    }
    else
    {
      idSequence.add( idAndSequence );
    }
  }
}

Upvotes: 1

Tarski
Tarski

Reputation: 5500

override equals in EditorScriptRow

@Override
public boolean equals(Object o) {
    if (o instanceof EditorScriptRow) {
        EditorScriptRow other = (EditorScriptRow) o;
        return this.id.equals(other.id) && this.sequence.equals(other.sequence);
    }
    return false;
}

Then create a Set instead of a linked list.

Set<EditorScriptRow> set = new HashSet<EditorScriptRow>();

When you add EditorScriptRow objects to the set, duplicates will be ignored because you have overriden equals in this way

You may wish to view some Java collections tutorials if this doesn't make sense http://java.sun.com/docs/books/tutorial/collections/index.html

Upvotes: 0

RichN
RichN

Reputation: 6231

You mentioned "Set" but your code use LinkedList. Which one is it?

Do you have access to the source code for EditorScriptRow class?

If you do, you can override the equals method. The contract for the Set interface is that it may not contains two objects that are considered equal by the "equals" method. So you can use an implementation of Set (such as HashSet) and this way, you can just add the object to the Set and you are guaranteed to have no duplicate items.

Upvotes: 2

starblue
starblue

Reputation: 56762

You can use remove() on the iterator to remove the last element returned by next().

Upvotes: 0

Related Questions