-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathset.html
More file actions
79 lines (69 loc) · 2.73 KB
/
set.html
File metadata and controls
79 lines (69 loc) · 2.73 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://javaalmanac.io/almanac.min.css">
</head>
<body>
<div class="content" id="content">
<sandbox version="java17" mainclass="Main" preview="true" v-cloak>
<sandbox-source name="Main.java">
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(10);
set.add(20);
set.add(20);
System.out.println(set);
int sum = 0;
Iterator<Integer> iter = set.iterator();
while (iter.hasNext())
sum += iter.next();
System.out.println(sum);
sum = 0;
for (int n : set)
sum += n;
System.out.println(sum);
for (Iterator<Integer> i = set.iterator(); i.hasNext(); ) {
int n = i.next();
if (n == 10) i.remove();
}
System.out.println(set);
for (int n : set) {
if (n == 30) set.remove(n) ;
}
System.out.println(set);
System.out.println("Contains 20? " + set.contains(20));
set = new HashSet<>(Set.of(20, 10, 40, 30));
TreeSet<Integer> sortedSet = new TreeSet<>(set);
System.out.println(sortedSet.first());
System.out.println(sortedSet.last());
System.out.println(sortedSet.subSet(20,40)); // 20 <= στοιχεία < 40
System.out.println(sortedSet.subSet(20, true, 40, true)); // inclusive = true
System.out.println(sortedSet.headSet(20)); // στοιχεία < 20
System.out.println(sortedSet.headSet(20, true)); // inclusive = true
System.out.println(sortedSet.tailSet(20)); // στοιχεία >= 20
System.out.println(sortedSet.tailSet(20, false)); // inclusive = false
System.out.println(sortedSet.tailSet(25)); // στοιχεία >= 25
System.out.println(sortedSet.tailSet(25, true)); // inclusive = true
System.out.println(sortedSet.ceiling(25)); // το μικρότερο στοιχείο >= 25
System.out.println(sortedSet.floor(25)); // το μεγαλύτερο στοιχείο <= 25
System.out.println(sortedSet.higher(20)); // το μικρότερο στοιχείο > 20
System.out.println(sortedSet.lower(20)); // το μεγαλύτερο στοιχείο < 20
System.out.println(sortedSet.descendingSet());
Iterator<Integer> i = sortedSet.descendingIterator();
while (i.hasNext())
System.out.print(i.next() + " ");
enum Faces {JACK, QUEEN, KING};
Set<Faces> faceCards = EnumSet.of(Faces.JACK, Faces.QUEEN, Faces.KING);
System.out.println(faceCards);
faceCards = EnumSet.allOf(Faces.class);
System.out.println(faceCards);
}
}</sandbox-source>
</sandbox>
</div>
<script src="https://javaalmanac.io/app/sandbox-bundle.js"></script>
<script>new Vue({ el: '#content' })</script>
</body>
</html>