Java collections - the difference between List, Set and Map





1. There are three main collection types:

Set (set)
List (list)
Map (map)



2. The relationship between the three is as follows:



The difference between List, Set and Map:

gather List Set Map
element order orderly out of order key unordered, value unordered
whether the element is repeatable repeatable not repeatable key cannot be repeated, value can be repeated


List

The elements stored in the List are ordered, which means that the order of reading is consistent with the order of storing.

① Duplicate objects can be allowed;
② Multiple null elements can be inserted;
③ It is an ordered container, which maintains the insertion order of each element, and the order of output is the order of insertion;
④ Commonly used implementation classes are ArrayList, LinkedList and Vector ; ArrayList is the most popular, it provides random access using the index, and LinkedList is more suitable for occasions where you often need to add or delete elements from the List.



Set

The elements stored in Set are unordered, and the unordered here means that the order of storage may be inconsistent with the order of output.

①Duplicate objects are not allowed;
②Unordered containers, you cannot guarantee the storage order of each element, TreeSet maintains a sort order through Comparator or Comparable;
③Only one null element is allowed;
④The most popular implementation class of the Set interface is HashSet , LinkedHashSet and TreeSet;
the most popular is HashSet based on HashMap;
TreeSet also implements SortedSet interface, so TreeSet is an ordered container sorted according to the definition of its compare() and compareTo().



Map

The elements stored in the Map are key-value pairs (key-value), and the keys and values ​​are unordered, that is, the storage order may be different from the output order.

①Map is not a subinterface or implementation class of Collection, Map is an interface;
②Each Entry of Map holds two objects, that is, one key and one value, and Map may hold the same value object but the key object must be unique ;
③TreeMap also maintains a sort order through Comparator or Comparable;
④You can have any number of null values ​​in Map, but there can only be one null key at most;
⑤The most popular implementation classes of Map interface are HashMap, LinkedHashMap, Hashtable and TreeMap. (HashMap, TreeMap are most commonly used)












Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page.
Thank you for reading this article in the vast crowd.
Where is the signature without personality!
For details, please pay attention to me
and continue to update...

Scan to have a surprise!
© 2022 02 - Guyu.com | 【All rights reserved】

Guess you like

Origin blog.csdn.net/weixin_49770443/article/details/116230141