Java Collection Class Rollup

Source: Internet
Author: User

1. List (ordered, repeatable)
List of objects are ordered, but also can be repeated, list is concerned about the index, has a series of index-related methods, query fast. Since inserting or deleting data into the list collection is accompanied by the movement of the subsequent data, all insertions are slow to delete data.

2. Set (unordered, cannot repeat)
The objects stored in the set are unordered and cannot be duplicated, and the objects in the collection are not sorted in a particular way, simply by adding the object to the collection.

3. MAP (key value pair, key unique, value not unique)
The Map collection stores key-value pairs, keys cannot be duplicated, and values can be duplicated. According to the key to get the value, the map set to traverse the first get the set set of keys, the set set to traverse, to get the corresponding value.

The comparison is as follows:

 

 

is ordered

Whether elements are allowed to repeat

Collection

List

Is

Is

Set

Abstractset

Whether

Whether

 

HashSet

 

TreeSet

Yes (with binary sort tree)

Map

Abstractmap

Whether

Use Key-value to map and store data, key must be unique, value can be repeated

 

HashMap

 

TreeMap

Yes (with binary sort tree)

Four, traverse

The following four common output methods are available in the class set:

1) Iterator: Iterative output, is the most used output mode.

2) Listiterator: is the sub-interface of the iterator, specifically used to output the contents of the list.

3) foreach output: A new feature provided after JDK1.5 that allows you to output an array or a collection.

4) for Loop

The code examples are as follows:

For the form: for (int i=0;i<arr.size (); i++) {...}

foreach form: for (int i:arr) {...}

form of iterator:
Iterator it = Arr.iterator ();
while (It.hasnext ()) {object o =it.next (); ...}

ArrayList, LinkedList, vector of the general difference

1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list.
2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer.
3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.

1. In terms of thread safety,
Vector is thread safe, ArrayList linkedlist thread is unsafe.


2, the bottom realization

Vector ArrayList is an array, and LinkedList is a doubly linked list.

Type Difference HashMap (Thread not secure)

In a multithreaded environment, a put operation using hashmap causes a dead loop, which results in a CPU utilization of nearly 100%, so the hashmap cannot be used in a concurrency scenario.

The most commonly used map, which stores data according to the hashcode value of the key, can get its value directly according to the key, with fast access speed. HashMap allows a maximum of one record's key to be null (multiple entries are overwritten), and the value to allow multiple records to be null. is not synchronized.

TreeMap

The records that can be saved are sorted by key (key), by default in ascending order, or by a sort comparer, and when traversing treemap with iterator, the resulting records are sorted out. TreeMap does not allow the value of key to be null. is not synchronized.

Hashtable (Thread Safety)

Hashtable containers use synchronized to ensure thread safety, but Hashtable is inefficient in the event of intense online competition

Similar to HashMap, the value of key and value are not allowed to be null, it supports thread synchronization, that is, only one thread can write hashtable at any one time, so it also causes the Hashtale to be slower to write.

Linkedhashmap

The insertion Order of the records is saved, and the first record is inserted first when traversing the linkedhashmap with iterator. It is slower than hashmap when traversing. Both the key and value are allowed to be empty, non-synchronous.

concurrenthashmap Lock Segment Technology

Because all the threads that access Hashtable must compete for the same lock, if there are multiple locks in the container, each lock is used to lock some of the data in the container, there is no lock contention between threads when accessing data from different data segments in the container. This can effectively improve the efficiency of concurrent access, which is the lock segmentation technology used by Concurrenthashmap,

First, the data is divided into a section of storage, and then to each piece of data with a lock, when a thread occupies a lock to access one of the data, other segments of the data can also be accessed by other threads.

Java Collection Class Rollup

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.