-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHashSetTest.java
48 lines (40 loc) · 1.6 KB
/
HashSetTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
public class HashSetTest
{
//main method
public static void main (String[] args)
{
//Syntax: Set<Datatype> nameofSet = new HashSet<datatype>()
Set<Integer> t = new HashSet<Integer>();
//To append to a Set
t.add(5);
t.add(8);
t.add(5);
t.add(9);
System.out.println(t);//prints 5, 8, 9
t.add(-8);
t.remove(8);
//clear an entire Set
//t.clear();
//check if empty
//t.isEmpty()
//get the length of the Set
//t.size();
//Check if element exists within the Set
boolean m = t.contains(5);
System.out.println(m);
System.out.println(t);
/*
A set is a collection of unordered elements that are unique it cannot contain duplicate values.
Similar to a bubble it doesn’t know where they are, just knows that they exist there.
Set are useful since searching within a set is super fast and runs in constant time O(1)
regardless of how many elements are within whether it is 10000000 elements or 2 elements.
As opposed to arrays the longer it gets the longer it will take to perform an operation within it.
There are three types of sets: HASH SET, TREE SET, LINKED HASH SET. We use a set when we don’t care
how many times something exists or where it exists. We use sets over arrays because it is faster
and simpler to do operation on.
*/
}
}