equals Iterator - Java java.util

Java examples for java.util:Iterator

Description

equals Iterator

Demo Code

/*// w w w. j  av a 2 s.com
 * Copyright (c) 2006 Rick Mugridge, www.RimuResearch.com
 * Released under the terms of the GNU General Public License version 2 or later.
 * Written: 5/09/2006
 */
//package com.book2s;
import java.util.Iterator;

public class Main {
    public static void main(String[] argv) {
        Iterator it = java.util.Arrays.asList("asdf", "book2s.com")
                .iterator();
        Iterator it2 = java.util.Arrays.asList("asdf", "book2s.com")
                .iterator();
        System.out.println(equalsIterator(it, it2));
    }

    public static boolean equalsIterator(Iterator<?> it, Iterator<?> it2) {
        if (it == it2)
            return true;
        while (it.hasNext() && it2.hasNext()) {
            if (!it.next().equals(it2.next()))
                return false;
        }
        return !it.hasNext() && !it2.hasNext();
    }
}

Related Tutorials