collection classes in Java

Source: Internet
Author: User

The real line border is the implementation class , such as Arraylist,linkedlist,hashmap, and so on, the polyline border is abstract class , such as Abstractcollection, Abstractlist,abstractmap and so on, and the point line border is the interface , such as Collection,iterator,list.

All of the above collection classes implement the Iterator interface, which is an interface for iterating through the elements in the collection, consisting mainly of the Hashnext (), Next (), and remove () three methods. One of its sub-interfaces Linkediterator added three methods on top of it, namely add (), previous (), hasprevious (). That is, if the implementation of the iterator interface, then in the collection of elements, can only be traversed backwards, the traversed element will not be traversed, usually the unordered collection is implemented by this interface, such as Hashset,hashmap, and those elements of the ordered collection, The implementation of the general is the Linkediterator interface, the implementation of this interface can be a collection of two-way traversal, either through next () access to the next element, but also through previous () access to the previous element, such as ArrayList.

1, ArrayList, LinkedList

Both are ordered, allowing repetition of elements, different efficiencies in locating elements, adding and removing elements

2, HashSet, TreeSet, Linkedhashset

HashSet unordered and non-repeatable

TreeSet is better than HashSet, an ordered collection (by default, the red-black tree is used to complete the sorting). The sort here is based on the comparison of elements, not the order of insertions.

Linkedhashset on the basis of HashSet and formed a doubly linked list, you can remember the order of insertion

3, HashMap, TreeMap, Linkedhashmap

HashMap keys must be unique, values can be duplicated, no order

TreeMap has been improved on the basis of HashMap, and it is an orderly key-value pair to achieve sorting by comparing key values.

Linkedhashmap on the basis of HashSet and formed a doubly linked list, in accordance with the order of access, the mapping entries are adjusted, each access to the entry will automatically move to the tail of the doubly linked list

4, Weakhashmap

A hash table-based MAP implemented with weak keys. In Weakhashmap, when a key is no longer in normal use, its entry is automatically removed.

It is best to use Weakhashmap when using a cache that expires in a short period of time, and it contains an automatically called method Expungestaleentries, which executes the implied method directly after the value is referenced and clears the unused key.

Reference:

http://blog.csdn.net/liulin_good/article/details/6213815

Basic knowledge of Java core Technology Volume I

collection classes 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.