-
Notifications
You must be signed in to change notification settings - Fork 4
/
LinkedHashSet_Learn.java
203 lines (153 loc) · 5.75 KB
/
LinkedHashSet_Learn.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
/*
* A set is an unordered collection of elements allowing no duplicates,
* i.e. all the elements in a set are unique.
*/
public class LinkedHashSet_Learn {
public static void main(String[] args) {
/*
* The 'Set' interface extends 'Collection' interface and specifies the
* behaviour of a collection that does not allow duplicate elements.
*
* The 'LinkedHashSet' class extends the 'HashSet' class and adds no members of
* its own. It maintains a linked list of the entries in the set, in the order
* in which they were inserted. Thus iterating over the set elements are done as
* per insertion order of elements.
*/
Set<Integer> demoSet = new LinkedHashSet<>();
/*
* Adding elements to LinkedHashSet
*
* boolean add(E obj) : Declared in the Collection interface. Adds object to the
* collection. Returns true if object was added to the collection. Returns false
* if obj is already a member of the collection and the collection does not
* allow duplicates.
*
* boolean addAll(Collection c) : Declared in the Collection interface. Adds all
* the elements of c to the invoking collection. Returns true if elements were
* added to the invoking collection. Otherwise, returns false.
*/
demoSet.add(7);
// demoSet = [7]
demoSet.add(5);
// demoSet = [7, 5]
demoSet.add(3);
// demoSet = [7, 5, 3]
demoSet.add(9);
// demoSet = [7, 5, 3, 9]
demoSet.add(5); // 5 already exists in demoSet hence not added
// demoSet = [7, 5, 3, 9]
if (demoSet.add(3)) // 3 already exists in demoSet hence not added
System.out.println("Element added to set");
else
System.out.println("Duplicate element cannot be added to set");
System.out.println("demoSet = " + demoSet); // demoSet = [7, 5, 3, 9]
Set<Integer> numsSet = new LinkedHashSet<>(Arrays.asList(4, 7, 3, 6, 8));
// numsSet = [3, 4, 6, 7, 8]
numsSet.addAll(Arrays.asList(5, 7, 2, 8, 1, 3)); // duplicate elements are not added
// numsSet = [1, 2, 3, 4, 5, 6, 7, 8]
System.out.println("numsSet = " + numsSet); // numsSet = [1, 2, 3, 4, 5, 6, 7, 8]
/*
* Removing elements from LinkedHashSet
*
* boolean remove(Object obj) : Declared in the Collection interface. Removes
* one instance of obj from the invoking collection. Returns true if the element
* was removed. Otherwise, returns false.
*
* boolean removeAll(Collection c) : Declared in the Collection interface.
* Removes all elements of c from the invoking collection. Returns true if
* elements were removed from the invoking collection. Otherwise, returns false.
*
* boolean retainAll(Collection c) : Declared in the Collection interface.
* Removes all elements from the invoking collection except those in c. Returns
* true if elements were removed from the invoking collection. Otherwise,
* returns false.
*/
// demoSet = [7, 5, 3, 9]
demoSet.remove(7);
// demoSet = [5, 3, 9]
System.out.println("demoSet = " + demoSet); // demoSet = [5, 3, 9]
// numsSet = [1, 2, 3, 4, 5, 6, 7, 8]
numsSet.removeAll(Arrays.asList(1, 3, 5, 7));
// numsSet = [2, 4, 6, 8]
System.out.println("numsSet = " + numsSet); // numsSet = [2, 4, 6, 8]
// numsSet = [2, 4, 6, 8]
numsSet.retainAll(Arrays.asList(4, 8));
// numsSet = [4, 8]
System.out.println("numsSet = " + numsSet); // numsSet = [4, 8]
/*
* Get the count of elements present in the LinkedHashSet
*
* int size() : Declared in the Collection interface. Returns the number of
* elements held in the invoking collection.
*/
// demoSet = [5, 3, 9]
int setSize = demoSet.size();
System.out.println("Size = " + setSize); // Size = 3
/*
* Check if LinkedHashSet is empty or not
*
* boolean isEmpty() : Declared in the Collection interface. Returns true if the
* invoking collection is empty. Otherwise, returns false.
*/
// demoSet = [5, 3, 9]
if (demoSet.isEmpty())
System.out.println("Set is empty !");
else
System.out.println("Set is not empty !");
/*
* Check if an object is present the LinkedHashSet
*
* boolean contains(Object obj) : Declared in the Collection interface. Returns
* true if obj is an element of the invoking collection. Otherwise, returns
* false.
*/
int value = 9;
// demoSet = [5, 3, 9]
if (demoSet.contains(value))
System.out.println("Set contains " + value);
else
System.out.println("Set does not contain " + value);
/*
* Clear the LinkedHashSet
*
* void clear() : Declared in the Collection interface. Removes all elements
* from the invoking collection.
*/
// demoSet = [5, 3, 9]
demoSet.clear();
// demoSet = []
System.out.println("demoSet = " + demoSet); // demoSet = []
/*
* Construct LinkedHashSet from array
*/
String fruits[] = { "apple", "grape", "banana", "orange", "grape" };
LinkedHashSet<String> fruitSet = new LinkedHashSet<>();
Collections.addAll(fruitSet, fruits);
System.out.println("fruitSet = " + fruitSet); // fruitSet = [apple, grape, banana, orange]
/*
* Construct array from LinkedHashSet
*/
String fruitArr[] = fruitSet.toArray(new String[fruitSet.size()]);
System.out.println("fruitArr = " + Arrays.toString(fruitArr)); // fruitArr = [apple, grape, banana, orange]
/*
* Iterating over the contents of a LinkedHashSet
*
* Iterator<E> iterator() : Declared in the Collection interface. Returns an
* iterator for the invoking collection.
*/
Iterator itr = fruitSet.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
System.out.println();
// Iterate using for-each loop
for (String fruit : fruitSet) {
System.out.print(fruit + " ");
}
}
}