Java Collection framework--list, set, Map

Source: Internet
Author: User

Java Collection framework interfaces and hierarchies of Classes:

java.util.Collection [I]+--java.util.List [I]+--java.util.ArrayList [C]+--java.util.LinkedList [C]+--Java.util.Vector [C]+--Java.util.Stack [C]+--Java.util.Set [I]+--Java.util.HashSet [C]+--Java.util.SortedSet [I]+--Java.util.TreeSet [c]java.util.map [I]+--Java.util.SortedMap [I]+--Java.util.TreeMap [C]+--java.util.Hashtable [C]+--Java.util.HashMap [C]+--Java.util.LinkedHashMap [C]+--java.util.weakhashmap [C]

Note: [I] represents the interface, [C] represents the class

Collection interface

Collection is the most basic set interface, and a collection represents a set of objects that are called collection Elements.

All classes that implement the collection interface must provide two standard constructors: a parameterless constructor is used to create an empty collection, and a constructor with a collection parameter is used to create a new Collection. This new collection has the same elements as the incoming Collection. The latter constructor allows the user to copy a Collection.

How do I traverse every element in the collection? Regardless of the actual type of collection, it supports a iterator () method that returns an iteration that uses the iteration to access each element of the collection one at a Time. Typical usage is as follows:

while/ / }  

Depending on the purpose, collection is divided into lists and Sets.

List interface
The list inherits from the collection Interface. The list is an ordered collection, using this interface to precisely control where each element is Inserted. The user is able to access the elements in the list using an index (where the element is positioned in the list, similar to an array subscript), similar to an array of Java.

Unlike set sets, the list allows repeating elements. for E1 and E2 object elements that meet the e1.equals (e2) condition, they can exist in the list collection at the same time. Of course, There is also a list implementation class that does not allow the existence of duplicate Elements.
In addition to the iterator () method, which has the collection interface prerequisites, The list also provides a Listiterator () method that returns a Listiterator interface, compared to the standard iterator interface. Listiterator has a number of add () methods that allow you to add, delete, set elements, and traverse forward or Backward.

The common classes that implement the list interface are Linkedlist,arraylist,vector and stacks.

LinkedList class
The LinkedList implements a list interface that allows null Elements. Additionally LinkedList provides an additional Get,remove,insert method at the first or the tail of the Linkedlist. These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque).

Note LinkedList does not have a synchronization method. If multiple threads access a list at the same time, you must implement Access synchronization Yourself. One workaround is to construct a synchronized list when the list is created:

List List = Collections.synchronizedlist (new LinkedList (...));

ArrayList class
ArrayList implements a Variable-size array. It allows all elements, including Null. ArrayList is not Synchronized.

Size,isempty,get,set method Run Time is Constant. But the Add method cost is the allocated constant, and adding n elements requires an O (n) time. Other methods run at a linear time.

Each ArrayList instance has a capacity (capacity), which is the size of the array used to store the Elements. This capacity automatically increases as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to improve insertion Efficiency.

Like linkedlist, ArrayList is also unsynchronized (unsynchronized).

Note ArrayList does not have a synchronization method. If multiple threads access a list at the same time, you must implement Access synchronization Yourself. One workaround is to construct a synchronized list when the list is created:

List List = Collections.synchronizedlist (new ArrayList (...));

Vector class
vectors are very similar to arraylist, but vectors are synchronous. the iterator created by the vector, although the iterator created by the ArrayList is the same interface, but because the vector is synchronous, when a iterator is created and is being used, Another thread changed the state of the vector (for example, adding or removing some elements), and when the method called iterator would throw concurrentmodificationexception, the exception must be caught.

Synchronizedlist and Vector differences
by Collections.synchronizedlist (...) Provides a mechanism for linkedlist/arraylist thread safety, similar to vectors, so that the difference between synchronizedlist and vectors is that the ArrayList is different from the increment speed of the vector, so when a thread-safe operation is required, We recommend using vectors when the increments are faster, and nothing else. note, however, that synchronizedlist requires the developer to add the thread lock control code to the Iteration.


Stack class
stack inherits from Vector and implements a last-in-first-out Stack. the stack provides 5 additional ways to make the vector available as a stack. The basic push and pop methods, and the Peek method to get the stack top element, the empty method tests if the stack is empty, and the search method detects the position of an element on the Stack. Stack has just been created as an empty stack.
Xxxxx

Java Collection framework--list, set, Map

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.