[Java class set] _ sortedset interface notes
Objectives of this chapter:
Understanding the relationship between sortedset interface and set Interface
Measure the test taker's knowledge about common sortedset operations.
3. Details
The treeset class is an operation class that can be sorted. Treeset is actually a subclass of the sortedset interface, so all classes of this interface can be sorted.
Methods defined in the sortedset Interface
No. method type description
1 Public comparator <? Super E> normal return sorting associated Comparator
2 public e first () returns the first element in the Set
3 Public sortedset <E> headset (E toelement) returns a set of elements from start to end.
4 public e last () returns the last element
5 Public sortedset <E> subset (E fromelement, e toelement) returns elements between specified objects
6 Public sortedset <E> tailset (E fromelement) from the specified element to the end
Import Java. util. sortedset; import Java. util. treeset; public class treesetdemo05 {public static void main (string [] ARGs) {sortedset <string> allset = new treeset <string> (); allset. add ("A"); allset. add ("B"); allset. add ("C"); allset. add ("D"); allset. add ("e"); system. out. println ("first element:" + allset. first (); system. out. println ("last element" + allset. last (); system. out. println ("headset element" + allset. headset ("C"); system. out. println ("tailset element" + allset. tailset ("C"); system. out. println ("subset element:" + allset. subset ("B", "D "));}}
Output:
First element:
Last Element E
Headset element [a, B]
Tailset element [C, D, E]
Subset element: [B, c]
Summary:
As long as you see the interfaces starting with sorted are basically the interfaces that can be sorted
In fact, this is the output interface of the Set interface.