List, Map, set the difference and contact

Source: Internet
Author: User

List, Map, set the difference and contact

First, structural characteristics

List,set are inherited from the collection interface, map is not;
List and set are a collection of single-column data, and map is a collection of two columns of data that store key-value pairs;
The data stored in the list is sequential and the values are allowed to be duplicated, the data stored in the map is unordered, its keys are not allowed to be duplicated, but the values are allowed to be duplicated;
The data stored in set is not sequential and is not allowed to be duplicated, but the position of the element in the collection is determined by the hashcode of the element, i.e. the position is fixed (the set is stored according to the hashcode, so the position is fixed, but this position is not controlled by the user,
So the elements in set are unordered for the user.

Second, the realization of the class


The list interface has three implementation classes:
1.1 LinkedList
Based on the chain list implementation, the list of memory is hashed, and delete quickly, find slow;
1.2 ArrayList
Based on the array implementation, non-thread-safe, high efficiency, adding and deleting slow, Find Fast;
1.3 Vector
Based on the implementation of the array, thread safety, low efficiency, adding and deleting slow, find slow;
The map interface has four implementation classes:
2.1 HashMap
Hash table based MAP interface implementation, non-thread-safe, efficient, null-valued and null-enabled
Key
2.2 HashTable
Thread-safe, inefficient, null values and NULL keys are not supported;
2.3 Linkedhashmap
is a subclass of HashMap that preserves the order in which records are inserted;
2.4 Sortmap Interface
TreeMap, the ability to sort its saved records by key, by default is the ascending sort of the key value
The set interface has two implementation classes:
3.1 HashSet
The bottom layer is implemented by the hash Map, does not allow duplicate values in the collection, the use of this method needs to rewrite the Equals () and the Hash Code () method;
3.2 Linkedhashset
Inherited from the HashSet, and also based on the Linkedhashmap to implement, the underlying use is Linkedhashmap

Three, the difference

1. Objects in the list collection are sorted by index location and can have duplicate objects that allow objects to be retrieved by object at the index of the collection, for example, by List.get (i) method to get the elements in the collection;
2. Each element in the MAP contains a key and a value, in pairs, the key object can not be duplicated, the value object can be repeated;
3. The objects in the set collection are not sorted in a particular way, and there is no duplicate object, but its implementation class can sort the objects in the collection in a particular way, such as the Tree Set class, either by default or by implementing the java.util.comparator< Type > Interfaces customize how they are sorted.




Iv. additions: HashMap and HashTable

HashMap is thread insecure, HashMap is an interface, is a map of a sub-interface, is to map the key to the worthy object, do not allow key value duplication, allow null key and null value, because of non-thread security, HASHMAP efficiency is more efficient than HashTable.
HashTable is a thread-safe collection that does not allow null values as a key value or value;
HashTable is sychronize (synchronous), multiple threads do not need to implement their own methods to achieve synchronization, and HashMap is accessed by multiple threads need to implement their own method of synchronization;

List, Map, set the difference and contact

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.