Skip to content

Commit 32630f1

Browse files
Add files via upload
1 parent aba2151 commit 32630f1

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

TreeSetDemo.java

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,144 @@
1+
package TreeSet;
2+
import java.util.TreeSet;
3+
import java.util.Iterator;
4+
import java.util.Spliterator;
15
public class TreeSetDemo {
6+
public static void main(String[] args) {
7+
TreeSet<String> set = new TreeSet<>();
8+
TreeSet<String> set1 = new TreeSet<>();
9+
TreeSet<String> clonedset = new TreeSet<>();
10+
TreeSet<Integer> treeSet = new TreeSet<>();
11+
TreeSet<Integer> treeSet1 = new TreeSet<>();
12+
set.add("A");
13+
set.add("a");
14+
set.add("B");
15+
set.add("b");
16+
set1.add("B");
17+
set1.add("b");
18+
set1.add("C");
19+
set1.add("c");
20+
21+
treeSet.add(2);
22+
treeSet1.add(1);
23+
24+
System.out.println(set);
25+
set.remove("A");
26+
System.out.println(set);
27+
set.clear();
28+
System.out.println(set);
29+
System.out.println(" ");
30+
clonedset = (TreeSet<String>) set1.clone();
31+
System.out.println(clonedset);
32+
System.out.println(" ");
33+
Iterator<String> iterator = set1.iterator();
34+
while (iterator.hasNext()) {
35+
System.out.println(iterator.next());
36+
}
37+
System.out.println(" ");
38+
set1.iterator().forEachRemaining(System.out::println);
39+
System.out.println(" ");
40+
set1.iterator().forEachRemaining((String s) -> {
41+
System.out.println(s);
42+
});
43+
44+
System.out.println(" ");
45+
46+
47+
try {
48+
Iterator<Integer> iterator1 = treeSet.iterator();
49+
Iterator<Integer> iterator2 = treeSet1.iterator();
50+
while (iterator1.hasNext() && iterator2.hasNext()) {
51+
int comparison = iterator1.next().compareTo(iterator2.next());
52+
System.out.println(comparison);
53+
}
54+
}
55+
56+
catch (Exception e) {
57+
System.out.println("Exception");
58+
59+
}
60+
61+
try {
62+
Iterator<String> iterator3 = set1.iterator();
63+
String s = "";
64+
while (iterator3.hasNext()) {
65+
s = iterator3.next();
66+
if (s.equals("c")) {
67+
iterator3.remove();
68+
}
69+
}
70+
System.out.println(set1);
71+
}
72+
73+
catch (Exception e) {
74+
System.out.println("Exception");
75+
76+
}
77+
set1.addAll(clonedset);
78+
System.out.println(set1);
79+
80+
System.out.println(" ");
81+
82+
clonedset.retainAll(set1);
83+
System.out.println(clonedset);
84+
85+
System.out.println(" ");
86+
87+
Spliterator<String> numbers = clonedset.spliterator();
88+
numbers.forEachRemaining((n) -> System.out.println("Letters:" + n));
89+
90+
System.out.println(" ");
91+
Boolean b = set1.contains("B");
92+
93+
System.out.println(b);
94+
95+
System.out.println(" ");
96+
System.out.println(set1.size());
97+
98+
System.out.println(" ");
99+
100+
boolean c = set1.isEmpty();
101+
System.out.println(c);
102+
103+
System.out.println(" ");
104+
105+
// java.util.AbstractCollection
106+
107+
Boolean d = set1.containsAll(clonedset);
108+
109+
System.out.println(d);
110+
111+
// java.util.AbstractSet
112+
113+
System.out.println(" ");
114+
115+
TreeSet<String> set3 = new TreeSet<>();
116+
117+
set3.add("A");
118+
set3.add("a");
119+
set1.removeAll(set3);
120+
System.out.println(set1);
121+
122+
//java.util.Collection
123+
124+
set1.removeIf(str -> str.contains("B"));
125+
126+
System.out.println(" ");
127+
System.out.println(set1);
128+
129+
System.out.println(" ");
130+
set1.stream().forEach(s -> System.out.println(s));
131+
132+
System.out.println(" ");
133+
set1.parallelStream().forEach(s -> System.out.println(s));
134+
135+
System.out.println(" ");
136+
Object[] arr = set1.toArray();
137+
for (Object o : arr) {
138+
System.out.println(o);
139+
}
140+
141+
142+
}
2143

3144
}

0 commit comments

Comments
 (0)