[Java class set] _ sortedset interface notes (instance test)

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.