Home >Java >Javagetting Started >Overview of Collection interface and Map interface

Overview of Collection interface and Map interface

王林
王林forward
2020-08-14 16:54:022534browse

Overview of Collection interface and Map interface

Java collection is like a container, which can store any type of data. In Java, collection classes usually exist in the Java.util package.

(Recommended tutorial: java introductory tutorial)

Java collections are mainly composed of two major systems, namely: Collection system and Map system. Among them, the Collection system and the Map system are the top-level interfaces in the two systems respectively.

Collection mainly has three sub-interfaces, namely List, Set, and Queue.

Ordered and repeatable: List, Queue

Unordered and repeatable: Set

Collection interface

1. List has Order, repeatable

1. ArrayList

Advantages: The underlying data structure is an array, which is fast to query and slow to add and delete.

Disadvantages: Thread-unsafe, but high efficiency.

2. Vector

Advantages: The underlying data structure is an array, which is fast to query and slow to add and delete.

Disadvantages: Thread safety, low efficiency

3. LinkedList

Advantages: The underlying data structure is an array, the query is slow, and blocks are added and deleted.

Disadvantages: Thread unsafe, high efficiency

2. Set is unordered, the only one

1.HashSet

The underlying data structure is a hash table. (Unordered, unique)

How to ensure the uniqueness of elements?

Depends on two methods: hashCode() and equals()

2, LinkedHashSet

The underlying data structures are linked lists and hash tables. (FIFO insertion is ordered and unique)

Ⅰ. The elements are guaranteed to be ordered by the linked list

II. The elements are guaranteed to be unique by the hash table

3. TreeSet

The underlying data structure is a red-black tree. (Unique, ordered)

How to ensure the sorting of elements?

Natural sorting, comparator sorting.

How to ensure the uniqueness of elements?

Decide according to whether the return value of the comparison is 0.

(Video tutorial recommendation: java course)

Map interface

1. The Map interface has three important implementations The classes are: HashMap, TreeMap, and HashTable.

2. Orderly: TreeMap. Unordered: HashMap, HashTable.

3. The main difference between HashTable and HashMap:

The method of Hashtable is synchronous, but the method of HashMap is not synchronous.

4. Hashtable is thread-safe, but HashMap is not thread-safe.

5. HashMap is more efficient than Hashtable.

If there are no requirements for synchronization or compatibility with legacy code, it is recommended to use HashMap. Looking at the source code of Hashtable, we can find that, except for the constructor, all public method declarations of Hashtable have the synchronized keyword, but there is no such thing in the source code of HashMap.

6. Hashtable does not allow null values, and HashMap allows null values ​​(both key and value are allowed).

7. Different parent classes: The parent class of Hashtable is Dictionary, and the parent class of HashMap is AbstractMap.

The above is the detailed content of Overview of Collection interface and Map interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete