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?