java collection framework collection(3)

According to "Java Programming Ideas", Java collections are also called containers, and are divided into two categories according to single-slot and double-slots, Collection and Map, both of which are interfaces.

1. Collection

Collection is divided into three sub-interfaces, namely Set, List and Queue. Their characteristics are:

1.Set: The element is unique, that is, the elements in the Set can never be repeated, which is the same as the concept of set in mathematics. All its implementation classes have the same characteristics.

There are three main implementation classes: HashSet, LinkedHashSet, TreeSet

  • The underlying implementation of HashSet is the key in HashMap, so it is characterized by disorder and non-repetition;
  • The underlying implementation of LinkedHashSet is LinkedHashMap, which is characterized by elements being ordered. Ordered here means that the traversal order and insertion order are the same.
  • The underlying implementation of TreeSet is TreeMap, which is also ordered. The ordering here refers to sorting according to the size of the elements themselves. For Integer and String, the Comparable interface has been implemented, so they are comparable. The developer-defined object needs to implement the Comparable interface to facilitate the program execution element comparison.

2.List: Ordered, List is ordered and repeatable, and the order here is only the same as the traversal order and insertion order. The most commonly used implementation classes:

  • The underlying implementation of ArrayList is an array, so the query efficiency is high, and the addition and deletion efficiency is low;
  • The underlying implementation of LinkedList is a linked list, which is longer than insertion and deletion, and has low query efficiency.

3.Queue: Queue, characterized by first-in, first-out, only allows deletion at the front end of the queue, insertion at the back end of the queue, commonly used implementation class PriorityQueue.

2. Map

If arrays and Lists implement the mapping between subscripts and element objects, then Map implements the mapping between objects and objects.

Main implementation classes: HashMap, LinkedHashMap and TreeMap

The bottom layer of HashMap uses a hash table, which can achieve fast search.

The biggest feature of LinkedHashMap is that the key values ​​are ordered, and the traversal order and insertion order are the same.

The bottom layer of TreeMap is a red-black tree. The newly inserted element will be compared with the original element in the Map and inserted into the inserted position. Objects in TreeMap must implement the Comparable interface.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325501000&siteId=291194637