-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHash Set.txt
More file actions
executable file
·29 lines (21 loc) · 891 Bytes
/
Hash Set.txt
File metadata and controls
executable file
·29 lines (21 loc) · 891 Bytes
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
HashSet should be used when you don't want to allow duplicate value in your collection.
In HashSet you won't be able to find value by index.
If you still want to find the element you have no other option but to use the iterator,
it will iterate through the Hashset and give you one by one element from the Hashset.
You want to find your element by index you should use ArrayList.
There are two ways of iterating the HashSet -
HashSet <String> newset = new HashSet <String>();
// populate hash set
newset.add("A");
newset.add("BB");
newset.add("CCC");
// create an iterator
Iterator iterator = newset.iterator();
// check values
while (iterator.hasNext()){
System.out.println("Value: "+iterator.next() + " ");
}
Second approach is by using for loop-
for (String s : newset ) {
System.out.println("Value: " +s);
}