Several common data structures in Java

Source: Internet
Author: User

Java has several commonly used data structures, mainly divided into collection and map two main interface (interface only provides methods, does not provide implementation), and the final data structure used in the program is inherited from these interfaces data structure class.

the difference between a few common classes
1. ArrayList: single element, high efficiency, more for querying
2. Vector: element single, thread safe, more for querying
3. LinkedList: Element single, multiple for insert and delete
4. HashMap: element is paired, element can be empty
5. HashTable: Element pair, thread safe, element not empty
ii. vectors, ArrayList and LinkedList
Most of the time, the performance of the ArrayList is best, but when the elements in the collection need to be inserted frequently, delete LinkedList will have a better performance, but they are three performance than the array, and the vector is thread synchronization. So:
If you can use an array (the element type is fixed and the array length is fixed), try using an array instead of a list;
If there is no frequent deletion of the insert operation, but also do not consider multithreading problems, the preferred choice of ArrayList;
If used in multi-threaded conditions, vector can be considered;
If you need to delete inserts frequently, LinkedList has a place to go;
If you don't know anything, use ArrayList right.
Iii. Collections and Arrays
In the Java Collection Class framework there are two classes called collections (note, not collection! and arrays, which is a powerful tool inside JCF, but beginners tend to ignore it. According to the JCF documentation, these two classes provide wrapper implementations (Wrapper implementations), data structure algorithms, and array-related applications.
Presumably you won't forget the classic algorithms of "binary find" and "sort" mentioned above, and the collections class provides a rich static approach to help us do these annoying tasks in the data structure class:
BinarySearch: Binary Find.
Sort: Sorting, here is a method similar to the quick sort, the efficiency is still O (n * log n), but it is a stable sorting method.
Reverse: the linear table in reverse order, this is the previous data structure of the classic questions Oh!
Rotate: "Rotates" the linear table with an element as its axis.
Swap: Swaps the position of two elements in a linear table.
......
Another important feature of collections is the "wrapper" (Wrapper), which provides a way to convert a collection into a special set, as follows:
Unmodifiablexxx: Converted to a read-only collection, where xxx represents six basic set interfaces: Collection, List, Map, Set, SortedMap, and SortedSet. If you insert the delete operation on a read-only collection, the Unsupportedoperationexception exception will be thrown.
Synchronizedxxx: Converts to a synchronous collection.
Singleton: Creates a collection of only one element, where Singleton generates the cell set,
Singletonlist and Singletonmap each generate a single-element list and map.
Empty set: Represented by collections static properties Empty_set, Empty_list, and Empty_map.

Reprint: http://blog.csdn.net/u010947402/article/details/51878166

Several common 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.