-
Notifications
You must be signed in to change notification settings - Fork 1
/
HashSetCode.java
72 lines (54 loc) · 1.92 KB
/
HashSetCode.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
68
69
70
71
72
package mypack;
import java.util.*;
public class HashSetCode {
public static void main(String[] args) {
// Write a Java program to append the specified element to the end of a hash
// set.
HashSet<Integer> s = new HashSet<>();
s.add(10);
s.add(20);
s.add(120);
s.add(200);
s.add(150);
s.add(230);
// System.out.println(s);
// Write a Java program to iterate through all elements in a hash list
// Iterator itr = s.iterator();
// while (itr.hasNext()) {
// // System.out.println(itr.next());
// }
// Write a Java program to get the number of elements in a hash set and empty it
// System.out.println(s.size());
// s.clear();
// System.out.println(s);
// Write a Java program to clone a hash set to another hash set
HashSet<String> str = new HashSet<>();
str.add("atb");
str.add("atb2");
str.add("atb4");
str.add("atnuooo");
// System.out.println(str);
// for (String i : str) {
// System.out.println(i);
// }
Set<String> st = new HashSet<>();
st = (Set) str.clone();
st.add("atb4");
st.add("atnu");
st.add("atnub");
// System.out.println(st);
// Write a Java program to convert a hash set to an array
// String ab[] = str.toArray(new String[str.size()]);
// for (String i : ab) {
// System.out.println(i);
// }
// Write a Java program to convert a hash set to a List/ArrayList
// List<String> l = new ArrayList<>(str);
// System.out.println(l);
// Write a Java program to compare two hash set
// System.out.println(str.equals(st));
// Compare two sets and retain elements which are same on both sets
// str.retainAll(st);
// System.out.println(str);
}
}