Common set types for "Java", HashSet, TreeSet, Linkedhashset

Source: Internet
Author: User

HashSet, edged, we are most used to. The other two, we are less used to, today, we look at their differences.

ImportJava.util.HashSet;ImportJava.util.Set; Public classHashsettester { Public Static voidMain (string[] args) {Set<Integer> set =NewHashset<integer>(); Set.add (15); Set.add (31); Set.add (12); Set.add (123); Set.add (42);  for(Integer i:set) {System.out.println (i); }    }}
View Code

HashSet, the ability to quickly find key values.

See its realization, depends on the implementation of HashMap . Print the log:

42123123115

If modified to use TreeSet, this is the log.

TreeSet the underlying maintenance is a red-black tree , maintaining an orderly sequence , the element must implement the comparable interface.

12153142123

and using linkedtreeset, the log is like this.

Keep the insertion order and support Quick Lookup of key values.

Linkedtreeset is the inheritance TreeSet construction method, and TreeSet's construction method uses the linkedhashmap, the visible principle hides in the linkedhashmap inside.

15311212342

Common set types for "Java", HashSet, TreeSet, Linkedhashset

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.