The concept, classification and usage of container in java development are explained in detail

This paper describes the concept, classification and usage of container in java development. Share with you for your reference, as follows:

1. Concept of container

In Java, if a class is specially used to store objects of other classes, this class is called a container, or a collection. A collection is a whole formed by combining several class objects with the same or similar properties

2. Relationship between container and array

Why containers are needed:

① The length of the array is difficult to expand ② the types of data in the array must be the same

Difference and relation between container and array:

① The container is not an array, and the elements in the container cannot be accessed by subscript. ② all functions of the array can be realized through the ArrayList container, but the implementation methods are different. ③ if the container is not to be used as an array, an array is returned through the toarraylist method

Sample program:

Output results:

Get an array arr through the ArrayList container:

3. Several common methods of container

Boolean add (object obj): adds a specified element to the container. Iterator iterator(): returns an iterator that can traverse all elements in the current collection. Object [] toarray(): returns an array containing all elements in this container. Object get (int index): get the element with index subscript object remove (int index): delete the element with index subscript object set (int index, object element): set the element with index subscript as element object add (int index, object element): add an object element object put (object key, object value) at the position with index subscript : add a specified element to the container. Object get (object key): get the object with the key. Int size(): return the number of elements in the container

Instance program:

Operation results:

4. Classification of containers

Containers are divided into set, list and map mapping

Set set: due to the characteristics of the internal storage structure, the order of elements is not distinguished in the set set, and duplicate elements are not allowed. The TreeSet container is special, and there is a natural order when the elements are put in. The set container can correspond to the set in Mathematics: the same elements will not be added

List: due to the characteristics of internal storage structure, the order of elements in the list collection is distinguished, and duplicate elements are allowed. The elements in the list set correspond to an integer serial number, recording their position in the container. The elements in the container can be accessed according to the serial number - ordered and repeatable

Map mapping: due to the characteristics of internal storage structure, the mapping cannot contain duplicate key values, and each key can only map one value at most, otherwise it will be overwritten (the later value value will overwrite the previous value value). Map is a set that maps key objects and value objects, that is, the data itself should be stored in the map container, Also store Keywords: the same elements will be overwritten

Note: for set and map, there is no order after the elements are placed. If you want the elements to be placed in order, you can use TreeSet and treemap to store data.

Instance program:

Operation results:

Instance program:

Operation results:

5. Use of tostring() method: whenever a class object is placed in a container, the corresponding class should implement the tostring() method in the object class; For all data types that come with Java, the toString () method has been rewritten

Instance 1: (before overriding the tostring() method)

Output results:

Example 2: (after rewriting the toString () method)

Output results:

6. CompareTo () method in the comparable interface: all classes that need to be compared and sorted should implement the CompareTo () method in the comparable interface; Whenever you put a class object into a container with a tree as its internal structure, you should implement the CompareTo () method in the comparable interface

Example 1:

Output results:

Example 2:

Output results:

7. If a class object is placed in a container with a hash table as its internal storage structure, the corresponding class must implement the equals method and hashcode method, so as to comply with the real logical function of the hash table

Instance program 1: (before rewriting)

Output results:

Instance program 2: after rewriting

Output results:

8. An important logic: logically, as long as the contents of two objects are the same, their addresses (hashcode() return value) and the two objects should be the same (equals()),

Instance program (before rewriting):

Output results:

After Rewriting:

Output results:

The above 5, 6, 7 and 8 can be summed up as four "all" and one "logic":

1. Whenever a class object is placed in a container, the corresponding class should implement the toString () method in the object class; 2. All classes that need to be compared and sorted should implement the CompareTo () method in the comparable interface; If you put a class object into a container with a tree as the internal structure, you should implement the compareto() method in the comparable interface. 3. If you put a class object into a container with a hash table as the internal storage structure, the corresponding class must implement the equals method and hashcode method, so as to meet the real logical function of the hash table 4. Logically, as long as the contents of two objects are the same, their addresses (hashcode () return value) and the two objects should be the same (equals ()).

9. Related concepts of hash conflict

In essence, hash (object 1. Hashcode()) = hash2 (object 2. Hashcode()), that is, the hash code value returned by the hashcode() method of the first object is brought into the hash function, and the index position obtained is the same as that obtained by the hash code value returned by the hashcode() method of the second object. This is a hash conflict.

The most common hash algorithm is modulo.

The following is a brief introduction to the calculation process of the modulus method.

For example, the length of the array is 5. At this time, one data is 6. So how to store this 6 in an array with a length of only 5. According to the modular method, calculate 6% 5, and the result is 1. Then put 6 in the position where the subscript of the array is 1. So, 7

It should be in 2 this position. At this point, the Haas conflict has not yet emerged. At this time, there is a data of 11. According to the modulus method, 11% 5 = 1, which is also equal to 1. So there are several places where the subscript of the original array is 1, which is 6. At this time, the position of 1 is calculated, so the position of array 1 must store two numbers. At this time, it is called hash conflict. After the conflict, it should be stored in order.

If the data is widely distributed and the length of the array storing the data is relatively large.

Then there are fewer hash conflicts. Otherwise, the conflict is very high.

10. Function of iterator interface

Important methods:

Boolean hasnext(): it is used to judge whether there is an element behind the current cursor (iterator). If there is, it returns true, otherwise it returns false object. Next(): first return the element to the right of the current cursor, and then move the cursor one position later. Void remove(): the remove() method of iterator is not recommended, but the remove() method of the container is recommended.

Instance program:

Thinking questions:

For more information about Java algorithms, readers who are interested can see the topics on this site: introduction and advanced tutorial of java object-oriented programming, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>