Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Returns true if both collections contain the same elements and the elements are in the same order.
     * Note that the start elements do not have to be the equal!
     * @param s1 The first collection
     * @param s2 The second collection
     * @return true if s1 and s2 are same in the sense explained above. 
     */
    public static boolean sameElementsSameOrder(Collection<?> s1, Collection<?> s2) {
        if (s1.size() != s2.size())
            return false;

        Iterator<?> it1 = s1.iterator();
        Object first = it1.next();
        for (Iterator<?> it2 = s2.iterator(); it2.hasNext();) {
            if (it2.next().equals(first)) {
                while (it1.hasNext()) {
                    if (!it2.hasNext())
                        it2 = s2.iterator();
                    if (!it1.next().equals(it2.next()))
                        return false;
                }
                return true;
            }
        }
        return false;
    }
}