import java.util.*; public class SetListPerformanceTest { public static void main(String[] args) { // Create a hash set, and test its performance Collection set1 = new HashSet(); System.out.println("Time for hash set is " + getTestTime(set1, 500000) + " milliseconds"); // Create a linked hash set, and test its performance Collection set2 = new LinkedHashSet(); System.out.println("Time for linked hash set is " + getTestTime(set2, 500000) + " milliseconds"); // Create a tree set, and test its performance Collection set3 = new TreeSet(); System.out.println("Time for tree set is " + getTestTime(set3, 500000) + " milliseconds"); // Create an array list, and test its performance Collection list1 = new ArrayList(); System.out.println("Time for array list is " + getTestTime(list1, 60000) + " milliseconds"); // Create a linked list, and test its performance Collection list2 = new LinkedList(); System.out.println("Time for linked list is " + getTestTime(list2, 60000) + " milliseconds"); } public static long getTestTime(Collection c, int size) { long startTime = System.currentTimeMillis(); // Add numbers 0, 1, 2, ..., size - 1 to the array list List list = new ArrayList(); for (int i = 0; i < size; i++) list.add(i); Collections.shuffle(list); // Shuffle the array list // Add the elements to the container for (int element: list) c.add(element); Collections.shuffle(list); // Shuffle the array list // Remove the element from the container for (int element: list) c.remove(element); long endTime = System.currentTimeMillis(); return endTime - startTime; // Return the execution time } }