June 2017
Beginner
1296 pages
69h 23m
English
The collections framework also includes the SortedSet interface (which extends Set) for sets that maintain their elements in sorted order—either the elements’ natural order (e.g., numbers are in ascending order) or an order specified by a Comparator. Class TreeSet implements SortedSet. The program in Fig. 16.16 places Strings into a TreeSet. The Strings are sorted as they’re added to the TreeSet. This example also demonstrates rangeview methods, which enable a program to view a portion of a collection.
1 // Fig. 16.16: SortedSetTest.java
2 // Using SortedSets and TreeSets.
3 import java.util.Arrays;
4 import java.util.SortedSet;
5 import java.util.TreeSet;
6
7 public class SortedSetTest {
8 public static void main(String[] args) { ...Read now
Unlock full access