Difference list, set and map collections

The difference between the set and the Map List Set

To understand the difference between the three, we wanted to see java collection framework map
Here Insert Picture Description
and then they need from their respective characteristics for

First, the main characteristics and usage List

  1. Allow the storage of objects can be repeated
  2. A plurality of elements may be inserted null
  3. Is an ordered container holding the inserted sequence of each element, i.e., the order of insertion sequence output
  4. Common implementation class has ArrayList, LinkedList and Vector. ArrayList most popular, which provides free access to use the index, while LinkedList is often more appropriate to add or remove elements from the List of occasions
  5. Map distinguished collections, List is a collection of separate collection

Second, the main features of the collection and usage Set

  1. Do not allow duplicate objects
  2. Only one null elements
  3. Unordered container, you can not guarantee the storage order of each element, TreeSet by Comparator or Comparable maintains a sort order
  4. Set interface to achieve several of the most popular classes are HashSet, LinkedHashSet and TreeSet. The most popular implementation is based HashSet HashMap; TreeSet SortedSet also implements the interface, thus TreeSet ordered a container which is sorted by compare define () and the compareTo () of

Third, the main features of Map collection and usage

  1. Map is not a collection of sub-interface or implementation class. Map is an interface. The List and Set is a collection of sub-interfaces
  2. Each Entry Map are holding two objects, which is a key value, Map may hold the same key value of the object but the object must be unique, it is the feature set of double row
  3. Comparable or Comparator TreeMap also maintained by a sort order
  4. Map where you can have a null value but are free at most one null key
  5. Map the most popular of several interfaces implementation class is HashMap, LinkedHashMap, Hashtable and TreeMap. (Which HashMap, TreeMap most commonly used)

Fourth, the difference

  1. Collection and Map distinction
    within the vessel moved by a different number of each element stored.
    Collection type person, each location is only one element.
    Map type who hold key-value pair, like a small database.

  2. Its respective subclass relationships
    Collection
    -List: storage element will be in a particular order. Therefore, the order may be taken out and placed in different order.
    -ArrayList / the LinkedList / the Vector
    -Set: not contain duplicate elements
    -HashSet / TreeSet
    the Map
    -HashMap
    -HashTable
    -TreeMap

  3. Other features
    List, Set, Map will be deemed to hold the object Object type.
    Collection, List, Set, Map is an interface, it can not be instantiated.
    Inherited from their ArrayList, Vector, HashTable, HashMap figurative class, before these are instantiated.
    vector container to know exactly what objects it holds a membership type. vector without bounds checking.

to sum up

  1. If it comes to operating stacks, queues, etc., should consider the use of List, the need to quickly insert, delete elements, you should use LinkedList, if you need fast random access to elements, you should use ArrayList.
  2. If the program, or access only in a thread in a single-threaded environment, consider the asynchronous class, the higher the efficiency, if multiple threads may also operate a class, the class should be used to synchronize.
  3. In addition to the need to use TreeSet when ordering, outside TreeMap, should use HashSet, HashMap, because of their higher efficiency.
  4. Pay special attention to the operation of the hash table, as a key target to correct replication equals and hashCode methods.
    Containers held only object reference (pointer to the object), instead of the copy destination information to a number of columns in a certain position. Once the objects are placed within the container, they do not lose information about the object type.
    Try to return to the interface instead of the actual type, such as return List rather than ArrayList, so that if you later need to be replaced ArrayList LinkedList, client code need not change. This is for the abstract programming.

note:

1, Collection did not get () method to obtain an element. Can only be traversed by elements iterator ().
2, Set and Collection has exactly the same interface.
3, List, one element may be removed by a get () method. Use numbers to select a bunch of objects, get (0) .... (the Add / GET)
. 4, generally used ArrayList. LinkedList stack configuration with stack, queue queue.
5, Map using put (k, v) / get (k), you may be used containsKey () / containsValue () to check whether they contain one wherein key / value.
HashMap hashCode object will use to quickly find the key.
6, Map elements may be key sequence, value sequence extracted separately.
Use keySet () extracting a key sequence, all generated keys map in a Set.
Using the values () value extraction sequence, all the values map to generate a Collection.
Why generate a Set, a generation Collection? That is because, key is always unique, value allowed to be repeated.

Reprinted from: https://blog.csdn.net/qq_42894060/article/details/82251908

Guess you like

Origin blog.csdn.net/xiao_xiao_b/article/details/93733157