![]() ![]()
|
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 ofObjects 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 aComparator. The ordering must be compatible with the conditions for ajava.util.TreeSet. If your array contains duplicate items,JCTreeSetwill ensure that only one of them is placed in the sorted set.
JCTreeSetadds this convenience constructor:
- Construct a new
JCTreeSetcontaining the elements in a specified array, sorted according to the elements' natural ordering principle.
JCTreeSetincludes the followingTreeSetstandard constructors:
- Construct an empty
TreeSetusing a specifiedCollection, sorted according to the elements' natural ordering principle.- Construct a new
TreeSetcontaining the elements in the specifiedCollection, sorted according to the elements' natural ordering principle.- Construct a new
JCTreeSetcontaining the same elements as the givenSortedSet, sorted according to the same ordering.
26.2 Constructors and Methods
JCTreeSethas constructors that allow you to form a sorted set from the elements of aCollection, an array ofObjects, or aSortedSet. A no-parameter constructor lets you instantiate an empty tree set using natural ordering, or you can supply aComparatorto specify how the elements are to be sorted.
JCTreeSetdefines no methods of its own. Its methods are inherited fromjava.util.TreeSet,java.util.AbstractSet,java.util.AbstractCollection, andjava.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());
}
}
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
![]() ![]()
|