JClass Elements

PreviousNextIndex

26

Tree Set

Features of JCTreeSet  Constructors and Methods  Examples


26.1 Features of JCTreeSet

This class adds a convenience constructor to java.util.TreeSet, which provides you with a way of representing an array of Objects as a sorted set. If the elements of the set have a defined ordering principle, it is used by default to construct the tree, or you can provide a Comparator. The ordering must be compatible with the conditions for a java.util.TreeSet. If your array contains duplicate items, JCTreeSet will ensure that only one of them is placed in the sorted set.

JCTreeSet adds this convenience constructor:

JCTreeSet includes the following TreeSet standard constructors:


26.2 Constructors and Methods

JCTreeSet has constructors that allow you to form a sorted set from the elements of a Collection, an array of Objects, or a SortedSet. A no-parameter constructor lets you instantiate an empty tree set using natural ordering, or you can supply a Comparator to specify how the elements are to be sorted.

JCTreeSet defines no methods of its own. Its methods are inherited from java.util.TreeSet, java.util.AbstractSet, java.util.AbstractCollection, and java.lang.Object.


26.3 Examples

The example shown here illustrates passing an array of Strings to the constructor. Some Strings are duplicates, but the resulting sorted set contains no duplicates.

import com.klg.jclass.util.JCTreeSet;

public class TreeSetExample {

public static void main(String args[]){
System.out.println("Starting TreeSetExample");
String[] items =
{"moe", "joe", "poe", "zoe", "aoe", "poe", "joe", "moe"};
JCTreeSet ts = new JCTreeSet(items);
System.out.println("The number of items in the array is: " + items.length);
System.out.println("The number of items in JCTreeSet " + ts + " is: " + ts.size());
System.out.println("The last element of " + ts + " is: " + ts.last());
}
}

The output of the program is:

Starting TreeSetExample
The number of items in the array is: 8
The number of items in JCTreeSet [aoe, joe, moe, poe, zoe] is: 5
The last element of [aoe, joe, moe, poe, zoe] is: zoe


PreviousNextIndex