tags:

views:

108

answers:

5

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++;
}
A: 

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

starblue
+2  A: 

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.

RichN
Would need to override the hashCode method too - "If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result" - http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode()
barrowc
A: 

The logic is wrong with regard to the k variable, and the outer iteration should be based on _scriptRows.

RichN's suggestion however is good, to avoid duplicates in the first place...

mjv
+1  A: 

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 );
    }
  }
}
Dan Midwood
List idSequence = new ArrayList() should be replaced with Set idSequence = new HashSet() for better performance.
Staale
A: 

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

Tarski