Java Reference
In-Depth Information
trieve the obvious components from the collection. The following program uses a
TreeSet
to
sort some names:
// A TreeSet keeps objects in sorted order. Use a Comparator
// published by String for case-insensitive sorting order.
TreeSet
<
String
>
theSet
=
new
new
TreeSet
<>(
String
.
CASE_INSENSITIVE_ORDER
);
theSet
.
add
(
"Gosling"
);
theSet
.
add
(
"da Vinci"
);
theSet
.
add
(
"van Gogh"
);
theSet
.
add
(
"Java To Go"
);
theSet
.
add
(
"Vanguard"
);
theSet
.
add
(
"Darwin"
);
theSet
.
add
(
"Darwin"
);
// TreeSet is Set, ignores duplicates.
System
.
out
.
printf
(
"Our set contains %d elements"
,
theSet
.
size
());
// Since it is sorted we can easily get various subsets
System
.
out
.
println
(
"Lowest (alphabetically) is "
+
theSet
.
first
());
// Print how many elements are greater than "k"
// Should be 2 - "van Gogh" and "Vanguard"
System
.
out
.
println
(
theSet
.
tailSet
(
"k"
).
toArray
().
length
+
" elements higher than \"k\""
);
// Print the whole list in sorted order
System
.
out
.
println
(
"Sorted list:"
);
theSet
.
forEach
(
name
->
System
.
out
.
println
(
name
));
One last point to note is that if you have a
Hashtable
or
HashMap
, you can convert it to a
TreeMap
, and therefore get it sorted, just by passing it to the
TreeMap
constructor:
TreeMap sorted = new TreeMap(unsortedHashMap);
Finding an Object in a Collection
Problem
You need to see whether a given collection contains a particular value.