| 7 | |
| 8 | public class SortedSetDemo { |
| 9 | public static void main(String[] args) { |
| 10 | SortedSet<String> sortedSet = |
| 11 | Arrays.stream( |
| 12 | "one two three four five six seven eight" |
| 13 | .split(" ")) |
| 14 | .collect(toCollection(TreeSet::new)); |
| 15 | System.out.println(sortedSet); |
| 16 | String low = sortedSet.first(); |
| 17 | String high = sortedSet.last(); |
| 18 | System.out.println(low); |
| 19 | System.out.println(high); |
| 20 | Iterator<String> it = sortedSet.iterator(); |
| 21 | for(int i = 0; i <= 6; i++) { |
| 22 | if(i == 3) low = it.next(); |
| 23 | if(i == 6) high = it.next(); |
| 24 | else it.next(); |
| 25 | } |
| 26 | System.out.println(low); |
| 27 | System.out.println(high); |
| 28 | System.out.println(sortedSet.subSet(low, high)); |
| 29 | System.out.println(sortedSet.headSet(high)); |
| 30 | System.out.println(sortedSet.tailSet(low)); |
| 31 | } |
| 32 | } |
| 33 | /* Output: |
| 34 | [eight, five, four, one, seven, six, three, two] |