Android Iterable Compare compareAsIterable( @Nonnull Comparator comparator, @Nonnull Iterable it1, @Nonnull Iterable it2)

Here you can find the source of compareAsIterable( @Nonnull Comparator comparator, @Nonnull Iterable it1, @Nonnull Iterable it2)

Description

compare As Iterable

License

Open Source License

Declaration

public static <T> int compareAsIterable(
            @Nonnull Comparator<? super T> comparator,
            @Nonnull Iterable<? extends T> it1,
            @Nonnull Iterable<? extends T> it2) 

Method Source Code

//package com.java2s;
import javax.annotation.Nonnull;
import java.util.*;

public class Main {
    public static <T> int compareAsIterable(
            @Nonnull Comparator<? super T> comparator,
            @Nonnull Iterable<? extends T> it1,
            @Nonnull Iterable<? extends T> it2) {
        Iterator<? extends T> elements2 = it2.iterator();
        for (T element1 : it1) {
            T element2;//  w ww .ja va 2s  . c o  m
            try {
                element2 = elements2.next();
            } catch (NoSuchElementException ex) {
                return 1;
            }
            int res = comparator.compare(element1, element2);
            if (res != 0)
                return res;
        }
        if (elements2.hasNext()) {
            return -1;
        }
        return 0;
    }

    public static <T extends Comparable<? super T>> int compareAsIterable(
            @Nonnull Iterable<? extends T> it1,
            @Nonnull Iterable<? extends T> it2) {
        Iterator<? extends T> elements2 = it2.iterator();
        for (T element1 : it1) {
            T element2;
            try {
                element2 = elements2.next();
            } catch (NoSuchElementException ex) {
                return 1;
            }
            int res = element1.compareTo(element2);
            if (res != 0)
                return res;
        }
        if (elements2.hasNext()) {
            return -1;
        }
        return 0;
    }
}

Related

  1. compareAsIterable( @Nonnull Iterable it1, @Nonnull Iterable it2)