forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedSetDemo.java
More file actions
46 lines (37 loc) · 1.69 KB
/
Copy pathSortedSetDemo.java
File metadata and controls
46 lines (37 loc) · 1.69 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
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Collection;
public class SortedSetDemo{
public static void main(String[] args){
SortedSet<String> sss = new TreeSet<String>();
String[] fruitAndVeg = {
"apple", "potato", "turnip", "banana", "corn", "carrot", "cherry",
"pear", "mango", "strawberry", "cucumber", "grape", "banana",
"kiwi", "radish", "blueberry", "tomato", "onion", "raspberry",
"lemon", "pepper", "squash", "melon", "zucchini", "peach", "plum",
"turnip", "onion", "nectarine"
};
System.out.println("Array size: " + fruitAndVeg.length);
for(String str: fruitAndVeg){
sss.add(str);
}
dump("sss:", sss);
System.out.println("SortedSet size: " + sss.size());
System.out.println("First element: " + sss.first());
System.out.println("last element: " + sss.last());
System.out.println("Comparator: " + sss.comparator()); // null when using natural ordering.
dump("headSet:", sss.headSet("n")); // less than n
dump("tailSet:", sss.tailSet("n")); // greater or equal to n
System.out.println("Count of p-named fruits & vegetables: " + sss.subSet("p", "q").size());
// Just use the arguments to compare to determine the range. Half closed range.
System.out.println("Incorrect count of c-named fruits & vegetables: " + sss.subSet("carrot", "cucumber").size()); // Subset excludes high endpoint.
System.out.println("Correct count of c-named fruits & vegetables: " + sss.subSet("carrot", "cucumber\0").size() + "; " + sss.subSet("c", "d").size());
}
static void dump(String title, Collection<String> cs){
System.out.print(title + " ");
for(String s: cs){
System.out.print(s + " ");
}
System.out.println();
}
}