当前位置: 移动技术网 > IT编程>开发语言>Java > HashMap和List遍历方法及如何遍历删除元素总结

HashMap和List遍历方法及如何遍历删除元素总结

2019年07月19日  | 移动技术网IT编程  | 我要评论

jazzymag,功夫英雄论坛,煤老板返乡

相信大家对集合遍历再熟悉不过了,这里总结一下hashmap和list的遍历方法,以及它们该如何实现遍历删除。

这里对于每种遍历删除出现的问题的原因都给出了详解!

(一)list的遍历方法及如何实现遍历删除

我们造一个list出来,接下来用不同方法遍历删除,如下代码:

list<string> list= new arraylist<string>();
  famous.add("zs");
  famous.add("ls");
  famous.add("ww");
  famous.add("dz");

1、for循环遍历list:

 for(int i=0;i<list.size();i++){
       if(list.get(i).equals("ls"))
       list.remove(i);
      }

这是一种很常见的遍历方式,但是使用这种遍历删除元素会出现问题,原因在于删除某个元素后,list的大小发生了变化,而你的索引也在变化,所以会导致你在遍历的时候漏掉某些元素。比如当你删除第一个元素后,继续根据索引访问第二个元素后,因为删除的原因,后面的元素都往前移动了以为,所以实际访问的是第三个元素。因此,这种遍历方式可以用在读取元素,而不适合删除元素。

2、增强for循环:

    for(string x:list){
      if(x.equals("ls"))
      list.remove(x);
    }

这也是一种很常见的遍历方式,但是使用这种遍历删除元素也会出现问题,运行时会报concurrentmodificationexception异常
其实增强for循环是java语法糖的一种体现,如果大家通过反编译得到字节码,那么上面这段代码的内部实现如下所示:

for(iterator<string> it = list.iterator();it.hasnext();){
     string s = it.next();
     if(s.equals("madehua")){
       list.remove(s);
     }
   }

下面就解释为什么会报concurrentmodificationexception异常。分析iterator的源代码,重点分析整个调用该过程中的
函数(hasnext和remove):

private class itr implements iterator<e> {
	    int cursor;    // index of next element to return
	    int lastret = -1; // index of last element returned; -1 if no such
	    int expectedmodcount = modcount;
 
 
	    public boolean hasnext() {
	      return cursor != size; // size为集合中元素的个数
	    }
 
 
	    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];
	    }
 
 
	    /* 此方法并没被调用,只是调用list.remove方法
	    public void remove() {
	      checkforcomodification();
	      try {
	        arraylist.this.remove(lastret);	// size字段减1
	        cursor = lastret;
	        lastret = -1;
	        expectedmodcount = modcount;
	      } catch (indexoutofboundsexception ex) {
	        throw new concurrentmodificationexception();
	      }
	    }
	    */
 
 
	    final void checkforcomodification() {	// 检查修改和当前版本号是否一致,不一致则抛出异常
	      if (modcount != expectedmodcount)
	        throw new concurrentmodificationexception();
	    }
 
 
  	}
 
 
  	// list.remove
  	@override public boolean remove(object object) {
	    object[] a = array;
	    int s = size;
	    if (object != null) {
	      for (int i = 0; i < s; i++) {
	        if (object.equals(a[i])) {
	          system.arraycopy(a, i + 1, a, i, --s - i);
	          a[s] = null; // prevent memory leak
	          size = s;
	          modcount++;	// 核心代码:修改了版本号。这样当checkforcomodification的时候,modcount值就和expectedmodcount不同
	          return true;
	        }
	      }
	    } else {
	      for (int i = 0; i < s; i++) {
	        if (a[i] == null) {
	          system.arraycopy(a, i + 1, a, i, --s - i);
	          a[s] = null; // prevent memory leak
	          size = s;
	          modcount++;
	          return true;
	        }
	      }
	    }
	    return false;
	  }

接下来梳理一下流程,这时候你就会发现这个异常是在next方法的checkforcomodification中抛出的。抛出的原因是
modcount !=expectedmodcount。这里的modcount是指这个list对象从呢我出来到现在被修改的次数,当调用list
的add或者remove方法的时候,这个modcount都会自动增减;iterator创建的时候modcount被复制给了
expectedmodcount,但是调用list的add和remove方法的时候不会同时自动增减expectedmodcount,这样就导致
两个count不相等,从而抛出异常。大家如果理解了上面的执行流程,以后碰到类似这种问题,比如如果删除的是倒数
第二个元素却不会碰到异常。就会知道为什么了。

3、iterator遍历删除

   iterator<string> it = list.iterator();
   while(it.hasnext()){
    string x = it.next();
    if(x.equals("del")){
      it.remove();
   }
  }

这种方式是可以正常遍历和删除的。但是你可能看到上面代码感觉和增强for循环内部实现的代码差不多,其实差别就在于上面使用一个使用list.remove(),一个使用it.remove()。

(二)hashmap的遍历删除及如何实现遍历删除

一样我们先造一个hashmap出来,如下

	private static hashmap<integer, string> map = new hashmap<integer, string>();;
 
	public static void main(string[] args) {
		
		 for(int i = 0; i < 10; i++){ 
	      map.put(i, "value" + i); 
	    } 
	 
	
	}

1、第一种遍历删除:

    for(map.entry<integer, string> entry : map.entryset()){ 
       integer key = entry.getkey(); 
       if(key % 2 == 0){ 
         system.out.println("to delete key " + key); 
         map.remove(key); 
         system.out.println("the key " + + key + " was deleted"); 
       } 

这种遍历删除依旧会报concurrentmodificationexception异常,

2、第二种遍历删除:

    set<integer> keyset = map.keyset();
	   for(integer key : keyset){
	      if(key % 2 == 0){
	        system.out.println("to delete key " + key);
	        keyset.remove(key);
	        system.out.println("the key " + + key + " was deleted");
	      }
	   }

这种遍历删除依旧会报concurrentmodificationexception异常,

3、第三种遍历删除:

  iterator<map.entry<integer, string>> it = map.entryset().iterator();
    while(it.hasnext()){
      map.entry<integer, string> entry = it.next();
      integer key = entry.getkey();
      if(key % 2 == 0){
      	 system.out.println("to delete key " + key);
      	 it.remove();  
      	 system.out.println("the key " + + key + " was deleted");
 
      }
    }

这种遍历是ok的

分析上述原因,如果大家理解了list的遍历删除,那么感觉hashmap的遍历删除是不是有类似之处啊。下面就分析一下原因:
如果查询源代码以上的三种的删除方式都是通过调用hashmap.removeentryforkey方法来实现删除key的操作。
在removeentryforkey方法内知识一场了key  modcount就会执行一次自增操作,此时modcount就与expectedmodcount不一致了
,上面三种remove实现中,只有第三种iterator的remove方法在调用完removeentryforkey方法后同步了expectedmodcount值与
modcount相同,所以iterator方式不会抛出异常。最后希望大家遇到问题到查询源代码,它会给你最好的解释!
---------------------
作者:demohui
来源:csdn
原文:https://blog.csdn.net/demohui/article/details/77748809
版权声明:本文为博主原创文章,转载请附上博文链接!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网