Summary of data structures in Java

Source: Internet
Author: User

Java's class library is so many that many people don't understand it, and the result is that they always build their own wheels.

The following summarizes some of the data structures in Java, plus some implementations of the analysis, as well as the memo.

As for the complexity of time, the personal feel that the use of writing is not very useful. If you understand how it is achieved, it is natural to know its time complexity.

If you do not understand its implementation, it is useless to memorize the complexity of time.

Interface:

Collection<e>

Sub-Interface:

Blockingdeque<e>, Blockingqueue<e>, Deque<e>, List<e>, Navigableset<e>, Queue<E , Set<e>, sortedset<e>

Implementation class:

Arrayblockingqueue, Arraydeque, ArrayList, Concurrentlinkedqueue, Concurrentskiplistset, Copyonwritearraylist, Copyonwritearrayset, Delayqueue, Enumset, HashSet, Linkedblockingdeque, Linkedblockingqueue, Linkedhashset, LinkedList, Priorityblockingqueue, Priorityqueue, Stack, Synchronousqueue, TreeSet, Vector

List<e>

Implementation class:

ArrayList, Copyonwritearraylist, Linkedlist,stack, Vector

Queue<e>

Sub-Interface:

Blockingdeque<e>, Blockingqueue<e>, deque<e>

Implementation class:

Arrayblockingqueue, Arraydeque, Concurrentlinkedqueue, Delayqueue, Linkedblockingdeque, LinkedBlockingQueue, LinkedList, Priorityblockingqueue, Priorityqueue, Synchronousqueue

Set<e>

Sub-Interface:

Navigableset<e>, sortedset<e>

Implementation class:

Concurrentskiplistset, Copyonwritearrayset, Enumset, HashSet, Linkedhashset, TreeSet

Map<k,v>

Sub-Interface:

Concurrentmap<k,v>, Concurrentnavigablemap<k,v>, sortedmap<k,v>

Implementation class:

Concurrenthashmap, Concurrentskiplistmap, Enummap, HashMap, Hashtable, Identityhashmap, Linkedhashmap, TreeMap, Weakhashmap

Concurrency and line Cheng, etc.

Typically, concurrent,copyonwrite,blocking is thread-safe, but these thread safety is usually conditional, so be sure to read the document carefully before using it.

Specific implementation:

List<e> Series:

Arraylist<e>, as its name is, but its capacity growth plan is newcapacity = (oldcapacity * 3)/2 + 1, and C + + the usual vector is the doubling strategy of the different.

Copyonwritearraylist<e>, there is a reentrantlock in it, each time the add is locked, all the elements are copied to a new array.

Only ensure that the calendar operation is thread-safe, the get operation is not guaranteed, that is, if you get the size first, then call Get (size-1), it may be invalidated

So how does copyonwritearraylist implement thread-safe iterative operations?

Saves the original array in the iterator.

Linkedlist<e>, standard bidirectional linked list

Vector<e>, outdated, most methods added synchronized

Stack<e>, inherit from vector, obsolete, preferably should use deque<integer> stack = new arraydeque<integer> ();

Queue<e> Series:

Linkedlist<e>, see List<e> Series

Arraydeque<e>, an array is used internally to preserve elements, with int type head and tail.

Priorityqueue<e>, an array is used internally to hold the elements, but the arrays are organized in the form of heaps and are therefore ordered.

Priorityblockingqueue<e>, wrapped up a priorityqueue<e> a reentrantlock, a Condition,todo

Arrayblockingqueue<e>,todo

Concurrentlinkedqueue<e>,todo

Delayqueue<e>,todo

Linkedblockingdeque<e>,todo

Linkedblockingqueue<e>,todo

Synchronousqueue<e>,todo

Deque<e> (double-ended queue) series:

Arraydeque<e> see Queue series

LINKEDLIST<E> See List series

Linkedblockingdeque<e>,todo

Set Series:

HashSet, wrapped up a hashmap:

Public HashSet () {

Map = new hashmap<e,object> ();

}

TreeSet, wrapped up a treemap, reference hashset

Linkedhashset, packaged linkedhashmap, reference HashSet

Enumset,todo

Copyonwritearrayset, simply wrapping the copyonwritearraylist, note the time complexity of the get of this set.

Concurrentskiplistset, packaging a concurrentskiplistmap, refer to HashSet.

Map series:

Hashmap<k,v> The standard chain address method is implemented

Treemap<k,v>, red and black two-prong tree

Linkedhashmap<k,v> in entry, add before and after pointers to concatenate the elements in the HashMap so that, in the iteration, it is possible to iterate through the insertion order.

Enummap,todo

Concurrenthashmap, refer to the previous article

Concurrentskiplistmap,todo,log (n) time complexity, a bit like multilevel chain table preservation, seemingly a bit like the implementation of SortedSet in Redis

Hashtable, outdated

Identityhashmap, the normal hashmap is compared with the Equals method, which uses the "= =" comparison character

Weakhashmap<k,v>, weakly quoted HashMap, normal HashMap is a strong reference, that is, the value inside is not recycled by GC, in Weakhashmap<k,v>, V is preferably a weakreference type, with code like this: M.put (key, new WeakReference (value)).

Some of the other useful third-party data structures:

Lrucache,longhashmap,java7 in the LinkedTransferQueue,

Apache's package, which has a lot of useful classes:

http://commons.apache.org/collections/

Google's package, there are a lot of concurrent cattle B class:

Atomiclongmap, wait.

Data structures for large objects: Https://github.com/HugeCollections/Collections

Precautions:

Most concurrent containers cannot use null values

Summary of data structures 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.