-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSetEx.java
More file actions
32 lines (24 loc) · 983 Bytes
/
HashSetEx.java
File metadata and controls
32 lines (24 loc) · 983 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
//http://javarevisited.blogspot.sg/2012/06/hashset-in-java-10-examples-programs.html
import java.util.HashSet;
import java.util.Iterator;
public class Solution
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
HashSet <Integer> s = new HashSet<Integer>(); //Make a new set.
s.add(1); //Add
if (s.contains(1)) s.add(3); //contains
int n = s.size(); //size
s.remove(1); //remove
System.out.println(s.remove(4)); //removing a non-existant element returns false.
System.out.println(s); //print
Object[] a = s.toArray(); //convert to array.
System.out.println(a[0]);
Iterator<Integer> i = s.iterator(); //HashSet iterator
while (i.hasNext()) i.next(); // hasNext and next for iterator
HashSet <Integer> s2 = new HashSet<Integer>();
s2.addAll(s); //merge to sets by addAll.
s.clear(); // clear the set.
}
}