import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

public class HashTest {
	
static class Point implements Comparable<Object>   {
	private int x;
	private int y;
	
	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	
	@Override
	public int compareTo(Object obj) {
		if (this == obj)
			return 0;
		Point other = (Point) obj;
		if (x != other.x)
			return (x - other.x);
		if (y != other.y)
			return (y - other.y);
		return 0;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Point other = (Point) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	
	
}

	public static void benchmark (Collection<Point> s, int range) {
		Random r = new Random(12345);
		long start = System.nanoTime();
		
		for (int i=0; i<1000000; i++) {
			s.add(new Point(r.nextInt(range), r.nextInt(range)));
		}
		int count = 0;
		for (int i=0; i<10000; i++) {
			if (s.contains(new Point(r.nextInt(range), r.nextInt(range))))
				count++;
		}
		long stop = System.nanoTime();
		System.out.format("Time: %d milliseconds%n", (stop - start)/1000000);
		System.out.println("Count = " + count);
	}


	public static void main(String[] args) {
		
		Collection<Point> treeSet = new TreeSet<Point>();
		Collection<Point> hashSet = new HashSet<Point>();
		System.out.println("Using tree set");
		benchmark(treeSet, 10);
		System.out.println("Using hash set");
		benchmark(hashSet, 10);
	}

}
