Please answer correctly, show code and result output of compiled code. HashSet T
ID: 3869569 • Letter: P
Question
Please answer correctly, show code and result output of compiled code.
HashSet
The HashSet class in the JCF implements the Set ADT. A set is an unordered collection of unique items. Imprtant methods include add, contains, remove, isEmpty and size.
https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html
Run the SetORList program. Rerun the program several times with different values of size (see the main method). Try 1000, 10000, 20000, 50000, etc.
What can we say about using an ArrayList and using a HashSet?
Modify the SetORList to generate a collection of 10 items (Integers for example). Sort the ArrayList (using Collections.sort). How do you sort the data in the set?
SETORLIST.JAVA
import java.util.HashSet; import java.util.ArrayList; public class SetORList{ public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<Integer>(); HashSet<Integer> set = new HashSet<Integer>(); int size = 100; System.out.print("making collections... "); for(int i=0; i<size; i+=1){ list.add(i); set.add(i); } System.out.println("done!"); long start_time = System.nanoTime(); for(int i=0; i<2*size; i+=1){ set.contains(i); } long set_time = System.nanoTime()-start_time; System.out.println("set time = " + (set_time*1e-9)); start_time = System.nanoTime(); for(int i=0; i<2*size; i+=1){ list.contains(i); } long list_time = System.nanoTime()-start_time; System.out.println("list time = " + (list_time*1e-9)); HashSet<Integer> s = new HashSet<Integer>(); for(int i=0; i<10; i+=1){ s.add(i); } System.out.println(s); java.util.Collections.sort(s); System.out.println(s); } }Explanation / Answer
For size = 1000:
making collections... done!
set time = 0.0013071200000000002
list time = 0.023455092
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*******************************************
For size = 10000
making collections... done!
set time = 0.005574146
list time = 0.350091896
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*******************************************
For size = 20000:
making collections... done!
set time = 0.006356612
list time = 1.2208360070000002
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*******************************************
For size = 50000:
making collections... done!
set time = 0.012470601000000001
list time = 11.470981642
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
As can be seen from the above runs, the time to search an element in the collection is very much higher in lists than that of in sets. It can also be seen that this difference increases with increase in size of collection. Thus, we can say that though arraylists perform better or are good for smaller collections, hash sets should be used for larger collections which require faster retrievals and unique elements.
Modified SetORList to create collection of items and sort lists:
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class SetORList {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
HashSet<Integer> set = new HashSet<Integer>();
int size = 1000;
System.out.print("making collections... ");
for (int i = 0; i < size; i += 1) {
list.add(i);
set.add(i);
}
System.out.println("done!");
long start_time = System.nanoTime();
for (int i = 0; i < 2 * size; i += 1) {
set.contains(i);
}
long set_time = System.nanoTime() - start_time;
System.out.println("set time = " + (set_time * 1e-9));
start_time = System.nanoTime();
for (int i = 0; i < 2 * size; i += 1) {
list.contains(i);
}
long list_time = System.nanoTime() - start_time;
System.out.println("list time = " + (list_time * 1e-9));
HashSet<Integer> s = new HashSet<Integer>();
for (int i = 0; i < 10; i += 1) {
s.add(i);
}
System.out.println(s);
// Collection of integers
List<Integer> collectionOfItems = new ArrayList();
int arr[] = { 10, 13, 25, 16, 9, 8, 7, 6, 10, 11 };
for (int i = 0; i < 10; i++) {
collectionOfItems.add(arr[i]);
}
// Sorting array list
System.out.println("Before sorting : " + collectionOfItems);
Collections.sort(collectionOfItems);
System.out.println("After sorting : " + collectionOfItems);
}
}
Output:
making collections... done!
set time = 7.385400000000001E-4
list time = 0.01322392
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Before sorting : [10, 13, 25, 16, 9, 8, 7, 6, 10, 11]
After sorting : [6, 7, 8, 9, 10, 10, 11, 13, 16, 25]
Since hashsets are not ordered collections, Collections.sort cannot be used to sort hashsets. To sort hashsets,
Method 1:
Convert hashset to list and sort the list.
System.out.println("Before sorting : " + hashset1);
List<Integer> list1 = new ArrayList<Integer>(hashset1);
Collections.sort(list1);
System.out.println("After sorting : " + list1);
OUTPUT:
Before sorting : [16, 6, 7, 8, 25, 9, 10, 11, 13]
After sorting : [6, 7, 8, 9, 10, 11, 13, 16, 25]
Method 2:
Create a treeset which is a sorted set.
System.out.println("Before sorting : " + hashset2);
TreeSet<Integer> treeset1 = new TreeSet<Integer>(hashset2);
System.out.println("After sorting : " + treeset1);
OUTPUT:
Before sorting : [10, 13, 25, 16, 9, 8, 7, 6, 10, 11]
After sorting : [6, 7, 8, 9, 10, 10, 11, 13, 16, 25]