Here you can find the source of compareTo(Collection
private static <E extends Comparable<E>> int compareTo(Collection<E> o1, Collection<E> o2)
//package com.java2s; /**/* w ww. j av a2s. c om*/ * Copyright (C) 2013 Jayaprakash Pasala * <p/> * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * <p/> * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * <p/> * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * <p/> * * Created with IntelliJ IDEA. * User: Jayaprakash Pasala * Date: 5/2/13 * Time: 3:23 PM */ import java.util.*; public class Main { private static <E extends Comparable<E>> int compareTo(Collection<E> o1, Collection<E> o2) { if (o1.size() < o2.size()) return -1; if (o1.size() > o2.size()) return 1; Iterator<E> it1 = o1.iterator(); Iterator<E> it2 = o2.iterator(); for (; it1.hasNext() && it2.hasNext();) { E first = it1.next(); E second = it2.next(); if (first.equals(second)) continue; return first.compareTo(second); } return 0; } }