Skip to content

Commit 0d3e9f0

Browse files
committed
Create TreeSet_Learn.java
1 parent 5190d8f commit 0d3e9f0

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

Diff for: TreeSet_Learn.java

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import java.util.Arrays;
2+
import java.util.Collections;
3+
import java.util.Iterator;
4+
import java.util.Set;
5+
import java.util.TreeSet;
6+
7+
/*
8+
* A set is an unordered collection of elements allowing no duplicates,
9+
* i.e. all the elements in a set are unique.
10+
*/
11+
12+
public class TreeSet_Learn {
13+
14+
public static void main(String[] args) {
15+
16+
/*
17+
* The 'Set' interface extends 'Collection' interface and specifies the
18+
* behaviour of a collection that does not allow duplicate elements.
19+
*
20+
* The 'TreeSet' class extends the 'AbstractSet' class and implements the
21+
* 'NavigableSet' interface. It creates a collection that uses a tree for
22+
* storage. Objects are stored in sorted, ascending order.
23+
*/
24+
25+
Set<Integer> demoSet = new TreeSet<>();
26+
27+
/*
28+
* Adding elements to TreeSet
29+
*
30+
* boolean add(E obj) : Declared in the Collection interface. Adds object to the
31+
* collection. Returns true if object was added to the collection. Returns false
32+
* if obj is already a member of the collection and the collection does not
33+
* allow duplicates.
34+
*
35+
* boolean addAll(Collection c) : Declared in the Collection interface. Adds all
36+
* the elements of c to the invoking collection. Returns true if elements were
37+
* added to the invoking collection. Otherwise, returns false.
38+
*/
39+
40+
demoSet.add(7);
41+
// demoSet = [7]
42+
43+
demoSet.add(5);
44+
// demoSet = [5, 7]
45+
46+
demoSet.add(3);
47+
// demoSet = [3, 5, 7]
48+
49+
demoSet.add(9);
50+
// demoSet = [3, 5, 7, 9]
51+
52+
demoSet.add(5); // 5 already exists in demoSet hence not added
53+
// demoSet = [3, 5, 7, 9]
54+
55+
if (demoSet.add(3)) // 3 already exists in demoSet hence not added
56+
System.out.println("Element added to set");
57+
else
58+
System.out.println("Duplicate element cannot be added to set");
59+
60+
System.out.println("demoSet = " + demoSet); // demoSet = [3, 5, 7, 9]
61+
62+
Set<Integer> numsSet = new TreeSet<>(Arrays.asList(4, 7, 3, 6, 8));
63+
// numsSet = [3, 4, 6, 7, 8]
64+
65+
numsSet.addAll(Arrays.asList(5, 7, 2, 8, 1, 3)); // duplicate elements are not added
66+
// numsSet = [1, 2, 3, 4, 5, 6, 7, 8]
67+
68+
System.out.println("numsSet = " + numsSet); // numsSet = [1, 2, 3, 4, 5, 6, 7, 8]
69+
70+
/*
71+
* Removing elements from TreeSet
72+
*
73+
* boolean remove(Object obj) : Declared in the Collection interface. Removes
74+
* one instance of obj from the invoking collection. Returns true if the element
75+
* was removed. Otherwise, returns false.
76+
*
77+
* boolean removeAll(Collection c) : Declared in the Collection interface.
78+
* Removes all elements of c from the invoking collection. Returns true if
79+
* elements were removed from the invoking collection. Otherwise, returns false.
80+
*
81+
* boolean retainAll(Collection c) : Declared in the Collection interface.
82+
* Removes all elements from the invoking collection except those in c. Returns
83+
* true if elements were removed from the invoking collection. Otherwise,
84+
* returns false.
85+
*/
86+
87+
// demoSet = [3, 5, 7, 9]
88+
demoSet.remove(7);
89+
// demoSet = [3, 5, 9]
90+
91+
System.out.println("demoSet = " + demoSet); // demoSet = [3, 5, 9]
92+
93+
// numsSet = [1, 2, 3, 4, 5, 6, 7, 8]
94+
numsSet.removeAll(Arrays.asList(1, 3, 5, 7));
95+
// numsSet = [2, 4, 6, 8]
96+
97+
System.out.println("numsSet = " + numsSet); // numsSet = [2, 4, 6, 8]
98+
99+
// numsSet = [2, 4, 6, 8]
100+
numsSet.retainAll(Arrays.asList(4, 8));
101+
// numsSet = [4, 8]
102+
103+
System.out.println("numsSet = " + numsSet); // numsSet = [4, 8]
104+
105+
/*
106+
* Get the count of elements present in the TreeSet
107+
*
108+
* int size() : Declared in the Collection interface. Returns the number of
109+
* elements held in the invoking collection.
110+
*/
111+
112+
// demoSet = [3, 5, 9]
113+
int setSize = demoSet.size();
114+
115+
System.out.println("Size = " + setSize); // Size = 3
116+
117+
/*
118+
* Check if TreeSet is empty or not
119+
*
120+
* boolean isEmpty() : Declared in the Collection interface. Returns true if the
121+
* invoking collection is empty. Otherwise, returns false.
122+
*/
123+
124+
// demoSet = [3, 5, 9]
125+
if (demoSet.isEmpty())
126+
System.out.println("Set is empty !");
127+
else
128+
System.out.println("Set is not empty !");
129+
130+
/*
131+
* Check if an object is present the TreeSet
132+
*
133+
* boolean contains(Object obj) : Declared in the Collection interface. Returns
134+
* true if obj is an element of the invoking collection. Otherwise, returns
135+
* false.
136+
*/
137+
138+
int value = 9;
139+
140+
// demoSet = [3, 5, 9]
141+
if (demoSet.contains(value))
142+
System.out.println("Set contains " + value);
143+
else
144+
System.out.println("Set does not contain " + value);
145+
146+
/*
147+
* Clear the TreeSet
148+
*
149+
* void clear() : Declared in the Collection interface. Removes all elements
150+
* from the invoking collection.
151+
*/
152+
153+
// demoSet = [3, 5, 9]
154+
demoSet.clear();
155+
// demoSet = []
156+
157+
System.out.println("demoSet = " + demoSet); // demoSet = []
158+
159+
/*
160+
* Construct TreeSet from array
161+
*/
162+
163+
String fruits[] = { "apple", "grape", "banana", "orange", "grape" };
164+
165+
TreeSet<String> fruitSet = new TreeSet<>();
166+
167+
Collections.addAll(fruitSet, fruits);
168+
169+
System.out.println("fruitSet = " + fruitSet); // fruitSet = [apple, grape, banana, orange]
170+
171+
/*
172+
* Construct array from TreeSet
173+
*/
174+
175+
String fruitArr[] = fruitSet.toArray(new String[fruitSet.size()]);
176+
177+
System.out.println("fruitArr = " + Arrays.toString(fruitArr)); // fruitArr = [apple, grape, banana, orange]
178+
179+
/*
180+
* Iterating over the contents of a TreeSet
181+
*
182+
* Iterator<E> iterator() : Declared in the Collection interface. Returns an
183+
* iterator for the invoking collection.
184+
*/
185+
186+
Iterator itr = fruitSet.iterator();
187+
188+
while (itr.hasNext()) {
189+
System.out.print(itr.next() + " ");
190+
}
191+
192+
System.out.println();
193+
194+
// Iterate using for-each loop
195+
196+
for (String fruit : fruitSet) {
197+
System.out.print(fruit + " ");
198+
}
199+
200+
}
201+
202+
}

0 commit comments

Comments
 (0)