![]() ![]() ![]() |
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 ofObject
s 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,JCTreeSet
will ensure that only one of them is placed in the sorted set.
JCTreeSet
adds this convenience constructor:
- Construct a new
JCTreeSet
containing the elements in a specified array, sorted according to the elements' natural ordering principle.
JCTreeSet
includes the followingTreeSet
standard constructors:
- Construct an empty
TreeSet
using a specifiedCollection
, sorted according to the elements' natural ordering principle.- Construct a new
TreeSet
containing the elements in the specifiedCollection
, sorted according to the elements' natural ordering principle.- Construct a new
JCTreeSet
containing the same elements as the givenSortedSet
, sorted according to the same ordering.
26.2 Constructors and Methods
JCTreeSet
has constructors that allow you to form a sorted set from the elements of aCollection
, an array ofObject
s, or aSortedSet
. A no-parameter constructor lets you instantiate an empty tree set using natural ordering, or you can supply aComparator
to specify how the elements are to be sorted.
JCTreeSet
defines 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
![]() ![]() ![]() |