Java Collection framework

Source: Internet
Author: User
Tags comparable

-- Start

Listarraylist

Arraylist is a variable array that has all the advantages and disadvantages of arrays, such as efficient random access and inefficient insertion and deletion. it allows repeated values, null, and Order (the so-called order refers to the order in which elements are read and inserted are consistent ).

Vector

In addition to synchronization, vector has no difference with arraylist because it is thread-safe and therefore inefficient. We recommend that you use arraylist as much as possible in Java.

Stack

Stack is a type of post-import, first-out data structure that inherits from the vector, so it is thread-safe. Due to design flaws, it is no longer recommended. deque is recommended.

Copyonwritearraylist

Copyonwritearraylist is a thread-safe arraylist. When we construct an iterator, it references the current array. If the array is modified, it still references the old array. this design is very useful for reading threads much more than writing threads.

Shortlist

An index list is a bidirectional list. It features efficient insertion and deletion, and inefficient random access. Therefore, do not use methods such as get (INT index) that contain index information. the problem is that sometimes we don't know whether a list is an arraylist or an external list. For this reason, JDK 1.4 introduces an interface randomaccess, which has no method, it is only used to mark whether a list supports efficient random access. Therefore, we can use the following code to determine whether a list supports efficient random access.

if (list instanceof RandomAccess) {...} else {...}

In addition, it allows repeated values, null, and order.

Set

Hashset

Hashset does not allow repeated values and null values. It can help us quickly find objects. The disadvantage is that we cannot control the order of objects.

Linkedhashset

Linkedhashset inherits hashset, which overcomes the disadvantages of hashset. We can iterate objects in the order they are inserted.

Treeset

Treeset does not allow repeated values. A null value is allowed. The objects contained in the treeset are sorted to form a tree structure. This is why treeset is called it. because it needs to be sorted, the objects to be added must implement the comparable interface or provide the comparator when creating the treeset. During the treeset iteration, We Can iterate in ascending order, it can also be iterated in descending order.

Concurrentskiplistset

Skip List is a data structure. It is expanded based on the ordered linked list to solve the difficulty of searching for specific values in the ordered linked list. Because the linked list cannot perform binary search like an array, in the worst case, we need to traverse the entire list to find the value we need. however, if each node stores the address of the second node, we can skip the intermediate node after the traversal, for example, directly jump from 1st nodes to 3rd nodes, in the worst case, we need to traverse n/2 + 1 nodes. if each node stores the last four node addresses, we can directly jump from 1st nodes to 5th nodes. In the worst case, we need to traverse N/4 + 1
Nodes, this is the idea of table jumping (Skip List), this is an idea of exchanging space for time.

Concurrentskiplistset does not allow repeated values or null values. It contains objects sorted and stored in the Skip List data structure. because it is sorted, the object to be added must implement the comparable interface or provide a comparator when creating the concurrentskiplistset ). in addition, it is thread-safe. We can iterate in ascending or descending order. However, if a value is modified later, the old value is still referenced during iteration.

Copyonwritearrayset

Copyonwritearrayset is a thread-safe set that uses copyonwritearraylist internally, so they all share the same features. When we build an iterator, it references the current array, if the array is modified, it still references the old array. this design is very useful for reading threads much more than writing threads.

Enumset

Enumset can only contain objects of some enumeration type, and does not allow repeated values or null values. The iteration sequence is consistent with the sequence defined in enumeration.

Map

Hashmap

Hashmap allows the null value and the null key. Its advantage is that it can help us quickly find objects. The disadvantage is that we cannot control the order of objects.

Linkedhashmap

Linkedhashmap inherits from hashmap. It overcomes the shortcomings of hashmap. We can follow the objectInsert sequenceOrAccess SequenceIt depends on the constructor you are using. when we use the following constructor to construct a linkedhashmap, each time an element is accessed, the element will be moved from the current position to the end of the linked list. We can know that, elements that are closer to the beginning of a linked list use the least, which is the legendary "least recent principle". This feature is very useful for high-speed caching. For example, we may want to put the most frequently accessed elements in the cache,
Instead, add the elements with no long access to the database.

LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
Hashtable

The main difference between hashtable and hashmap is that hashtable is synchronized by threads. In addition, hashtable does not allow null values and null keys.

Concurrenthashmap

Concurrenthashmap is thread-safe. It uses a very complex algorithm and never locks the entire table. Instead, it only locks a part of the table, thus improving concurrency. its iterator can only reflect the status of the build. If the data is modified after the iterator is created, the iterator still references the old data. in addition, it does not allow null values and null keys.

Weakhashmap

If the key object in map does not implement the equals method (that is to say, the action of the equals method is the same as that of =), if this key object is not found outside map, we will not be able to find this element in the map, but the Garbage Collector will not recycle this element. weakhashmap is designed to solve this problem. In this case, this element in weakhashmap will be recycled by the garbage collector. in addition, it is no different from hashmap.

Identityhashmap

In hashmap, it uses the equals method to determine whether the two keys are equal. In identityhashmap, it uses = to determine whether the two keys are equal. in addition, it is no different from hashmap.

Treemap

The keys of treemap are sorted and form a tree structure. This is why treemap is called treemap. because it needs to be sorted, the key to be added must implement the comparable interface or provide a comparator when creating a treemap ). in addition, it provides some key retrieval methods.

Concurrentskiplistmap

Skip List is a data structure. It is expanded based on the ordered linked list to solve the difficulty of searching for specific values in the ordered linked list. Because the linked list cannot perform binary search like an array, in the worst case, we need to traverse the entire list to find the value we need. however, if each node stores the address of the second node, we can skip the intermediate node after the traversal, for example, directly jump from 1st nodes to 3rd nodes, in the worst case, we need to traverse n/2 + 1 nodes. if each node stores the last four node addresses, we can directly jump from 1st nodes to 5th nodes. In the worst case, we need to traverse N/4 + 1
Nodes, this is the idea of table jumping (Skip List), this is an idea of exchanging space for time.

Concurrentskiplistmap is thread-safe and does not allow null keys and null values. Its keys are sorted and stored using the Skip List data structure, therefore, the key to be added must implement the comparable interface or provide the comparator when creating the concurrentskiplistmap. In addition, it also provides some key retrieval methods.

Enummap

Enummap does not allow the null key and the null value. Its key must be of an enumeration type.

Queue)

A queue is an FIFO data structure, but some JAVA Implementation classes of queue are not FIFO, such as priorityqueue and priorityblockingqueue. queue does not allow null values, because the poll method returns NULL to indicate that there are no elements in the queue.

Priorityqueue

Elements in the priorityqueue are sorted. Therefore, the elements added to this queue must implement the comparable interface or specify the comparator when constructing the priority queue. we can add elements in any order, but the smallest elements returned by methods such as poll, remove, and peek. it should be noted that when we iterate the priority queue, the iteration order is uncertain. second, it has no capacity limit and does not allow null. It is non-thread-safe. A typical example of using a priority queue is task scheduling. Each task has a priority. A task is added to the priority queue in any order, and a high priority is always taken out by the first task.

Concurrent1_queue

Concurrentlinkedqueue is a type of first-in-first-out data structure. It is thread-safe and has no capacity limit. It does not allow null values. its iterator can only reflect the status at build time. If the data is modified after the iterator is created, the iterator still references the old data.

Blockingqueue)

Java 1.5 has added blockingqueue, which greatly simplifies the workload of writing a classic producer-consumer thread. We no longer need to use wait, notifyall and other methods to synchronize producer and consumer threads. the producer thread only adds data to blockingqueue. If the blockingqueue capacity is full, the put method is blocked until there is capacity. the consumer thread only reads data from blockingqueue. If blockingqueue does not have data, the take method is blocked until there is data. obviously,
The producer thread needs to add a special data (such as EOF) after all the data is added to blockingqueue to tell the consumer that the thread data has been read, otherwise, the consumer thread will wait until it ends.

Arrayblockingqueue

Arrayblockingqueue is a blocking queue that uses arrays to store data at the underlying layer.

Linkedblockingqueue

Linkedblockingqueue is a blocking queue that uses the Linked List data structure to store data at the underlying layer.

Priorityblockingqueue

Priorityblockingqueue is a blocking queue. In addition to providing thread security and blocking, priorityblockingqueue and priorityqueue are very similar.

Delayqueue

Delayqueue is a special blocking queue. The elements added to it must implement the delayed interface. Only elements that expire can be taken out. The first element to be retrieved is the element with the longest expiration time.

Synchronousqueue

Synchronousqueue is a special blocking queue, which cannot hold any elements, and its insert operations will be blocked until another thread reads data, and vice versa. it is used to synchronously transmit data between multiple threads.

Linkedtransferqueue

The queue transferqueue is a special blocking queue. It can be used as the queue blockingqueue or synchronousqueue. therefore, it can be used to synchronously transmit data between multiple threads to asynchronously transmit data.

Deque)

Deque is also a type of queue. It is short for "double ended queue" and is read as "deck ". as the name suggests, we can insert and delete data at both ends of deque. it can be used as both a first-in-first-out queue and a later-in-first-out stack.

Arraydeque

Arraydeque is a deque that uses array storage at the underlying layer.

Shortlist

Shortlist is a deque stored in the linked list at the underlying layer.

Concurrentincludeque

In addition to thread security, it is similar to the producer list.

Linkedblockingdeque

Linkedblockingdeque is a blocking Double-end queue that uses linked list storage at the underlying layer.

Arrays and collections

These two classes are tool classes that contain many very practical static methods. The following two methods can help us to convert arrays and lists.

Arrays.asListList.toArray

 

If our set needs to be used in a multi-threaded environment, we can use the following method to wrap the set.

Collections.synchronizedCollectionCollections.synchronizedListCollections.synchronizedMapCollections.synchronizedSetCollections.synchronizedSortedMapCollections.synchronizedSortedSet

If we want to create a read-only set, we can use the following method to wrap the set.

Collections.unmodifiableCollectionCollections.unmodifiableListCollections.unmodifiableMapCollections.unmodifiableSetCollections.unmodifiableSortedMapCollections.unmodifiableSortedSet

If you want to sort the list, you can use the following method.

Collections.sortCollections.reverseOrderCollections.shuffle

In short, these two classes provide many very useful methods.

 

---For more information, see:Java
--Shengming: reprinted, please indicate the source
-- Last updated on 2012-10-24
-- Written by shangbo on 2012-09-19
-- End

Related Article

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.