What is the difference between a list and a map in Java? List, Set, does map inherit from collection interface? __java

Source: Internet
Author: User

Whether the list,set,map inherits from the collection interface.


A: List,set is, map is not. Collection is the most basic set interface, and a collection represents a set of object, that is, the collection element. Some collection allow the same elements while others do not. Some can be sorted and others not. The Java JDK does not provide classes that directly inherit from collection, and the Java JDK provides classes that inherit from collection, such as list and set.
Note: The map does not inherit the collection interface, and map provides a mapping of key to value. A map cannot contain the same key, and each key can map only one value. The map interface provides a view of 3 collections, which can be used as a set of key sets, a set of value sets, or a set of key-value mappings.


List saves objects in the order in which they are entered, without sorting or editing operations.

Set accepts only once for each object and uses its own internal sorting method (usually, you only care about whether an element belongs to the set, not the order of it--otherwise you should use the list).

Map also saves one copy of each element, but this is based on the "key", and the map has a built-in sort, and therefore does not care about the order in which the elements are added. If the order in which you add elements is important to you, you should use Linkedhashset or Linkedhashmap.



Detailed Introduction:
List features: elements are placed in order, elements can be repeated


Map Features: element key value to store, no put in order


Set features: Elements are not placed in order, elements can not be repeated (note: Although the element is not put in order, but the position of the element in the set is the hashcode of the element is determined, its position is actually fixed)


The list interface has three implementation classes: Linkedlist,arraylist,vector


LinkedList: The bottom is based on the linked list implementation, the list memory is scattered, each element stores its own memory address while also storing the address of the next element. Linked list additions and deletions quickly, find slow


The difference between ArrayList and vectors: ArrayList is not thread safe, high efficiency, vector is thread-safe, inefficient


The set interface has two implementation classes: HashSet (the bottom is implemented by HashMap), Linkedhashset


The SortedSet interface has an implementation class: TreeSet (bottom layer is implemented by a balanced binary tree)


The query interface has an implementation class: Linklist


The map interface has three implementation classes: Hashmap,hashtable,linkehashmap


HashMap is not thread safe, efficient, and supports null;

Hashtable is thread safe, inefficient, and does not support null
SortedMap has an implementation class: TreeMap


In fact, the most important thing is that the list is used to process the sequence, and set is used to deal with the set. The map is known to store the key value pairs
Set is generally unordered and not repeated. MAP KV structure list ordered.

the function method of list

There are actually two kinds of lists: One is the basic ArrayList, the advantage is the random access element, the other is the more powerful linkedlist, it is not designed for fast random access, but has a more general approach.

List: Order is the most important feature of the list: it guarantees that the order of the elements is maintained. List adds a number of methods to collection that allow you to insert and remove elements into the middle of the list (this is recommended only for linkedlist use.) A list can generate Listiterator, use it

You can traverse the list from two directions, or you can insert and remove elements from the middle of the list.

ArrayList: A list implemented by an array. Allows quick random access to elements, but inserts and removes elements in the middle of the list is slow. Instead of inserting and removing elements, listiterator should only be used to traverse the ArrayList from the back forward. Because it's a lot bigger than the linkedlist overhead.

LinkedList: The sequential access is optimized, and the overhead of inserting and deleting into the list is not significant. Random access is relatively slow. (use ArrayList instead.) Also has the following methods: AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), and Removelast (), these methods (not defined in any interface or base class) Enables LinkedList to be used as stacks, queues, and bidirectional queues.

the function method of set

The set has exactly the same interface as collection, so there is no additional functionality, unlike the two different lists that preceded it. In fact set is collection, but behavior is different. (This is a typical application of inheritance and polymorphic thinking: behavior that behaves differently.) Set does not save duplicate elements (more responsible for determining the same element)

Set: Each element that is stored in the set must be unique because the set does not save the repeating element. Elements that join a set must define the Equals () method to ensure the uniqueness of the object. Set has exactly the same interface as collection. The set interface does not guarantee the order of elements to be maintained.

HashSet: Set for quick find design. The object that is stored in the HashSet must define HASHCODE ().

TreeSet: Save order set, bottom is tree structure. Use it to extract ordered sequences from set.

Linkedhashset: Has a hashset query speed and internally uses a linked list to maintain the order of elements (in the order of insertion). So when you use iterators to traverse set, the results are displayed in the order in which the elements are inserted.

the functional method of map

Method put (Object key, object value) adds a "value" (What you Want) and a key that is associated with the value (used to find it). The method get (Object key) returns the value associated with the given key. You can use ContainsKey () and Containsvalue () to test whether a "key" or "value" is included in the map. The standard Java class library contains several different map:hashmap, TreeMap, Linkedhashmap, Weakhashmap, and Identityhashmap. They all have the same basic interface map, but behavior, efficiency, sorting strategies, the lifecycle of saving objects, and the policy of determining "key" equivalence are different.

Execution efficiency is a big problem in map. Look at what gets () to do, and you'll see why it's rather slow to search for "keys" in ArrayList. And that's where hashmap is raising speed. HashMap uses a special value, called hash code, to replace a slow search for keys. "Hash code" is "relatively unique" to represent an int value of an object, which is generated by converting some of the object's information. All Java objects can produce hash codes, because Hashcode () is a method defined in the base class object.

HashMap is a quick query using an object's Hashcode (). This approach can significantly improve performance.

MAP: Maintains the association of key-value pairs so that you can find the value by "key"

Hashmap:map is based on a hash table implementation. The cost of inserting and querying key-value pairs is fixed. The capacity capacity and load factor load factor can be set through the constructor to adjust the performance of the container.

Linkedhashmap: Similar to HashMap, but when iterating through it, the order in which "key value pairs" is obtained is its insertion order, or the order in which the least recent (LRU) is used. Just a little slower than HashMap. It is faster when iterating through the access, because it uses a linked list to maintain the internal order.

TREEMAP: The implementation of the data structure based on the red-black tree. When you view key or key-value pairs, they are sorted (in order by Comparabel or comparator). TreeMap is characterized by the fact that the results you get are sorted. TreeMap is the only map with a Submap () method that can return a subtree.

Weakhashmao: The object used in the weak key (weak key) Map,map is also allowed to be released: This is designed to solve special problems. This key can be reclaimed by the garbage collector if a reference other than the map points to a key.

Identifyhashmap: Hash map that uses = = instead of equals () to compare "keys". Designed to solve specific problems.

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.