Home  >  Article  >  Java  >  The difference between java map and list

The difference between java map and list

angryTom
angryTomOriginal
2019-11-18 11:24:145203browse

The difference between java map and list

The difference between java map and list

Map (mapping)

Map is a collection that maps key objects and value objects. Each element of it contains a key object and a value object. Map mainly has the following two implementation classes:

HashMap: HashMap is implemented based on a hash table. The cost of inserting and querying is fixed and can be adjusted by setting the capacity and load factor through the constructor. Container performance.

LinkedHashMap: Similar to HashMap, but when iterating through it, the order in which is obtained is its insertion order, or the least recently used (LRU) order.

TreeMap: TreeMap is implemented based on red-black trees. When viewing , they will be sorted. TreeMap is the only Map with a subMap() method, which can return a subtree.

List (list)

The elements of List are stored in a linear manner and can store repeated objects. List mainly has the following two implementation classes:

ArrayList: An array with variable length, which allows random access to elements. Inserting and deleting elements into ArrayList is slow. The implementation of ArrayList expansion in JDK8 is to use the statement newCapacity = oldCapacity (oldCapacity >> 1) (i.e. 1.5 times expansion) in the grow() method to calculate the capacity, and then call the Arrays.copyof() method to copy the original array.

LinkedList: Using linked list data structure, insertion and deletion are fast, but access speed is slow.

Compare List Map
Inherited interface Collection
Common implementation classes AbstractList (its common subclasses include ArrayList, LinkedList, and Vector) HashMap, HashTable
Common methods add( ), remove( ), clear( ), get( ), contains( ), size ( ) put( ), get( ), remove( ), clear( ), containsKey( ), containsValue( ), keySet( ), values( ), size( )
Element Repeatable Non-repeatable
Sequence Ordered
Thread safety Vector thread safety Hashtable thread safety
##php Chinese website, a large number of free

Java introductory tutorials, welcome to learn online!

The above is the detailed content of The difference between java map and list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn