Collection class in Java (1)

zhaozj2021-02-16  57

Why use a collection class?

When you don't know the number of data to be stored in advance, or if you need a more flexible way to get a more flexible approach to the array, you need to use the collection class.

Understanding set class

The set class is stored in the java.util package. The collection class is stored in an object's reference, not the object itself. For the convenience of expression, we call the object in the collection refer to the reference in the collection (REFERENCE). There are three set types: SET (set), list (list), and MAP (mapping).

(1) Collection (SET) is the simplest collection, its objects are not sorted in a particular way, just simply add objects to the collection, just like moving things in the pocket. The access and operation of the centralized member is based on the reference to the centralized object, so the concentration cannot be repeated. The set also has a variety of variants, which can be sorted, such as Treeset, which adds objects to the concentrated operation to insert it into an ordered object sequence in accordance with some comparison rules. It implements the sortedset interface, which is the method of joining an object comparison. By iteration of the focus, we can get a set of ascended objects.

(2) The main feature of the list list is that the object is stored in linear mode, there is no specific order, only one start and an end, of course, it is different from the set of no order at all. The list appears in the data structure as: arrays and vector, linked lists, stacks, queues. Regarding the collection class that implements the list, it is often used in our daily work, which will be described in detail behind the notes.

(3) The mapping mapping is distinct from the set or list, and each item in the mapping is paired. Each object stored in the map has a related keyword (key) object, the keyword determines the storage location of the object in the mapping, and the corresponding keyword must be provided when retrieving the object, just like checking the words in the dictionary. The keyword should be unique. The keyword itself does not determine the storage location of the object, it needs to process a hashing technology to generate an integer value called a Hash Code, the hash code is usually used as a part Set, the bias is the storage location of the keyword / object pair with respect to the start position of the memory area assigned to the map. Ideally, hash processing should produce a uniform distributed value within a given range, and each keyword should get a different hash code.

Introduction to the collection

There are 13 classes in Java.util to manage collection objects, their support sets, lists, or mappings, etc., the following is a brief introduction to these classes

Set: hashset: Implementation of a set using HashMap. Although the set is defined in disorder, there must be a method to find an object with a particularly efficiently. The storage and retrieval operation using a HashMap object is implemented in a fixed time. Treeset: The implementation of the set of objects in ascending sequence to objects. This means that the first iterator from a TreeSet object will provide an object in an ascending order. The Treeset class uses a TreeMap. List: Vector: Implement a similar number of tables, automatically add capacity to accommodate the elements you need. Use the subscript store and retrieve objects as in a standard array. You can also retrieve objects from a vector with an iterator. Vector is the only synchronous container class? "It is also good to be in both two or more threads. STSCK: This class is derived from the Vector and adds a method to implement a stack - a storage structure of a backward first out. LinkedList: Implement a linked list. Links defined by this class can also be used as stacks or queues. ArrayList: Implement an array, its size varies, can be accessed like a linked list. It provides features similar to the Vector class but does not synchronize. Mapping: HashTable: Implement a image, all keys must be non-empty. In order to efficiently, the defined key must implement the HashCode () method and equal () method. This class is a inheritance of the previous Java implementation and is usually better in other classes that implement the image. Hashmap: Implement a map, allowing storage empty objects, and allows the keys to be empty (because the keys must be unique, certain one can only be one). WeakhashMap: Implement such a image: usually if a key is no longer referenced to an object, the key / object pair will be discarded. This is a life cycle of a key / object pair in a comparison, a key to a key / object pair, although the program is used, and therefore cannot retrieve objects. TreeMap: To achieve such an image, the object is a button ascending order. The following figure is the relationship between the interfaces implemented by the collection class:

SET and LIST are extended by the public interface Collection, so they can be referenced using a variable that type collection. This means that any list of lists or sets can be referenced in this way, except for mapping classes (but not completely excluded, because you can get a list from the mapping.) So, pass a list or set to The standard way of the method is to use the parameters of the Collection type.

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

New Post(0)