Two Java container class LIST and SET analysis

xiaoxiao2021-03-30  232

The container class can greatly improve programming efficiency and programming capabilities. In Java2, all containers are redesigned by Sun's Joshua Bloch, enriched the functionality of the container library.

The use of the Java2 container class library is "saving objects", which is divided into two categories:

Collection ---- A set of independent elements, usually these elements are obeyed from some rules. List must maintain a specific order of element, and SET does not have a repetitive element.

Map - A set of "key value pairs" objects, that is, its element is paired, the most typical application is the data dictionary, and there are other extensive applications. In addition, the MAP can return the set of all the sets of all the keys, or all values ​​composed of all values, or their key value pairs, and can also expand multi-dimensional MAP like an array, as long as the MAP key value pair is enabled each value "It is a MAP.

Iterator

The iterator is a design pattern that is an object that can be traversed and selects objects in the sequence, and developers do not need to understand the underlying structure of the sequence. Iteer is often referred to as "lightweight" object because it is created.

The iTerator function in Java is relatively simple, and can only move one-way:

(1) Use method iterator () requires the container to return an item. When you first call the NEXT () method of the Iterator, it returns the first element of the sequence.

(2) Use Next () to obtain the next element in the sequence.

(3) Check if there is an element in the sequence using the HasNext ().

(4) Use remove () to delete the elements that the iterator returned.

Iterator is the simplest implementation of the Java iterator, which has more features for Listiterator, which can be traversed from two directions, or insert and delete elements from the list.

2.List function method

List (interface): The order is the most important feature of List; it ensures maintenance element specific order. List adds a number of methods to Collection to enable insertion with remove elements in the middle of the LIST (LINKEDLIST). A list can generate a listiterator that uses it to traverse the list from both directions, or insert and delete elements from the middle of the list.

ArrayList: List implemented by an array. It allows fast rapid access to the elements, but inserts to the middle of the LIST and the speed of removing elements are slow. Listiterator should only be used by traversing arraylist, not to insert and delete elements, because this is much larger than the LINKEDLIST overhead.

LINKEDLIST: Optimize sequential access, inserting to List and delete a lot of overhead, random access is relatively slow (ARRAYLIST can be replaced). It has methods addFirst (), addlast (), getFirst (), getlast (), removefirst (), removelast (), these methods (not defined in any interface or base class) makes LinkedList as stack, queue, and Bidirectional queue is used.

3.Set function method

Set (Interface): Each element stored in SET must be unique because SET does not save repeating elements. The Object to join the SET must define the equals () method to ensure the uniqueness of the object. Set with Collection exactly the same interface. The SET interface does not guarantee the order of maintenance elements.

Hashset: SET designed for fast findings. Objects deposited in Hashset must define HashCode ().

Treeset: Keep the order of SET, the bottom layer is a tree structure. Use it to extract ordered sequences from the SET.

LinkedHashSet: With the query speed of the Hashset, the order in which the linked list maintenance element is used inside (inserted order). So when using an iterator traverses SET, the result is displayed in the order of element insertion. The Hashset is sorted by a hash function. This is designed for quick queries; Treeset uses a red-haircut data structure for sorting elements; LinkedHashSt uses a hash to speed up the query speed, and use the order of the linked list. It makes it sustainable in the order of insertion. It should be noted that when generating its own class, the SET requires maintenance elements to store the order, so it is to implement the Comparable interface and define the compareto () method.

转载请注明原文地址:https://www.9cbs.com/read-130645.html

New Post(0)