Java Collection Compare compare(Collection lhs, Collection rhs)

Here you can find the source of compare(Collection lhs, Collection rhs)

Description

compare

License

Open Source License

Declaration

public static <T extends Comparable<T>> int compare(Collection<T> lhs, Collection<T> rhs) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;

public class Main {
    public static <T extends Comparable<T>> int compare(Collection<T> lhs, Collection<T> rhs) {
        int cmp;// w  w w.  j  a  va 2  s. co  m
        Iterator<T> lit = lhs.iterator();
        Iterator<T> rit = rhs.iterator();
        while (lit.hasNext() && rit.hasNext()) {
            cmp = lit.next().compareTo(rit.next());
            if (cmp != 0) {
                return cmp;
            }
        }
        return lhs.size() - rhs.size();
    }

    public static <T extends Comparable<T>> int compare(Collection<T> lhs, Collection<T> rhs,
            Comparator<T> comparator) {
        int cmp;
        Iterator<T> lit = lhs.iterator();
        Iterator<T> rit = rhs.iterator();
        while (lit.hasNext() && rit.hasNext()) {
            cmp = comparator.compare(lit.next(), rit.next());
            if (cmp != 0) {
                return cmp;
            }
        }
        return lhs.size() - rhs.size();
    }
}

Related

  1. compare(Collection c1, Collection c2)
  2. compare(Collection a, Collection b)
  3. compare(Collection a, Collection b)
  4. compare(final Collection o1, final Collection o2)
  5. compare(final Collection c0, final Collection c1, final Comparator c)
  6. compareAnyOrder(Collection c1, Collection c2)
  7. compareCollections(Collection a, Collection b)