Improving Java Part 9 collection system
After passing the front JDK6 After learning the new features of JDK, we will further study JDK. Because of the importance of collection, we will start with collection:
1、 Collection overview
Java is an object-oriented language. If we want to operate on multiple objects, we must first save multiple objects before operation. Then we will certainly think of using arrays for storage, but the array length is fixed and can not meet the requirements of change. Therefore, Java provides collections.
Differences between arrays and Collections:
Array: fixed length, can store basic types / reference types, and the types of elements in the array must be consistent
Set: variable length, automatic capacity expansion, can only store reference types, and the element types in the set can be inconsistent but generally consistent
2、 Set system
As a container, a collection can store multiple elements, but due to different data structures, Java provides a variety of collection classes. The common functions in collection classes are extracted upward, and finally the collection architecture is formed.
Data structure: the way data is stored
Diagram of collection classes in Java:
3、 Collection [single value interface]
Collection is the most basic collection interface. A collection represents a group of objects, that is, the elements of the collection. Because some collections allow to store the same elements while others do not. Some can be ordered while others do not, two subclass interfaces list and set are derived.
Common methods in collection interface:
A: Add function Boolean add (object obj): add an element to the collection Boolean addall (collection C): add an element of the collection to the collection. B: Delete function void clear(): deletes all elements in the collection. Boolean remove (object obj): deletes a specified element from the collection. Boolean removeAll (collection C): deletes a specified collection element from the collection. C: Judge function Boolean isempty(): judge whether the collection is empty. Boolean contains (object obj): determines whether the specified element exists in the collection. Boolean containsall (collection C): judge whether there are elements in a specified collection in the collection. D: Traversal function iterator iterator (): it is used to get each element in the collection. E: Length function int size(): get the number of elements in the set F: intersection function Boolean retainAll (collection C): judge whether there are the same elements in the two sets. G: Convert a collection into an array object [] toarray(): convert a collection into an array.
Common methods in the list interface:
First of all, we know that the list interface is a subclass of the collection interface, so it also has the above methods. However, in addition to the above methods, the bottom layer of the typical ArrayList in the list interface is an array structure, so it is also used by some index operation methods, as follows:
List's unique function a: add function void add (int index, object obj): add elements at the specified location B: delete function object remove (int index): delete elements according to the specified index and return the deleted elements. C: Modification function object set (int index, object obj): modify the element of the specified index position to the specified value and return the value before modification. D: Get function int indexof (object o): return the index of the first occurrence of the specified element in the collection. Object get (int index): get the element at the specified location listiterator(): list iterator e: intercept function list sublist (int fromindex, int toindex): intercept the collection.
Common methods in set interface:
The elements under the set interface are out of order and cannot be repeated. It is divided into HashSet and TreeSet.
HashSet
The underlying data structure is hash table, which is unsafe and efficient. Ensuring uniqueness depends on two methods: hashcode () and equals (). Sequence: judge whether the hashcode () value is the same. Same: continue to walk equals() to see the return value. If true, it will not be added to the collection. If false: added to the collection. Different: just add to the collection.
TreeSet
The underlying data structure is a binary tree, which is unsafe and efficient. The method to ensure element uniqueness depends on whether the return value is 0. Two ways to ensure sorting: natural sorting (elements with comparability): implement comparable interface; comparator sorting (sets with comparability): implement comparator interface
4、 Iterator
Iterator. As can be seen from the first Java collection class diagram, except for the collection in the middle, the iterator is placed on the top left
1. Use steps:
1. Gets the iterator object from the collection object. 2. Judged by iterator object. 3. Obtained through an iterator object.
2. Iterator principle
Because the data structures of multiple sets are different, the storage methods are different, and the retrieval methods are also different. At this time, the judgment and acquisition functions are defined in an interface. In the future, when traversing which collection, as long as the interface is implemented inside the collection. [iterator mode]
3. Collection stores strings and custom objects and iterates through them
4. Listiterator iterator is a sub interface of iterator
Therefore, there are three traversal modes of list: 1. Iterator iterator 2. Listiterator iterator 3. Common for + get ()
5、 Map < key value pair interface >
A map is a collection of key value pairs. Its elements are composed of keys and values. The key of map is unique, and the value can be repeated.
Common methods in map interface:
A: Add function V put (k key, V value): when the key does not exist in the set, add an element; Replace element when key exists B: judge whether the specified key exists in the set Boolean containsvalue (object value): judge whether the specified value exists in the set Boolean isempty(): judge whether the set is empty C: delete function void clear(): clear all key value pair data D: obtain function object get (object key) : get value according to key set < k > keyset(): collection of all keys collection < V > values(): collection of all values set < map Entry < K, V > > entryset(): returns the set view of the mapping relationship contained in this mapping e: length function int size()
Map includes HashMap, hashtable and treemap. Among them, hashtable has been basically replaced by HashMap, and hashtable is basically not used in the new code (Note: HashMap supports null, hashtable does not support null)
Traversal method of map:
1. Key value finding:
2. Key value pair
Through the above learning, we have a general understanding of the class diagram of the collection. Later, we will go deep into our most commonly used knowledge points, such as ArrayList HashMap.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.