There is another common bug with iterator traversing deletes

List<String> consSystemNameList=Arrays.asList("SCCP,PMCP,CMCDC,PIS".split(",")); Iterator<String> it = consSystemNameList.iterator(); while(it.hasNext()){ it.next(); it.remove(); } The following error is reported:  Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:161) at java.util.AbstractList$Itr.remove(AbstractList.java:374) at Test.main(Test.java:13)Copy the code

This is because: arrays.aslist () returns java.util.Arrays ArrayList instead of ArrayList. ArraysArrayList, not ArrayList. ArraysArrayList, not ArrayList. ArraysArrayList and ArrayList are inherited AbstractList, remove, add method is the default in AbstractList throw UnsupportedOperationException and don’t make any operation. ArrayList rewrite these methods to the list, but the Arrays $ArrayList without rewriting the remove (int), add (int), and other methods, so throw UnsupportedOperationException.

The solution is to convert to an ArrayList.