Java Reference
In-Depth Information
{
System.out.print(title+" ");
for (String s: ls)
System.out.print(s+" ");
System.out.println();
}
}
Thisapplicationdemonstratesthateachsuccessive add() methodcallmustincrease
itsindexby2toaccountforthepreviouslyaddedelementwhenaddinglongerweekday
namestothelist.Italsoshowsyouhowtooutputalistinreverseorder:returnalistiter-
atorwithitscursorinitializedpasttheendofthelistandrepeatedlycall previous() .
When you run this application, it generates the following output:
ls: Sun Mon Tue Wed Thu Fri Sat
ls: Sun Sunday Mon Monday Tue Tuesday Wed Wednesday Thu
Thursday Fri Friday Sat Saturday
Saturday Sat Friday Fri Thursday Thu Wednesday Wed Tuesday
Tue Monday Mon Sunday Sun
Set
A set isacollectionthatcontainsnoduplicateelements.Inotherwords,asetcontains
nopairofelements e1 and e2 suchthat e1 .equals( e2 ) returnstrue.Furthermore,a
setcancontainatmostonenullelement.Setsaredescribedbythe Set interface,whose
generic type is Set<E> .
Set extends Collection and redeclares its inherited methods, for convenience
and also to add stipulations to the contracts for add() , equals() , and
hashCode() ,toaddresshowtheybehaveinasetcontext.Also, Set 'sdocumentation
statesthatallconstructorsofimplementationclassesmustcreatesetsthatcontainnodu-
plicate elements.
Set does not introduce new methods.
TreeSet
The TreeSet classprovidesasetimplementationthatisbasedonatreedatastructure.
As a result, elements are stored in sorted order. However, accessing these elements is
Search WWH ::




Custom Search