java迭代map的方式及迭代器原理

您所在的位置:网站首页 map遍历remove java迭代map的方式及迭代器原理

java迭代map的方式及迭代器原理

2024-07-15 15:12:30| 来源: 网络整理| 查看: 265

java迭代map的方式及迭代器原理

目录java迭代map的方式及迭代器原理一、迭代器原理1、什么是迭代器,使用迭代器的好处?2、迭代器怎么实现的?3、迭代器的陷阱?4、为什么会产生这样的错误?5、 iterable接口源代码6、 iterator接口源代码7、HashMap中实现迭代的核心代码二、遍历map的方式

java迭代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));

}

}

}



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭