Java tutorial
//package com.java2s; import java.util.*; public class Main { public static <T> int compareAsIterable(Comparator<? super T> comparator, Iterable<? extends T> it1, 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 = 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(Iterable<? extends T> it1, 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; } }