java中foreach与迭代器

foreach语法主要用于数组,也可以应用于任何的Collection对象。这是Java SE5中引入的被称为Iterable接口的原因。该接口包含一个产生Iterator的iterator()方法,并且Iterable接口被foreach用来在序列中移动。因此如果你创建了任何实现了Iterable的类,都可以将其用于foreach语句中。

一下是Iterable接口的源码:

/*
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package java.lang;

import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;

/**
 * Implementing this interface allows an object to be the target of
 * the "for-each loop" statement. See
 * <strong>
 * <a href="{@docRoot}/../technotes/guides/language/foreach.html">For-each Loop</a>
 * </strong>
 *
 * @param <T> the type of elements returned by the iterator
 *
 * @since 1.5
 * @jls 14.14.2 The enhanced for statement
 */
public interface Iterable<T> {
    /**
     * Returns an iterator over elements of type {@code T}.
     *
     * @return an Iterator.
     */
    Iterator<T> iterator();

    /**
     * Performs the given action for each element of the {@code Iterable}
     * until all elements have been processed or the action throws an
     * exception.  Unless otherwise specified by the implementing class,
     * actions are performed in the order of iteration (if an iteration order
     * is specified).  Exceptions thrown by the action are relayed to the
     * caller.
     *
     * @implSpec
     * <p>The default implementation behaves as if:
     * <pre>{@code
     *     for (T t : this)
     *         action.accept(t);
     * }</pre>
     *
     * @param action The action to be performed for each element
     * @throws NullPointerException if the specified action is null
     * @since 1.8
     */
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

    /**
     * Creates a {@link Spliterator} over the elements described by this
     * {@code Iterable}.
     *
     * @implSpec
     * The default implementation creates an
     * <em><a href="Spliterator.html#binding">early-binding</a></em>
     * spliterator from the iterable's {@code Iterator}.  The spliterator
     * inherits the <em>fail-fast</em> properties of the iterable's iterator.
     *
     * @implNote
     * The default implementation should usually be overridden.  The
     * spliterator returned by the default implementation has poor splitting
     * capabilities, is unsized, and does not report any spliterator
     * characteristics. Implementing classes can nearly always provide a
     * better implementation.
     *
     * @return a {@code Spliterator} over the elements described by this
     * {@code Iterable}.
     * @since 1.8
     */
    default Spliterator<T> spliterator() {
        return Spliterators.spliteratorUnknownSize(iterator(), 0);
    }
}


下面是一个实现Iterable接口的类,将其放入foreach语句中:

public class IterableClass implements Iterable<String> {

	String[] words = ("And that is how we konw"
			+ "the Earth to be banana-shaped.").split(" ");

	public Iterator<String> iterator() {
		return new Iterator<String>(){
			private int index = 0;
			public boolean hasNext() {
				return index < words.length;
			}

			public String next() {
				return words[index++];
			}
			
		};
	}

}

测试类:

public class IterableClassTest {
	public static void main(String[] args) {
		for(String s : new IterableClass()){
			System.out.print(s + " ");
		}
	}
}

输出如下:

And that is how we konwthe Earth to be banana-shaped. 

这里iterator()方法返回的是实现了Iterator<String>的匿名内部类的实例。该匿名内部类可以遍历数组中所有的单词,在main方法中,我们可以看到IterableClass确实可以用于foreach语句中。


下面咱们写个产生反向遍历的迭代器玩玩。

public class ReversibleArrayList<T> extends ArrayList<T> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8711835951216698394L;

	public ReversibleArrayList(Collection<T> c) {
		super(c);
	}

	public Iterable<T> reverse() {
		return new Iterable<T>() {

			public Iterator<T> iterator() {
				return new Iterator<T>(){
					int current = size() - 1;
					public boolean hasNext() {
						return current > -1;
					}

					public T next() {
						return get(current--);
					}
					
				};
			}

		};
	}
}

测试类:

<pre name="code" class="java">public class ReversibleArrayListTest {
	public static void main(String[] args) {
		ReversibleArrayList<String> ral = new ReversibleArrayList<String>(
				Arrays.asList("To be or not to be".split(" ")));
		for (String s : ral) {
			System.out.print(s + " ");
		}
		System.out.println();
		for(String s : ral.reverse()){
			System.out.print(s + " ");
		}
	}
	
}


 


输出结果如下:

To be or not to be 
be to not or be To 




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值