使用iterator删除list,map中元素
来源:互联网 发布:代练游戏商场源码 编辑:程序博客网 时间:2024/05/17 08:34
使用iterator删除list,map元素
- 答案优先
- 解析Iterator of List
- 解析Iterator of Map
Answer
List<String> list = new ArrayList<>(); list.add("1"); list.add("2"); list.add("3"); Iterator<String> listIt = list.iterator(); while(listIt.hasNext()) { listIt.next(); //.next() 需要被调(无论是否需要检查此值),否则报java.lang.IllegalStateException listIt.remove(); }
Map<String, String> map = new HashMap<>(); map.put("attr1", "1"); map.put("attr2", "2"); map.put("attr3", "3"); Iterator mi = map.keySet().iterator(); while(mi.hasNext()){ mi.next();//.next() 需要被调用,否则报java.lang.IllegalStateException mi.remove(); }
解析Iterator of List
以ArrayList类为例,它包含了一个内部类:ArrayList$Itr.class ,其中包含三个int数值
int cursor; // 下个返回元素
int lastRet = -1; // 上个返回元素(remove问题出在此)
int expectedModCount = modCount; //容器大小
调用iterator的remove方法时,要提前调下.next()
public void remove() { if (lastRet < 0) throw new IllegalStateException(); //@1 checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; //@2 expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); }}
@1: 此处每次调用remove()时都会判断lastRet的值
@2: 成功remove后,重置-1,结果就导致下次remove前,如果lastRet值未被改变,异常依旧
然而lastRet值只能被.next()方法修改@3:
public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; //@3 lastRet修改在此,源码里就只在这可以被设置成>0的值}
解析Iterator of Map
HashMap
Node<K,V> next; // 下个返回Entry Node<K,V> current; // 当前Entry int expectedModCount; // int index; // 当前位置 HashIterator() { expectedModCount = modCount; //初始化到当前map大小 Node<K,V>[] t = table; current = next = null; //current初始值null index = 0; if (t != null && size > 0) { // advance to first entry do {} while (index < t.length && (next = t[index++]) == null); } }
public final void remove() { Node<K,V> p = current; if (p == null) //然而这里会判断当前entry的值,与list类似,也只有在调用HashIterator.nextNode()时 //才会更新current到新的node上,所以在remove()前调用Iterator.next()在Map也是必要的 throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); current = null; K key = p.key; removeNode(hash(key), key, null, false, false); expectedModCount = modCount;}
LinkedHashMap & LinkedList
这两个容器与基础的HashMap和ArrayList有所区别
private Node<E> lastReturned;
LinkedList中,可以改变lastReturned值得方法也只有内部类中的ListItr中的next(),因为双向链表维护的对象,因此多了previous()和forEachRemaining()方法。
在另一个map容器IdentityHashMap的内部类IdentityHashMapIterator里也存在:
int lastReturnedIndex = -1;
在IdentityHashMap的内部类EntryIterator中:
public void remove() { lastReturnedIndex = ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index); //@4 super.remove(); lastReturnedEntry.index = lastReturnedIndex; lastReturnedEntry = null;}
@4:lastReturnedEntry在初始化时也是指向了‘当前’的那个entry,从而保证了iterator的游标是在向后移动的
总结
‘保证了iterator的游标是在向后移动的’这个结论是推测的,以上无论是ArrayList的lastRet,和linkedhashmap的lastReturnedIndex,包括hashmap和linkedlist中对于当前node
- 使用iterator删除list,map中元素
- Java集合中删除元素使用Iterator
- list/map中安全删除元素
- Map、List元素的删除
- 如何在遍历中使用 iterator/reverse_iterator 删除元素
- 如何在遍历中使用 iterator/reverse_iterator 删除元素
- 删除list中元素
- List, Map, Set与Iterator的使用
- 遍历list时删除某些情况下的元素,使用迭代器Iterator
- 删除map中某条记录,使用iterator方法
- 使用Iterator的remove方法删除元素
- C++ stl list、map、vector删除元素
- stl的vector,map,list删除元素
- 删除map、list集合元素总结
- List,Set,Map遍历时删除元素
- map 中删除指定元素
- Map、List、Iterator、Romdon
- Map/List中的Iterator
- android中按电源键锁屏然后解锁导致Activity调用onDestory以及如何防止锁屏 翻转屏幕也会触发的情况
- 数据挖掘经典算法--adaboost算法
- Spring中事务的配置与使用
- 小米MAX Root,第三方REC,XP框架刷入
- HTC Vive SteamVR Plugin插件使用。
- 使用iterator删除list,map中元素
- Linux下清屏
- STM32-HAL
- ES6这些就够了
- 在Tomcat 服务器中部署简单的Web程序
- 删除字符串中的非26字母字符
- SQL游标的实现
- 关于Jquery-easyUi的常用用法
- hihoCoder太阁最新面经算法竞赛题解(6)