A Class A day, not a bit tired of the TreeSet

Source: Internet
Author: User

One Class A day, not a bit tired.

Now it's about---TreeSet.

Treeset<e>extends abstractset<e>implements Navigableset<e>, cloneable, Serializable

To tell you the truth, I haven't used this *_*.

--"a NavigableSet implementation based on a TreeMap ." The elements is ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which con Structor is used.

As above, the TreeSet implements the Navigableset, which requires that the elements are sorted in natural order or using the comparator comparer to implement

--"This implementation provides guaranteed log (n) time cost for the basic operations ( add , and remove contains ).

Some basic operations like the ones above say their time complexity in log (n)

--"Note that this implementation are notsynchronized

Still, he is non-thread safe.

Thank!! ~ ~ also provides us with a thread-safe way:

  SortedSet s = collections.synchronizedsortedset (new TreeSet (...));

"The internal implementation of TreeSet is also implemented using TreeMap"

1. Construction Method:

He offers a number of ways to construct it.

public TreeSet ();

Public TreeSet (COMPARATOR<? super e> Comparator);

Public TreeSet (collection<? extends e> c)

Public TreeSet (sortedset<e> s)

2. iterators

Do you remember that LinkedList provides an inverted iterator. The same treeset is also provided.

1    Public Iterator<e> Iterator () {2         return  m.navigablekeyset (). Iterator (); 3     }

Since this navigableset m is maintained in TreeSet, the return of all iterators only uses the iterator of his keyset.

3, some special methods of use:

Public E First () {

return M.firstkey ();
} Gets the first element

There is also the last element to get final ()

1   Public e Lower (e e) {2         return M.lowerkey (e); 3     }

Gets the closest element that is smaller than E.

1     Public e Floor (e e) {2         return M.floorkey (e); 3     }

Gets the smallest element greater than or equal to E

The ceiling of the opposite

The opposite of lower is higher.

Want to learn this class first to understand Navigableset

A Class A day, not a bit tired of the TreeSet

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.