package graphTheory.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * A list of helper method for Collections, different than the Collection java
 * class methods
 * 
 * @author Watel Dimitri
 * 
 */
public class Collections2 {

	/**
	 * Source : http://eyalsch.wordpress.com/2010/04/01/random-sample
	 * 
	 * @param col
	 * @param size
	 * @return a random subset of the set col which size is size.
	 */
	public static <T> Set<T> randomSubset(Set<T> col, int size) {
		if (size == 0) {
			return new HashSet<T>();
		}
		int s = col.size();
		int dif = s - size;
		if (dif == 0) {
			return new HashSet<T>(col);
		}
		if (dif >= 0 && dif < s) {
			HashSet<T> res = new HashSet<T>();
			int visited = 0;
			Iterator<T> it = col.iterator();
			T item;

			HighQualityRandom rand = new HighQualityRandom();

			while (size > 0) {
				item = it.next();
				if (rand.nextDouble() < ((double) size) / (s - visited)) {
					res.add(item);
					size--;
				}
			}
			return res;
		}
		return null;
	}

	/**
	 * Source : http://eyalsch.wordpress.com/2010/04/01/random-sample
	 * 
	 * @param col
	 * @param size
	 * @return a random subset of the list col which size is size.
	 */
	public static <T> Set<T> randomSubset(List<T> col, int size) {
		if (size == 0) {
			return new HashSet<T>();
		}
		int s = col.size();
		int dif = s - size;
		if (dif == 0) {
			return new HashSet<T>(col);
		}
		if (dif >= 0 && dif < s) {
			HashSet<T> res = new HashSet<T>();
			T item;

			HighQualityRandom rand = new HighQualityRandom();

			for (int i = dif; i < s; i++) {
				item = col.get(rand.nextInt(i + 1));
				if (!res.add(item))
					res.add(col.get(i));
			}
			return res;
		}
		return null;
	}

	/**
	 * @param col
	 * @return a random element of the list col.
	 */
	public static <T> T randomElement(List<T> col) {
		int s = col.size();
		if (s == 0)
			return null;
		int i = Math2.randomInt(s);
		return col.get(i);
	}

	/**
	 * @param k
	 * @return an array containing every integers from 0 to k-1, in that order.
	 */
	public static int[] range(int k) {
		int[] ar = new int[k];
		for (int i = 0; i < k; i++) {
			ar[i] = i;
		}
		return ar;
	}

}

@@ -42,141 +42,9 @@
 til.
-Arrays;%0Aimport java.util.Collection;%0Aimport java.util.HashSet;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Set
+*
 ;%0A%0A/
@@ -2120,10 +2120,429 @@
 ar;%0A%09%7D%0A%0A
+%09public static %3CT extends Comparable%3CT%3E%3E T max(Iterator%3CT%3E it)%7B%0A%09%09T max = null;%0A%09%09while(it.hasNext())%7B%0A%09%09%09T next = it.next();%0A%09%09%09if(max == null %7C%7C max.compareTo(next) == -1)%0A%09%09%09%09max = next;%0A%09%09%7D%0A%09%09return max;%0A%09%7D%0A%0A%0A%09public static void main(String%5B%5D args)%7B%0A%09    ArrayList%3CInteger%3E ar = new ArrayList%3CInteger%3E();%0A%09%09ar.add(1);%0A%09%09ar.add(2);%0A%09%09ar.add(3);%0A%09%09ar.add(4);%0A%09%09ar.add(5);%0A%09%09System.out.println(max(ar.iterator()));%0A%09%7D%0A
 %7D%0A

Add a max function