A brief analysis of collection packages in Java (Arraylist,linkedlist,vector, Stack,hashset,treeset,hashmap,treemap) __java

Source: Internet
Author: User

The collection package is the most commonly used package in Java, and it is most commonly used with implementation classes for collection and map two interfaces, where collection is used to hold multiple single objects, and map is used to store key-value pairs in key-value form.

There are two types of interfaces commonly used in collection: List and set, the most obvious difference is that the list supports placing duplicate objects, and set does not support. The list interface commonly used implementation classes are: Arraylist,linkedlist,vector and Stack;set interface commonly used implementations have hashset,treeset. And the common implementation of map is TreeMap and HashMap.

1. ArrayList is implemented on an array basis, with no capacity limitations.

2. ArrayList may have to expand when executing the Insert element, and deleting the array does not reduce the size of the array (if you want to reduce the capacity of the array appropriately, you can call ArrayList's TrimToSize ()), and you need to traverse the array when looking for elements. For non-null elements to be taken in the same way as equals.

3.ArrayList is non thread safe.

1. LinkedList is based on the bidirectional linked list mechanism.

2. LinkedList when inserting an element, you must create a new entry object and toggle the reference of the elements before and after the corresponding element, and when you look up an element, traverse the list, iterate through the list, locate the element you want to delete, and then remove the element from the list.

3. LinkedList is not thread safe.

From the difference between LinkedList and ArrayList, we can easily draw when to use the ArrayList and when to use LinkedList:

1 for random access get and set,arraylist is better than LinkedList because linkedlist to move the pointer.
2 for new and delete operations Add and remove,linedlist more advantageous, because ArrayList to move the data.

Third, Vector

And ArrayList's different points have

1 vector is the ArrayList of thread safety based on synchronized implementation. That vector is thread-safe.

2 The mechanism and ArrayList of capacity expansion are slightly different when inserting elements, the vector is twice times larger and the expansion of capacity can be controlled by incoming capacityincrement. and ArrayList is expanded 1.5 times times and plus 1.

Four, Stack

stack inheritance and vector, based on the vector to achieve the stack required by the LIFO (LIFO) of the press operation, which provides a push,pop,peek three main methods.

Five, HashSet

1. HashSet based on HASHMAP implementation, no capacity constraints.

2. HashSet is not thread safe.

3. HashSet does not guarantee order.

VI. TreeSet

1, TreeSet based on TREEMAP implementation, support sorting.

2, TreeSet is not thread-safe.

From the descriptions of HashSet and TreeSet, TreeSet and HashSet are also completely based on the map and do not support getting (int) to get the element at the specified location (which requires traversal fetching), and TreeSet provides some sort of support. Examples include incoming comparator implementations, Descendingset, and Descendingiterator.

Seven, HashMap

1, the HashMap uses the array way to store the Key,value composition entry object, does not have the capacity limit.

2, HashMap based on the key hash to find the entry object to the location of the array, for the hash conflict using a linked list of methods to solve.

3. HashMap may want to enlarge the capacity of the array when inserting elements, it is necessary to recalculate the hash when enlarging the capacity, and copy the object into the new array.

4, HashMap is not thread-safe.

5, HashMap traversal use is iterator

Eight, HashTable

1, Hashtable is thread-safe.

2, Hashtable, whether the key, or value is not allowed to null.

3. The Hashtable traversal uses the enumeration.

Once Hashtable applications are very extensive, hashmap is used in the new framework to replace the Hashtable class, that is, recommend the use of HashMap, do not use Hashtable, you can use Concurrenthashmap. So what's the difference between the few people?

1, hashtable internal storage structure

Hashtable and HashMap adopt the same storage mechanism, the two implementations are basically consistent and different:

1), HashMap is not thread-safe, Hashtable is thread-safe, the internal method is basically synchronized.

2), Hashtable does not allow the existence of null values.

When the Put method is invoked in Hashtable, if the key is null, the NullPointerException is thrown directly. Other subtle differences, such as initializing the size of the entry array, and so on, but the basic idea is the same as HashMap.

2, Comparison of Hashtable and Concurrenthashmap

Concurrenthashmap is currently the best thread-safe hashmap implementation. It is also a thread-safe class that differs from Hashtable in terms of synchronization.

Hashtable use the Synchronized keyword to ensure synchronization, and synchronized is actually a lock on the object, whether you add synchronized before the method or statement block, the lock is the object as a whole, But the Concurrenthashmap synchronization mechanism is different from this, it is not the addition of synchronized keyword, but based on the lock operation, the purpose is to ensure synchronization, the lock is not the whole object. In fact, Concurrenthashmap can meet concurrentlevel threads and non-blocking operations collection objects.

It is important to note that we must maintain sufficient attention to HashMap's thread-safe in our specific project development, and if we do not maintain enough synchronization in the concurrency scenario, it is possible to run the hashmap.get into a dead loop and consume the CPU to 100%.

1, TreeMap is a typical map based on the red-black tree implementation, so it requires a key to compare the method, either in the comparator implementation, or the Key object implementation comparable interface.

2, TreeMap is not thread-safe.

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.