-
Notifications
You must be signed in to change notification settings - Fork 0
/
P5-Main.java
67 lines (50 loc) · 1.75 KB
/
P5-Main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Java program to demonstrate working of HashSet
import java.util.*;
class P5
{
public static void main(String[]args)
{
HashSet<String> hash = new HashSet<String>();
hash.add("India");
hash.add("Australia");
hash.add("China");
hash.add("Japan");
hash.add("India"); // ADDING DUPLICATE ELEMENT BUT NOT ADDED
System.out.println(hash);
System.out.println("\nList contains India or not: " +
hash.contains("India"));
// Removing items from HashSet using remove()
hash.remove("Australia");
System.out.println("\nList after removing Australia: "+hash);
// Iterating over hash set items
System.out.println("\nIterating over list:");
Iterator<String> i = hash.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
// Java program to demonstrate working of HashSet
import java.util.*;
class P5
{
public static void main(String[]args)
{
HashSet<Integer> hash = new HashSet<Integer>();
hash.add(1);
hash.add(2);
hash.add(3);
hash.add(4);
hash.add(1); // ADDING DUPLICATE ELEMENT BUT NOT ADDED
System.out.println(hash);
System.out.println("\nList contains 3 or not: " +
hash.contains(3));
// Removing items from HashSet using remove()
hash.remove(4);
System.out.println("\nList after removing 4 : "+hash);
// Iterating over hash set items
System.out.println("\nIterating over list:");
Iterator<Integer> i = hash.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}