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
LinkedHashMap: Similar to HashMap, but when iterating through it, the order in which
TreeMap: TreeMap is implemented based on red-black trees. When viewing
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 |
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!