Java Reference
In-Depth Information
Array size = 29
sss: apple banana blueberry carrot cherry corn cucumber
grape kiwi lemon mango melon
nectarine onion peach pear pepper plum potato radish rasp-
berry squash strawberry
tomato
turnip zucchini
Sorted set size = 26
First element = apple
Last element = zucchini
Comparator = null
hs: apple banana blueberry carrot cherry corn cucumber
grape kiwi lemon mango melon
ts: nectarine onion peach pear pepper plum potato radish
raspberry squash strawberry
tomato
turnip zucchini
Count of p-named fruits & vegetables = 5
Incorrect count of c-named fruits & vegetables = 3
Correct count of c-named fruits & vegetables = 4
Thisoutputrevealsthatthesortedset'ssizeislessthanthearray'ssizebecauseaset
cannot contain duplicate elements: the duplicate
banana
,
turnip
, and
onion
ele-
ments are not stored in the sorted set.
The
comparator()
methodreturnsnullbecausethesortedsetwasnotcreatedwith
acomparator.Instead,thesortedsetreliesonthenaturalorderingof
String
elements
to store them in sorted order.
The
headSet()
and
tailSet()
methodsarecalledwithargument
"n"
toreturn,
respectively,asetofelementswhosenamesbeginwithaletterthatisstrictlylessthan
n
, and a letter that is greater than or equal to
n
.
Finally,theoutputshowsyouthatyoumustbecarefulwhenpassinganupperlimitto
subSet()
.Asyoucansee,
ss.subSet("carrot", "cucumber")
doesnotin-
clude
cucumber
inthereturnedrangeviewbecause
cucumber
is
subSet()
'shigh
endpoint.
Toinclude
cucumber
intherangeview,youneedtoforma
closed range
or
closed
interval
(bothendpointsareincluded).With
String
objects,youaccomplishthistask
by appending
\0
to the string. For example,
ss.subSet("carrot", "cucum-
ber\0")
includes
cucumber
because it is less than
cucumber\0
.