Java Collection class

Source: Internet
Author: User


The List interface is an ordered Collection. Unlike the set interface, the List interface allows the same elements. In addition to the Iterator () method, the List also provides the ListIterator () method, which returns the ListIterator interface. This interface has additional methods such as add () and allows you to add, delete, and set elements, you can also traverse forward and backward.

Common classes that implement the List interface include javaslist, ArrayList, Vector, and Stack.

(1) writable List class: allows null elements and has no synchronous method. If multi-threaded access requires self-synchronized access, a synchronous List can be constructed during List creation.

List list = Collections. synchronizedList (new Collections List (·····));

This class provides the get () remove () insert () method at the beginning or end so that it can be used as a stack, queue, bidirectional queue, and so on.

(2) ArrayList class: implements an array of variable sizes, allowing all elements, including null, without synchronization. You can use the ensureCapacity () method to increase the capacity before inserting a large number of elements.

(3) vector class: similar to ArrayList, but it is synchronous. When the Iterator is created and in use, another thread changes the Vector state and throws ConcurrentModificationException, capture required.

(4) Stack class: inherits from the Vector and provides the push () pop () and peek () methods to get the top element of the Stack.

 

The Set interface is a Collection that does not contain duplicate elements. Up to one null element is allowed.

(1) Hashset: the value of null is allowed and is not synchronized. Compare hashCode to see if it is the same. Compare object elements, You can override the hashCode and equals methods. Supported by hashMap instances, and the iteration sequence of the set is not guaranteed

(2) TreeSet: stores objects using a binary tree, and adds elements to the tree based on binary sorting. Supported by the treeMap instance, it is an ordered set, arrange the elements in the natural order or sort by the comparator provided when the set is created. This means that the elements must implement the comparable interface; you can also pass the comparator object that implements the comparator interface when building a treeSet.

 

The Map ing interface does not inherit the Collection interface, which provides key-value ing.

(1) Hashtable: Synchronous. The hashCode () and equals () methods must be implemented as key objects. If the two objects are the same, the hashCode must be the same. If the two objects are different, their hashCode is not necessarily different.

(2) HashMap: if one or more null values are not synchronized, its values () method returns Collection, and its iterator can be used to iterate all the elements. The entrySet () method obtains the Set <Entry <xx, xx> Set object, and obtains all keys and values by traversing the Set object;

Vector Hashtable Stack is not recommended.

Author: yijichangkong

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.