Java Reference
In-Depth Information
EnumSet , whose generic type is EnumSet<E extends Enum<E>> , provides
variousclassmethodsforconvenientlyconstructingenumsets.Forexample, <E ex-
tends Enum<E>> EnumSet<E> of(E e1, E e2) returns an EnumSet in-
stance consisting of elements e1 and e2 . In this example, those elements are Week-
day.SUNDAY and Weekday.MONDAY .
When you run this application, it generates the following output:
SUNDAY
MONDAY
Note Aswellasprovidingseveraloverloaded of() methods, EnumSet provides
othermethodsforconvenientlycreatingenumsets.Forexample, allOf() returnsan
EnumSet instancethatcontainsallofanenum'sconstants,wherethismethod'ssolit-
ary argument is a class literal that identifies the enum:
Set<Weekday> allWeekDays = EnumSet.allOf(Weekday.class);
Similarly, range() returns an EnumSet instance containing a range of an enum's
elements (with the range's limits as specified by this method's two arguments):
for
(WeekDay
wd
:
EnumSet.range(WeekDay.MONDAY,
WeekDay.FRIDAY))
System.out.println(wd);
SortedSet
TreeSet is an example of a sorted set, which is a set that maintains its elements in
ascendingorder,sortedaccordingtotheirnaturalorderingoraccordingtoacomparator
that is supplied when the sorted set is created. Sorted sets are described by the Sor-
tedSet interface.
SortedSet , whose generic type is SortedSet<E> , extends Set . With two ex-
ceptions,themethodsitinheritsfrom Set behaveidenticallyonsortedsetsasonother
sets:
• The Iterator instancereturnedfrom iterator() traversesthesortedset
in ascending element order.
• The array returned by toArray() contains the sorted set's elements in order.
Note Althoughnotguaranteed,the toString() methodsof SortedSet imple-
mentationsintheCollectionsFramework(suchas TreeSet )returnastringcontain-
ing all the sorted set's elements in order.
Search WWH ::




Custom Search