Development of several commonly used data types in Java

Source: Internet
Author: User
Tags array length wrapper wrappers

Data structures commonly used in Java (java.util.)

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 main relationships (inheritance relationships) are: (----See the Java API documentation in detail!) )

Collection---->collections MAP----->sortedmap------>treemap

Collection---->list-----> (Vector \ arrylist \ LinkedList) M AP------>hashmap

Collection---->set------> (HashSet \ linkedhashset \ SortedSet)

--------------Collection----------------

1, collections

API----This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a spec Ified collection, and a few other odds and ends.

The methods of this class all throw a nullpointerexception if the collections or class objects provided to them a Re null.

2. List

API----This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a spec Ified collection, and a few other odds and ends.

The methods of this class all throw a nullpointerexception if the collections or class objects provided to them a Re null.

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 located in the list, similar to an array of >), similar to an array of java.

3. Vector

API----The Vector class implements a growable array of objects. Like a array, it contains components that can is accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the have Vector been Created.

Array-based list, in fact, encapsulates the array does not have some of the features convenient for us to use, so it is difficult to avoid the limitations of the array, while the performance can not be beyond the array. So, if possible, we need to use arrays more. Another important point is that vectors are thread-synchronized (sychronized), which is one of the important differences between vectors and ArrayList.

4, ArrayList

API----Resizable-array Implementation of the List interface. Implements all optional The list operations, and permits all elements, includingnull. In addition to implementing theList interface, this class provides methods to manipulate the size of the array th At are used internally to store the list. (This class was roughly equivalent to theVector, except that it was unsynchronized.)

The same vector is an array-based list, but the difference is that ArrayList is not synchronous. So it's better to be more performance than a vector, but when running into a multithreaded environment, you need to manage your thread's synchronization problems.

5, LinkedList

API----Linked List Implementation of the list interface. Implements all optional The list operations, and permits all elements (includingnull). In addition to implementing theList interface, the LinkedList class provides uniformly named methods to< C6>get, remove andinsert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack,queue, ordouble-ended queue.

LinkedList differs from the previous two list, which is not array-based, so it is not limited by the performance of the array.
Each node of it contains two things:
1. Data of the node itself;
2. Information for the next node (nextnode).
So when adding to LinkedList, deleting an action does not have to be a lot of data movement like an array-based ArrayList. As long as you change the relevant information NextNode can be achieved, this is the LinkedList advantage.

List Summary:

    • All lists can contain only a single table of different types of objects, not key-value key-value pairs. Example: [Tom,1,c]

    • All lists can have the same elements, such as vector can have [Tom,koo,too,koo]

    • All lists can have null elements, for example [tom,null,1]

Array-based list (vector,arraylist) is suitable for querying, while LinkedList is suitable for adding, deleting operations

6. Set (interface)

API-----A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1and e2such that e1.equals(e2), and at the most one null element. As implied by it name, this interface models the mathematical SetAbstraction.

Set is a collection that contains no repeating elements

7, HashSet

API-----This class implements theSet interface, backed by a hash table (actually aHashMap instance). It makes no guarantees as to the iteration order of the set; In particular, it does isn't guarantee that the order would remain constant over time. This class permits thenull element.

Although both set and list implement the collection interface, they are implemented in a very different way. The list is basically based on an array. But set is implemented on the basis of HashMap, which is the fundamental difference between set and list. The hashset is stored in the HashMap key as the corresponding storage of the set. Look at the implementation of the HashSet's add (Object obj) method to see it at a glance. 8. Linkedhashset API----Linked List implementation of theListInterface. Implements all optional list operations, and permits all elements (includingNULL). In addition to implementing theListInterface, theLinkedListclass provides uniformly named methods toGet,RemoveandInsertAn element at the beginning and end of the list. These operations allow linked lists to be used as a stack,queue, ordouble-ended queue.

HashSet a sub-class, a linked list.

9, SortedSet

API---A, Set further provides a totalordering on its elements. The elements is ordered using theirnatural ordering, or by a Comparator typically provided at sorted set creation time. The set ' s iterator would traverse the set in ascending element order. Several additional operations is provided to take advantage of the ordering. (This interface is the set analogue of SortedMap .)

The ordered set is implemented by SortedMap.

Set Summary:

(1) The set implementation is based on the elements in Map (HASHMAP) (2) set that cannot be duplicated, and if an existing object is added using the Add (Object obj) method, the previous object is overwritten

-------------Map-----------------

A map is a container that associates a key object with a value object, and a value object can be a map, and so on, so that a multilevel map can be formed. For a key object, like set, a key object in a map container does not allow repetition, which is to keep the consistency of the search results, and if there are two key objects, then you have a problem with the value object that the key object has, and you may not get the value object you want, and the result will be confusing. So the uniqueness of the key is very important, and it conforms to the nature of the set. Of course, in the process of use, the value object corresponding to a key may change, then the last modified value object corresponds to the key. There is no unique requirement for a value object, and you can map any number of keys to a value object without any problems (although it may be inconvenient for you to use it, you do not know what you are getting for the value object that corresponds to that key).

1, HashMap

API----Hash table based implementation of theMapInterface. This implementation provides all of the optional map operations, and permitsNULLValues and theNULLKey. (TheHashMapClass is roughly equivalent toHashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; In particular, it does isn't guarantee that the order would remain constant over time.

2, TreeMap

API----A Red-black tree based NavigableMapimplementation. The map is sorted according to Thenatural ordering of their keys, or by a ComparatorProvided at map creation time, depending to which constructor is used.

TreeMap is stored in the order of keys, so it has some extended methods, such as Firstkey (), Lastkey (), and so on, you can also specify a range from TreeMap to get its child map.
The association of Keys and values is simple, and a key can be associated with a value object using the put (Object Key,object value) method. Use Get (object key) to get the value object corresponding to this key object.

 

------------Description-----------

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.

Development of several commonly used data types 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.