List, Set, and Map Detailed

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_41948075/article/details/100095990

Benpian reference Java collection List, Set, and Map, etc. Detailed collection system (the most complete history) , the Set, List, Map differences and relations, and interview key issues two bloggers write write blog posts. This is just my little its integration.

Java collections three categories

Java in the collection include three categories: the Set (set), List (list) and Map (map) , they are in the java.util package, Set, List and Map interfaces are, they have their own implementation class. Set mainly in the implementation class HashSet and TreeSet, List of the main implementation class ArrayList, Map implementation class mainly HashMap and TreeMap.

Collection is the most basic set of interfaces, declare a general method applicable to the collection of JAVA, List and Set interfaces are inherited from the collection.

List

  • Allowing duplicate object .
  • A plurality of elements may be inserted null.
  • Is a sorted containers , each holding the insertion sequence elements, it is sequentially output order of insertion.
  • 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.

Set

  • Do not allow duplicate objects
  • Unordered container , you can not guarantee the storage order of each element, TreeSet by Comparator or Comparable maintains a sort order.
  • Only one null elements
  • 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 according to its TreeSet is a compare () and define the compareTo () are orderly sorted containers.

Map

  • Map is not a collection of sub-interface or implementation class. Map is an interface.
  • Each Entry Map are holding two objects, which is a key value, Map may hold the same value of the object, but the object key must be unique.
  • TreeMap also maintains a sort order by Comparator or Comparable.
  • Map where you can have free to a null value but a maximum of one L NUL key .
  • Map the most popular of several interfaces implementation class is HashMap, LinkedHashMap, Hashtable and TreeMap. (HashMap, TreeMap most commonly used)

The difference between List, Set, and Map

Set collection objects are not sorted in a particular manner, and no duplicate object. There are default sort ordering and custom sorting, sort we need to implement custom Comparator interface.

List objects in the collection position by index sorted, the object can be repeated, according to the object allows the search target index position in the collection.

Map collection elements includes a pair of key value of objects and objects, can not duplicate key object, the object value can be repeated.

List, Set, and Map Contact and usage scenarios

The difference between ArrayList, Vector and LinkedList of

List orderly, repeatable Differences and connections
ArrayList Underlying data structure is an array ,Query fast, slow additions and deletions, thread-safe, high efficiency. When the ArrayList or Vector element beyond its original size, Vector will double its capacity, whereas only 50% of ArrayList size, so, there is help to save memory space ArrayList
Vector Underlying data structure is an array ,Query fast, slow additions and deletions, Vector's methods are synchronized (Synchronized),Thread-safeSince synchronization is bound to affect the performance of the thread, so performance is better than Vector ArrayList
LinkedList Underlying data structure is a linked list ,Slow queries, additions and deletions fast, thread-safe, high efficiency

ArrayList and LinkedList usage scenarios

  • If you frequently use the index to access elements in a container, then use the List. If you know the index, such as ArrayList List implementation class can provide faster access, because LinkedList to move the pointer. If you frequently add, delete elements, select LinkedList.
  • For new and delete operations add and remove, LinedList comparative advantage, because ArrayList to move data.
  • ArrayList is array-based, LinkedList is based on the list.

Difference HashSet, LinkedHashSet and the TreeSet

Set the disorder, the only
HashSet The underlying data structure is a hash table (unordered data only)
can be placed in null, but only into a null, both of the values can not be repeated, as the only constraint database
relies on two methods: hashCode () and equals () to ensure the uniqueness of the elements
just common set of stored data
LinkedHashSet The underlying data structure is a linked list and hash tables . (FIFO order insert only)
from the list to ensure that the elements ordered
by the hash table to ensure that the only element
to ensure i.e. ordered set FIFO (First In First Out)
TreeSet The underlying data structure is a red-black tree. (Unique, ordered)
data Treeset are automatically sorted, into a null value is not allowed.
By natural ordering and comparator ordering guarantee elements ordered
according to the comparison whether the return value is 0 is determined to ensure the uniqueness of the elements
are mainly used to sort

TreeSet and TreeMap links and differences

And exactly like HashSet, TreeSet method which most of the city directly call TreeMap methods to achieve.
The same point :

  • TreeMap and TreeSet collection are asynchronous, so they can not be shared between multiple threads , but you can use Collections.synchroinzedMap () to achieve synchronization.
  • 运行速度都要比Hash集合慢,他们内部对元素的操作时间复杂度为O(logN),而HashMap/HashSet则为O(1)。
  • TreeMap和TreeSet都是有序的集合,也就是说他们存储的值都是排好序的。

不同点
最主要的区别就是TreeSet和TreeMap分别实现Set和Map接口

  • TreeSet只存储一个对象,而TreeMap存储两个对象Key和Value(仅仅key对象有序)
  • TreeSet中不能有重复对象,而TreeMap中value可以存在重复对象。
  • TreeMap的底层采用红黑树的实现,完成数据有序的插入,排序。
    什么时候用Set什么时候用Map呢?

Set和Map的使用场景

如果希望保证插入元素的唯一性,不希望出现重复值,那么可以选择一个 Set 的实现类,比如 HashSet、LinkedHashSet 或者 TreeSet。所有 Set 的实现类都遵循了统一约束比如唯一性,而且还提供了额外的特性比如 TreeSet 还是一个 SortedSet,所有存储于 TreeSet 中的元素可以使用 Java 里的 Comparator 或者 Comparable 进行排序。LinkedHashSet 也按照元素的插入顺序对它们进行存储。
如果是希望以键值的形式进行数据存储,那么用Map。根据需要选择Hashtable、HashMap或TreeMap。

HashSet与HashMap的区别

HashMap HashSet
实现了Map接口 实现Set接口
存储键值对 仅存储对象
调用put()向map中添加元素 调用add()方法向Set中添加元素
HashMap使用键(Key)计算Hashcode HashSet使用成员对象来计算hashcode值,对于两个对象来说hashcode可能相同,所以equals()方法用来判断对象的相等性,如果两个对象不同的话,那么返回false
HashMap相对于HashSet较快,因为它是使用唯一的键获取对象 HashSet较HashMap来说比较慢

Guess you like

Origin blog.csdn.net/weixin_41948075/article/details/100095990