list map set usage

Do List, Set, Map inherit from the Collection interface?
Answer: List, Set is, Map is not. Collection is the most basic collection interface. A Collection represents a group of Objects, that is, the elements of Collection. Some Collections allow the same elements while others do not. Some sort and others don't. Java JDK cannot provide classes that inherit directly from Collection. The classes provided by Java JDK are all "sub-interfaces" that inherit from Collection, such as List and Set.
Note: Map does not inherit the Collection interface, Map provides a mapping from key to value. A Map cannot contain the same key, and each key can only map one value. The Map interface provides three types of collection views. The contents of the Map can be regarded 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. Set accepts each object only once and uses its own internal sorting method (usually, you only care if an element belongs to a Set, not its order - otherwise you should use a List). Map also saves a copy of each element, but this is based on "key", and Map also has built-in sorting, so it doesn't care about the order in which elements are added. If the order of adding elements is important to you, you should use LinkedHashSet or LinkedHashMap.

Details:
List Features: Elements are placed in an order, elements can be repeated
Map Features: Elements are stored in key-value pairs, no placement order
Set features: Elements are not placed The order of entry, the elements cannot be repeated (note: although the elements have no order of placement, the position of the element in the set is determined by the HashCode of the element, and its position is actually fixed) The
List interface has three implementation classes: LinkedList, ArrayList, Vector
LinkedList: The bottom layer is implemented based on a linked list. The memory of the linked list is scattered. Each element stores its own memory address and also stores the address of the next element. The difference between ArrayList
and Vector: ArrayList is not thread-safe and has high efficiency; Vector is thread-safe and inefficient. The
Set interface has two implementation classes: HashSet (the bottom layer is implemented by HashMap), and the LinkedHashSet
SortedSet interface has An implementation class: TreeSet (the bottom layer is implemented by a balanced binary tree)
Query interface has an implementation class: LinkList
Map interface has three implementation classes: HashMap, HashTable, LinkeHashMap
  HashMap is not thread-safe, efficient, and supports null; HashTable is thread-safe, inefficient, and inefficient. Support for null
SortedMap has an implementation class: TreeMap
is actually the most important thing, list is used to deal with sequences, and set is used to deal with sets. Map is known, and it stores the key-value pair
set, which is generally unordered and not repeated. The map kv structure list is ordered.


The functional method of List There are
  actually two kinds of Lists: one is the basic ArrayList, which has the advantage of randomly accessing elements, and the other is the more powerful LinkedList, which is not designed for fast random access, but has a set of more general method.
  List : Order is the most important feature of List: it guarantees that a specific order of elements is maintained. List adds a number of methods to Collection, enabling insertion and removal of elements to and from the middle of the List (this is only recommended for LinkedList.) A List can generate a ListIterator, which can be used to traverse the List in two directions, as well as insert and remove elements from the middle of the List. remove elements.
  ArrayList : A List implemented by an array. Allows fast random access to elements, but inserts and removes elements from the middle of the List very slowly. ListIterator should only be used to traverse an ArrayList backwards and forwards, not to insert and remove elements. Because that's a lot more overhead than LinkedList.
  LinkedList: Optimized for sequential access, the overhead of inserting and deleting in the middle of the List is not large. Random access is relatively slow. (Use ArrayList instead.) Also has the following methods: addFirst(), addLast(), getFirst(), getLast(), removeFirst() and removeLast(), which (not defined in any interface or base class) make LinkedList can be used as stack, queue and bidirectional queue.
  Set's functional method
  Set has exactly the same interface as Collection, so it doesn't have any extra functionality, unlike two different Lists in the front. In fact Set is Collection, but the behavior is different. (This is a typical application of inheritance and polymorphism: showing different behaviors.) Set does not store duplicate elements (it is more responsible for how to judge the same elements)
  Set : Each element stored in Set must be unique, because Set does not save duplicate elements. Elements added to the 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 that the order of the elements is maintained.
  HashSet : Set designed for quick lookup. Objects stored in HashSet must define hashCode().
  TreeSet : A Set that preserves the order, and the bottom layer is a tree structure. Use it to extract an ordered sequence from a Set.
  LinkedHashSet : Has the query speed of HashSet, and internally uses a linked list to maintain the order of elements (the order of insertion). So when using the iterator to traverse the Set, the results are displayed in the order in which the elements were inserted.
  Map's functional methods The
  method put(Object key, Object value) adds a "value" (what you want) and a "key" (key) associated with the "value" (use it to look up). The method get(Object key) returns the "value" associated with the given "key". You can use containsKey() and containsValue() to test whether a Map contains a "key" or "value". The standard Java class library contains several different Maps: HashMap, TreeMap, LinkedHashMap, WeakHashMap, IdentityHashMap. They all have the same basic interface Map, but differ in behavior, efficiency, sorting strategy, preservation of object life cycle, and strategy for determining equivalence of "keys".
  Execution efficiency is a big problem with Map. Looking at what get() does, you can see why searching for a "key" in an ArrayList is rather slow. And this is where HashMap improves speed. HashMap uses a special value, called a "hash code", to replace the slow search for the key. A "hash code" is a "relatively unique" int value used to represent an object, generated by converting some information about the object. All Java objects can generate hash codes because hashCode() is a method defined in the base class Object.
  HashMap uses the object's hashCode() for fast query. This approach can significantly improve performance.
  Map : maintains the association of "key-value pairs", allowing you to look up "values" by "keys"
  HashMap : Map based implementation of hash table. The cost of inserting and querying "key-value pairs" is fixed. The capacity and load factor can be set through the constructor to adjust the performance of the container.
  LinkedHashMap: Similar to HashMap, but when iteratively traversing it, the order in which the "key-value pairs" are obtained is its insertion order, or the least recently used (LRU) order. Only slightly slower than HashMap. It is faster when iteratively accessed, because it uses a linked list to maintain internal order.
  TreeMap : An implementation based on a red-black tree data structure. When viewing "keys" or "key-value pairs", they are sorted (order determined by Comparabel or Comparator). The thing about TreeMap is that the results you get are sorted. TreeMap is the only Map with a subMap() method that returns a subtree.
  WeakHashMao : Weak key (weak key) Map, the objects used in the Map are also allowed to release: This is designed to solve special problems. If no references outside the map point to a "key", this "key" can be reclaimed by the garbage collector.
  IdentifyHashMap : A hash map that uses == instead of equals() to compare "keys". Designed to solve special problems.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326706135&siteId=291194637