2016-12-30 22:22:39 -08:00
|
|
|
// collectiontopics/SortedSetDemo.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
2017-01-09 14:26:12 -08:00
|
|
|
import static java.util.stream.Collectors.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
public class SortedSetDemo {
|
|
|
|
public static void main(String[] args) {
|
2017-01-09 14:26:12 -08:00
|
|
|
SortedSet<String> sortedSet =
|
|
|
|
Arrays.stream(
|
|
|
|
"one two three four five six seven eight"
|
|
|
|
.split(" "))
|
|
|
|
.collect(toCollection(TreeSet::new));
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(sortedSet);
|
2015-06-15 17:47:35 -07:00
|
|
|
String low = sortedSet.first();
|
|
|
|
String high = sortedSet.last();
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(low);
|
|
|
|
System.out.println(high);
|
2015-06-15 17:47:35 -07:00
|
|
|
Iterator<String> it = sortedSet.iterator();
|
|
|
|
for(int i = 0; i <= 6; i++) {
|
|
|
|
if(i == 3) low = it.next();
|
|
|
|
if(i == 6) high = it.next();
|
|
|
|
else it.next();
|
|
|
|
}
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(low);
|
|
|
|
System.out.println(high);
|
|
|
|
System.out.println(sortedSet.subSet(low, high));
|
|
|
|
System.out.println(sortedSet.headSet(high));
|
|
|
|
System.out.println(sortedSet.tailSet(low));
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
[eight, five, four, one, seven, six, three, two]
|
|
|
|
eight
|
|
|
|
two
|
|
|
|
one
|
|
|
|
two
|
|
|
|
[one, seven, six, three]
|
|
|
|
[eight, five, four, one, seven, six, three]
|
|
|
|
[one, seven, six, three, two]
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|