Data Structure in Java

Source: Internet
Author: User

There are so many Java class libraries that many people do not know much about, and the results are always made on their own.

The following summarizes some data structures in Java and some implementation analysis.

As for the time complexity, I personally feel that writing is of little use. If you understand how it is implemented, you will naturally know its time complexity.

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


Interface:

Collection <E>

Sub-interface:

Blockingdeque <E>, blockingqueue <E>, deque <E>, list <E>, navigableset <E>, queue <E>, set <E>, sortedset <E>

Implementation class:

Queue, arraydeque, arraylist, concurrent1_queue, queue, delayqueue, enumset, hashset, inclublockingdeque, inclublockingqueue, incluhashset, inclulist, operator, stack, operator, treeset, Vector


List <E>

Implementation class:

Arraylist, copyonwritearraylist, rule list, stack, Vector


Queue <E>

Sub-interface:

Blockingdeque <E>, blockingqueue <E>, deque <E>

Implementation class:

Arrayblockingqueue, arraydeque, concurrentincluqueue, delayqueue, inclublockingdeque, inclublockingqueue, inclulist, 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 thread security

Concurrent, copyonwrite, and blocking are usually thread-safe, but these thread security is usually conditional, so you must carefully read the documentation before use.



Specific implementation:

List <E> series:

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

Copyonwritearraylist <E>, which contains a reentrantlock. Each time it is added, all elements are locked and copied to a new array.

It is thread-safe to only ensure that the operation is passed through, and the get operation is not guaranteed. That is to say, if the size is obtained first and then get (size-1) is called, it may become invalid.

So how does copyonwritearraylist implement thread-safe iterative operations?

Save the original array in the iterator.

Linkedlist <E>, standard two-way linked list

Vector <E>, out of date. Synchronized is added to most methods.

Stack <E>, inherited from vector, obsolete. deque <integer> stack = new arraydeque <integer> () is preferred ();


Queue <E> series:

Lists <E>. For more information, see list <E> series.

Arraydeque <E>, which contains an array of int type head and tail elements.

Priorityqueue <E>, an array is used internally to save elements, but the array is organized in the form of a heap, so it is ordered.

Priorityblockingqueue <E>, which encapsulates a priorityqueue <E>, A reentrantlock, a condition, and todo

Arrayblockingqueue <E>, todo

Concurrent1_queue <E>, todo

Delayqueue <E>, todo

Define blockingdeque <E>, todo

Linkedblockingqueue <E>, todo

Synchronousqueue <E>, todo


Deque <E> (dual-end Queue) series:

Arraydeque <E>, see the queue Series

Upload list <E>, see list Series

Define blockingdeque <E>, todo



Set series:

Hashset, which encapsulates a hashmap:

Public hashset (){

Map = new hashmap <E, Object> ();

}

Treeset, which encapsulates a treemap. For details, refer to hashset.

Linkedhashset, which encapsulates linkedhashmap. For details, refer to hashset.

Enumset, todo

Copyonwritearrayset is a simple package of copyonwritearraylist. Pay attention to the time complexity of get of this set.

Concurrentskiplistset, which encapsulates a concurrentskiplistmap. For more information, see hashset.



Map series:

Hashmap <K, V>, standard link address method implementation

Treemap <K, V>, red/Black Binary Tree

Linkedhashmap <K, V>: add the before and after pointers in the entry to concatenate the elements in the hashmap. In this way, you can repeat them in the insert sequence during iteration.

Enummap, todo

Concurrenthashmap, refer to previous articles

The time complexity of concurrentskiplistmap, todo, and log (n) is a bit like that saved by a multi-level linked list. It looks like the implementation of sortedset in redis.

Hashtable, obsolete

Identityhashmap. In normal hashmap, The equals method is used. This is a "=" comparison character.

Weakhashmap <K, V>, weak referenced hashmap. Normal hashmap is a strong reference, that is, the values in it are not collected by GC. In weakhashmap <K, V>, in V, it is better to use the weakreference type with code like this: M. put (Key, new weakreference (value )).



Other useful third-party data structures:

Lrucache, longhashmap, linkedtransferqueue in Java 7,

The Apache package contains many practical classes:

Http://commons.apache.org/collections/

Google's package contains many concurrent niub categories:

Atomiclongmap, etc.

Big Object Data Structure: https://github.com/HugeCollections/Collections

Note:

Most concurrent containers cannot use null values.

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.