2

I want to know Different ways to remove element from Linked Hash Set. I tried following code

LinkedHashSet<String> lhs = new LinkedHashSet<String>();
for(int i=0;i<10;i++)
  lhs.add(String.valueOf(i));
Iterator<String>  it=lhs.iterator();
System.out.println("removed?=="+lhs.remove("1"));
while(it.hasNext()) 
{
    System.out.println("lhs"+it.next());
}

i got following output

removed?==true
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(Unknown Source)
at java.util.LinkedHashMap$KeyIterator.next(Unknown Source)
at preac.chapter1.Start.main(Start.java:321)

What i miss? thanks in advance.

P.S I have also tried iterator.remove() method but got Illegal State Exception

EDIT

I just came to know i have to use iterator remove method. then what it is use of Link Hash Set remove method ? In which cases we should use this method?

sar
  • 1,277
  • 3
  • 21
  • 48

2 Answers2

7

Try to remove element using Iterator.remove like below,

LinkedHashSet<String> lhs = new LinkedHashSet<String>();
for (int i = 0; i < 10; i++) {
   lhs.add(String.valueOf(i));
}  

Iterator<String>  it=lhs.iterator();
  //  System.out.println("removed?=="+lhs.remove("1"));
 while(it.hasNext()) {
   String value=it.next();
   if("1".equals(value)){
      it.remove();
   }
   else{
      System.out.println("lhs  "+value);// Print the other value except 1
    }
 }
System.out.println(lhs);// After remove see the result here.
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • it will delete all records in linked HashSet. After your while loop i again called while to print data.but it prints nothing.I think it is deleting all data. I want to delete only specific record. – sar Nov 22 '13 at 12:23
  • @saurabh, It will delete only "1", I have tested it. – Masudul Nov 22 '13 at 12:25
  • your correct..your sysout works . but my while(it.hasNext()) { System.out.println("lhs"+it.next()); } is not executing. I have put these code below sysout.Do you why these happening? – sar Nov 22 '13 at 12:28
  • You should call `it.next()` only once. That has been called before `if` of `while` loop. – Masudul Nov 22 '13 at 12:30
  • how to iterate the list again with Iterator? i create new iterator and assign list to it and when i try to execute while(newIterator.lhs) got an error no such element Exception. – sar Nov 22 '13 at 12:41
  • while expect the `boolean` value of `it.hasNext()`. `while(newIterator.lhs)` is not correct. – Masudul Nov 22 '13 at 12:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41703/discussion-between-saurabh-and-masud) – sar Nov 22 '13 at 12:50
2

You get the exception because the iterator realizes that you called remove after creating the iterator (using an internal modification counter).

Let's assume add and remove increment the modification counter by 1. When the iterator is created, it sees a modification counter of 10. However, when the iterator is first accessed, the modification counter is 11, due to the call to remove, hence the exception.

Switch the statements and it should be fine:

...
System.out.println("removed?=="+lhs.remove("1"));
Iterator<String>  it=lhs.iterator();
...
Thomas
  • 87,414
  • 12
  • 119
  • 157