java迭代map的方式及迭代器原理 |
您所在的位置:网站首页 › map遍历remove › java迭代map的方式及迭代器原理 |
java迭代map的方式及迭代器原理 目录java迭代map的方式及迭代器原理一、迭代器原理1、什么是迭代器,使用迭代器的好处?2、迭代器怎么实现的?3、迭代器的陷阱?4、为什么会产生这样的错误?5、 iterable接口源代码6、 iterator接口源代码7、HashMap中实现迭代的核心代码二、遍历map的方式 java迭代map的方式及迭代器原理 一、迭代器原理 1、什么是迭代器,使用迭代器的好处? 迭代器就是用来遍历集合中对象的东西,也就是说,对于集合,我们不像对原始数组那样通过直接访问元素来迭代的,而是通过迭代器来遍历对象。这么做的好处是将对于集合类型的遍历行为与被遍历集合对象分离,这样以来,就不需要关心该集合类型的具体实现是怎么样的。只要获取这个集合对象的迭代器便可以遍历这个集合中的对象。而像遍历对象顺序以及怎么访问对象元素这些细节,全部由它自己的迭代器来处理。 2、迭代器怎么实现的? 首先集合要先实现iterable接口来表示此对象是可以进行迭代的。而实现iterable接口的对象实现了iterator方法,这个方法返回了一个Iterator对象。一个迭代器对象需要Iterator接口中的方法:hasNext(),next(),remove()。remove()方法会删除最近一次调用的元素,如果remove()之前没有调用next()的话直接调用remove()会产生报错信息(IllegalStateException)。我们在进行对集合对象迭代的时候,next()会返回当前对象第一个对象并返回,然后next会指向下一个元素,hasNext方法就是看这个指针后面还有没有元素了。 3、迭代器的陷阱? 使用for迭代的时候不可以使用集合进行remove操作。这时候需要使用迭代器进行迭代,然后使用迭代器中的remove方法进行删除。 4、为什么会产生这样的错误? remove()方法在删除元素的时候,还会修改一个修改次数的标志位modCount,如果iterator的expectedModCount与modCount的大小不相等时,会抛出一个ConcurrentModificationException异常。modCount的目的主要是为了防止当前对象迭代过程中存在其他线程对当前对象的修改。 5、 iterable接口源代码 public interface Iterable { /** * Returns an iterator over elements of type {@code T}. * * @return an Iterator. */ Iterator iterator(); default void forEach(Consumer super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } default Spliterator spliterator() { return Spliterators.spliteratorUnknownSize(iterator(), 0); } } 6、 iterator接口源代码 public interface Iterator { /** * 如果迭代拥有更多元素,那么返回true */ boolean hasNext(); /** * 返回iteration中的下一个元素 */ E next(); /** * 如果删除一个集合中的元素没有调用这个方法,二十直接中集合中删除,那么这个迭代器的行为没有被指定 */ default void remove() { throw new UnsupportedOperationException("remove"); } /** * 遍历集合中的剩余元素(如果之前调用了两次next()那么只会遍历集合中剩余元素 * 使用案例: * Iterator it = map.keySet().iterator(); it.next(); it.next(); it.forEachRemaining(System.out::println); */ default void forEachRemaining(Consumer super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } } 7、HashMap中实现迭代的核心代码 final Node nextNode() { Node[] t; Node e = next; // 对象属性中的next是下一个值 if (modCount != expectedModCount) throw new ConcurrentModificationException(); if (e == null) throw new NoSuchElementException(); // next = e.next 如果e.next为null,那么继续找数组下一个不为null的值 if ((next = (current = e).next) == null && (t = table) != null) { do {} while (index < t.length && (next = t[index++]) == null); } return e; } public final void remove() { Node p = current; if (p == null) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); current = null; K key = p.key; removeNode(hash(key), key, null, false, false); expectedModCount = modCount; 二、遍历map的方式 import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.HashMap; public class MapTest { public static void main(String[] args) { HashMap map = new HashMap(); map.put(0,1); map.put(2,2); map.put(1,2); map.put(4,5); map.put(3,4); // 遍历hashmap entry foreach Set> ent = map.entrySet(); for(Map.Entry entry:ent){ System.out.println(entry.getKey()+" : "+entry.getValue()); //map.remove(0); } System.out.println(); // 通过keySet或者values()遍历 Set set = map.keySet(); for(Integer key:set){ System.out.println(key+" -- "+map.get(key)); } Collection set1 = map.values(); for(Integer val:set1){ System.out.println(val); } System.out.println(); // iterator原理是什么 通过iterator遍历map Iterator> iter = map.entrySet().iterator(); while(iter.hasNext()){ Map.Entry entry = iter.next(); System.out.println(entry.getKey()+" : "+entry.getValue()); iter.remove(); } System.out.println(); Iterator keys = map.keySet().iterator(); while(keys.hasNext()){ int k = keys.next(); System.out.println(k+" -- "+ map.get(k)); } } } |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |