The relationship and difference between set and list in Java

Source: Internet
Author: User

Two interfaces are inherited from collection.

List (Inteface)

Order isThe most important feature of List is that it ensures that elements are maintained in a specific order.
--arraylist allows fast random access to elements.
--linkedlist is optimized for sequential access toThe cost of the middle insertion and removal of the List is small, with addfrist (), AddLast (), Getfirst,getlast,removefirst, and Removelast (). These methods allow LinkedList to be used as a stack/queue/bidirectional queue.


Set (Inteface)

Each element that is stored in the set must be unique and does not guarantee the order in which the elements are maintained. Object that joins set must define the Equals () method
--hashset set designed for quick Find , the HashSet object must be defined Hashcode ().
The set of the--treeset protection Order , which can be used to extract ordered sequences from the set.
The--linkedhashset has a hashset query speed and internally uses the chain list to maintain the order of elements.
They are not stored the same way:
TreeSet uses a tree of red-and-black trees to sort elements of the structure.
HashSet uses hash functions, which are specifically designed for fast queries.
Linkedhashset internally uses hashing to speed up queries, while maintaining the order of elements using a linked list.

When using Hashset/treeset, you must define equals () for the class, and Hashcode () is for HashSet, which, as a programming style, should overwrite the hashcode () at the same time as the Equals ().

The relationship and difference between set and list in Java

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.