-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhashSet.java
More file actions
39 lines (31 loc) · 964 Bytes
/
hashSet.java
File metadata and controls
39 lines (31 loc) · 964 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
30
31
32
33
34
35
36
37
38
39
package hashSet;
import java.util.HashSet;
import java.util.Set;
public class hashSet {
public static void setDemo() {
Set<String> fruit = new HashSet<>();
fruit.add("apple");
fruit.add("lemon");
fruit.add("banana");
fruit.add("orange");
fruit.add("lemon");
System.out.println(fruit.size()); //4
System.out.println(fruit); //[banana, orange, apple, lemon]
// One way of iterating through the Set
var i = fruit.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
// Iterating using the enhanced for loop
for (String j: fruit) {
System.out.println(j);
}
// Iterating using a forEach method
fruit.forEach(x -> System.out.println(x));
// OR
fruit.forEach(System.out::println);
}
public static void main(String[] args) {
setDemo();
}
}