Question: Start from this code: __________________________________________ import java.util.*; public class SetListPerformanceTest { static final int N = 35500; public static void main(String[] args) { // Add

Start from this code:

__________________________________________

import java.util.*;

public class SetListPerformanceTest {

static final int N = 35500;

public static void main(String[] args) {

// Add numbers 0, 1, 2, ..., N - 1 to the array list

List list = new ArrayList<>();

for (int i = 0; i < N; i++)

list.add(i);

Collections.shuffle(list); // Shuffle the array list

// Create a hash set, and test its performance

Collection set1 = new HashSet<>(list);

System.out.println("Member test time for hash set is " +

getTestTime(set1) + " milliseconds");

System.out.println("Remove element time for hash set is " +

getRemoveTime(set1) + " milliseconds");

// Create a linked hash set, and test its performance

Collection set2 = new LinkedHashSet<>(list);

System.out.println("Member test time for linked hash set is " +

getTestTime(set2) + " milliseconds");

System.out.println("Remove element time for linked hash set is "

+ getRemoveTime(set2) + " milliseconds");

// Create a tree set, and test its performance

Collection set3 = new TreeSet<>(list);

System.out.println("Member test time for tree set is " +

getTestTime(set3) + " milliseconds");

System.out.println("Remove element time for tree set is " +

getRemoveTime(set3) + " milliseconds");

// Create an array list, and test its performance

Collection list1 = new ArrayList<>(list);

System.out.println("Member test time for array list is " +

getTestTime(list1) + " milliseconds");

System.out.println("Remove element time for array list is " +

getRemoveTime(list1) + " milliseconds");

// Create a linked list, and test its performance

Collection list2 = new LinkedList<>(list);

System.out.println("Member test time for linked list is " +

getTestTime(list2) + " milliseconds");

System.out.println("Remove element time for linked list is " +

getRemoveTime(list2) + " milliseconds");

}

public static long getTestTime(Collection c) {

long startTime = System.currentTimeMillis();

// Test if a number is in the collection

for (int i = 0; i < N; i++)

c.contains((int)(Math.random() * 2 * N));

return System.currentTimeMillis() - startTime;

}

public static long getRemoveTime(Collection c) {

long startTime = System.currentTimeMillis();

for (int i = 0; i < N; i++)

c.remove(i);

return System.currentTimeMillis() - startTime;

}

}

__________________________________________

Add at least 3 data structures that are not in it. For example the stack data structure.

For each of the 3 new data structures add to them the elements in "list" using whatever "add" method is appropriate and them and then use the getTestTime() and getRemoveTime methods on them.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!