遍历集合和键值对

  • 一、遍历Collection集合
  • 1.遍历List

for循环:

 public void forEach(List list){
    	int size = list.size();
    	for(int i=0;i<size;i++){
    		System.out.println(list.get(i));
    	}
    }

这个方法适用于所有操作List接口的对象。

  • 2.遍历Set集合
   public void forEachSet(Set set){
    	for(Object o:set.toArray()){
    		System.out.println(o);
    	}
    }

Set收集的对象转化为Object[]返回,再进行遍历,这个方法适用于多有的操作Set接口对象。

  • 3.遍历Queue
public void forEachQueue(Queue queue){
   while(queue.peek()!=null){
   system.out.println(queue.poll());
}
}

然而,无论是List、Set还是Queue都会有个iterator方法。

  • 4.iterator()方法

jdk5之前,定义在Collection接口中,而List、Set、Queue继承自Collection,所以都用于iterator()的行为。iterator()方法会返回java.util.Iterator接口的操作对象,这个对象包含了Collection收集的所有对象,可以使用Iterator的hasNext()看看有无下一个对象,若有的化用next()取得下一个对象。

public void forEach(Collection collection){
Iterator iterator = collection.iterator();
while(iterator.next){
system.out.println(iterator.next());
}
}

jdk5之后,原先定义在Collection中的iterator()方法,提升至新的java.lang.Iterable父接口,因此在JDK5之后,可以使用以下方法,遍历集合所有对象。

  public void forEach(Iterable iterable){
    	Iterator iterator = iterable.iterator();
    	while(iterator.hasNext()){
    		System.out.println(iterator.next());
    	}
    }
    

jdk5之后有了增强式for循环,如下

   public void forEach1(Iterable iterable){
    	for(Object o:iterable){
    		System.out.println(o);
    	}
    }

以上,增强式for循环也调用了iterator()方法,运用返回的iterator对象来迭代取得收集的对象。

  • 二、遍历Map

取得Map中所有的键,调用Map中的keySet()返回Set对象;取得Map中所有的值,则可以使用values()返回Collection对象。

             Map<String,String> map = new HashMap<>();
		map.put("one","一");
		map.put("two", "二");
		map.put("three", "三");
		for(String a:map.keySet()){
			System.out.println(a);
		}
		for(String b:map.values()){
			System.out.println(b);
		}

同时取得Map的键和值,可以使用entrySet()方法,返回Set对象,每个元素都是Map.Entry实例,调用getKey()取得键,调用getValue()取得值。

for(Map.Entry<String, String> entry:map.entrySet()){
			System.out.println(entry.getKey()+"+"+entry.getValue());
		}
  • 三、List循环遍历删除

  JAVA中循环遍历list有三种方式for循环、增强for循环(也就是常说的foreach循环)、iterator遍历。

 

1、for循环遍历list

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

   这种方式的问题在于,删除某个元素后,list的大小发生了变化,而你的索引也在变化,所以会导致你在遍历的时候漏掉某些元素。比如当你删除第1个元素后,继续根据索引访问第2个元素时,因为删除的关系后面的元素都往前移动了一位,所以实际访问的是第3个元素。因此,这种方式可以用在删除特定的一个元素时使用,但不适合循环删除多个元素时使用。

 

2、增强for循环

for(String x:list){
    if(x.equals("del"))
        list.remove(x);

   这种方式的问题在于,删除元素后继续循环会报错误信息ConcurrentModificationException,因为元素在使用的时候发生了并发的修改,导致异常抛出。但是删除完毕马上使用break跳出,则不会触发报错。

 

3、iterator遍历

 

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

  这种方式可以正常的循环及删除。但要注意的是,使用iterator的remove方法,如果用list的remove方法同样会报上面提到的ConcurrentModificationException错误。

 

  总结:

  (1)循环删除list中特定一个元素的,可以使用三种方式中的任意一种,但在使用中要注意上面分析的各个问题。

  (2)循环删除list中多个元素的,应该使用迭代器iterator方式

转载于:https://my.oschina.net/u/3422045/blog/1494759

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值