Java Collection Equal equals(Iterable collection1, Iterable collection2)

Here you can find the source of equals(Iterable collection1, Iterable collection2)

Description

Returns true if the elements of the two given Collection s are equal and have the same order

License

Apache License

Parameter

Parameter Description
collection1 a parameter
collection2 a parameter

Declaration

public static boolean equals(Iterable<?> collection1, Iterable<?> collection2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 Danny Kunz//from   www .j a  v a 2 s  .  c o m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.Iterator;

public class Main {
    /**
     * Returns true if the elements of the two given {@link Collection}s are equal and have the same order
     * 
     * @param collection1
     * @param collection2
     * @return
     */
    public static boolean equals(Iterable<?> collection1, Iterable<?> collection2) {
        //
        boolean retval = false;

        //
        if (collection1 != null && collection2 != null) {

            //
            Iterator<?> iteratorOther = collection2.iterator();
            Iterator<?> iteratorThis = collection1.iterator();

            //
            retval = true;

            //
            while (iteratorOther.hasNext() && iteratorThis.hasNext()) {
                //
                Object elementOther = iteratorOther.next();
                Object elementThis = iteratorThis.next();

                //
                retval &= elementThis == elementOther || (elementThis != null && elementThis.equals(elementOther));

                //
                if (!retval) {
                    break;
                }
            }

            //
            retval &= !iteratorOther.hasNext() && !iteratorThis.hasNext();

        }

        //    
        return retval;
    }
}

Related

  1. equals(Collection c1, Collection c2)
  2. equals(Collection collection1, Collection collection2)
  3. equals(Collection collection1, Collection collection2)
  4. equals(Collection a, Collection b, boolean ordered)
  5. equals(final Collection c1, final Collection c2)
  6. equalsAny(Object object, Collection objects)
  7. equalsCollectionsAnyOrder(Collection i1, Collection i2)
  8. equalsNull(Collection collection)
  9. equalsOne(final T o1, final Collection others)

  10. HOME | Copyright © www.java2s.com 2016