Java collection List, Set collection system and etc. Detailed Map

1, a collection of Java branch, top-level interface

 

List, Set, Map is an interface, the first two to inherit the Collection interface, Map is independent interface

There HashSet, LinkedHashSet, TreeSet under Set

There ArrayList, Vector, LinkedList under List

Map下有Hashtable,LinkedHashMap,HashMap,TreeMap

Under the Collection interface there is a Queue interfaces, classes have PriorityQueue

Note:
  Queue interface List, Set the same level, they are inherited the Collection interface.
  Figure you will find, LinkedList can either implement the Queue interface, can be realized List interface. But it, LinkedList implements the Queue interface. Queue Interface narrowing the access to the LinkedList methods (ie parameter type in the method if a Queue, Queue access method can only be completely defined by the interface, but can not directly access non Queue method of LinkedList), so that only appropriate methods can be used.
  SortedSet is an interface inside it (only this one TreeSet implementation is available) the elements must be ordered.

to sum up:

Connection interface:

- List orderly, repeatable

  • ArrayList

    Advantages : the underlying data structure is an array, the query fast, slow additions.
    Drawback : thread-safe, high efficiency

  • Vector

    Advantages : the underlying data structure is an array, the query fast, slow additions.
    Drawback : thread-safe, low efficiency

  • LinkedList

    Advantages : the underlying data structure is a linked list, the query slow, fast deletions.
    Drawback : thread-safe, high efficiency

- the Set of disorder, the only

  • HashSet

    The underlying data structure is a hash table. (Unordered only)
    how to ensure the uniqueness of the elements?
      1. relies on two methods: hashCode () and equals ()

  • LinkedHashSet

    The underlying data structure is a linked list and hash tables. (FIFO ordered insertion, the only)
      1 by the ordered list of elements to ensure
      2. by a hash table to ensure that the only elements

  • TreeSet

    The underlying data structure is a red-black tree. (The only ordered)
    1. Sort the elements of how to ensure it?
      Natural order
      comparator sort
    2. How to ensure the uniqueness of the elements?
      Determined based on a comparison of the return value is 0


Collection collection for us in the end who use it? (Master)

The only right?

Is: Set

Sort it?

They are: TreeSet or LinkedHashSet
No: HashSet
if you know Set, but do not know which Set, to use HashSet.

No: List

To secure?

Is: Vector
No: ArrayList or LinkedList

Multi-query: ArrayList
additions and more: LinkedList
if you know the List, but do not know which List, to use ArrayList.

If you know Collection collection, but do not know who to use, use ArrayList.
If you know a collection, use ArrayList.

 

Having a Collection, to simply look Map.

Map interface:

 

 

 

Map interface has three more important to realize categories, namely HashMap, TreeMap and HashTable.

  • TreeMap is ordered, HashMap and HashTable are unordered.
  • Hashtable methods are synchronized, the method HashMap is not synchronized. This is the main difference between the two.

This means:

  • Hashtable is thread-safe, HashMap is not thread safe.
  • HashMap high efficiency, low Hashtable efficiency.

    If no synchronization requirements for compatibility with legacy code or recommended HashMap. Hashtable view the source code can be found, in addition to the constructor, declare all public methods of the Hashtable are synchronized keyword, while HashMap is not the source code.

  • Hashtable does not allow null values, HashMap allows null values ​​(key and value are allowed)
  • Different parent: Hashtable parent is Dictionary, HashMap parent is AbstractMap

It focuses on key issues:

The difference between (a) .TreeSet, LinkedHashSet and HashSet of

1 Introduction

  • TreeSet, LinkedHashSet and HashSet are realized in java data structure of a Set

    •   The main function for sorting TreeSet
    •   LinkedHashSet main function that is used to ensure an ordered set of FIFO (First In First Out)
    •   HashSet only common storing data collection

2. The same point

  • Duplicates elements: because all three to achieve Set interface, so the three do not contain duplicate elements
  • Thread safety: the three are not thread-safe, if you want to use thread-safe can Collections.synchronizedSet ()

3. Different points

  • Performance and Speed: HashSet insert data the fastest, followed by LinkHashSet, the slowest is achieved because the internal sorting TreeSet
    •   Ordering: HashSet does not guarantee an orderly, LinkHashSet ensure insertion FIFO order sorted i.e., internal TreeSet implement sorting installation, can also be custom collation
    •   null: HashSet and allowed LinkHashSet null data, but will be reported NullPointerException null data inserted when a TreeSet

 

Reference: https://blog.csdn.net/zhangqunshuai/article/details/80660974

Guess you like

Origin www.cnblogs.com/116970u/p/11440331.html