... Sets

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.

Fig. 16.16

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) { ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.