very happy to know everyone here, I am also just contact with the backend development of the learner, I believe many friends in the study will encounter a lot of headache problems, I hope we can all share the problem, the study of their own ideas to sort out, we explore together to GROW.
Today I am here to briefly explain my understanding of the collection in the Study. Many friends write explanations, not from the simplest, causing the confusion in reading, perhaps I can help you to have a new understanding of the Collection.
first, when we talk about collections, we need to understand the specific concepts of collections.
The concept of a Java collection is relative to an array, so what is an array?
Array: An array is a contiguous storage unit. Each storage unit has the same amount of space. the type is uniform, and the length of the array is immutable (after the definition).
The same array can hold only the same type of Data. (basic data type or reference data Type)
It would be easy to understand the collection Again.
Collection: 1, You can manipulate and store a set of data that is not Fixed.
2, All collections in Java are located in the Java.util in the Package.
The 3,java collection can only hold reference data types and cannot hold basic data types.
4, do not limit the length of the collection, you can add any number of Elements.
5, the qualified element in the collection becomes generic
So after we understand the basic concept of the collection, look at the classification of the collection? The first thing we need to know is the concept of Interfaces.
What is an interface?
What is an interface: an interface is a collection of method features------interfaces are abstractions of abstractions.
What is an abstract class: an abstract class that implements a partial implementation of a specific type------abstract class is a concrete Abstraction.
Method features Include: The name of the method, the number of arguments, and the type of the Parameter. Not included: return type , parameter name, and thrown Exception.
An interface is a precondition for type conversion and a guarantee of dynamic Invocation. A type conversion ( multiple inheritance ) is accomplished by implementing an interface, and the dynamic invocation only cares about the type and does not care about the specific class.
Because the interface must be involved when we talk about Collections. Let's take a look at the basic classification of the Collection. Let's look at the picture below.
1. Collection Interface (because The set itself is also an interface)collection is the most basic set interface, which defines a set of objects that are allowed to be duplicated. The collection interface derives two sub-interface sets and lists, respectively defining two different storage methods, as Follows:
2, set interface
the set interface inherits from the collection interface, which does not provide an additional method, but the elements in the collection class that implement the set interface are unordered and non-repeatable.
Features: unordered and Non-repeatable.
3. List interface
The list interface also inherits from the collection interface, but, contrary to the set interface, the elements in the collection class of the list interface are object-ordered and Repeatable.
Features: ordered and Repeatable.
Two important implementation classes: ArrayList and LinkedList
1.ArrayList features are ordered and repeatable
2.LinkedList is a doubly linked list Structure.
4. Map Interface
map is also an interface, but does not inherit the collection Interface. This interface describes a key-to-value mapping that is never duplicated. The map interface is used to maintain key/value pairs (key/value pairs).
Feature: It describes a mapping of key-to-value values that are never duplicated.
Two important implementation Classes: HashMap and TreeMap
1.HashMap, The Chinese call Hash list, based on the hash table implementation, the characteristic is the key value Pair's mapping Relation. A key corresponds to a Value. The ordering of elements in HashMap is not fixed. More suitable for inserting, deleting, and positioning elements.
2.TreeMap, based on the red and Black book Implementation. The elements in the TreeMap maintain some sort of fixed order. More suitable for sequential traversal of Elements.
5.Iterator Interface
Iterator , there is an exception in C # IEnumerator, They are all collection accessors that iterate over the objects in the Collection. all container classes that implement the collection interface have a iterator method that returns an object that implements the iterator Interface. The iterator object is called an iterator, and theiterator interface method iterates through each element of the collection iteratively, and can remove the appropriate element from the Collection.
A method of implementing classes for some sets;
Arrays.hashcode: used to calculate its hash value based on the contents of the array (the array object is hashCode()
not available). This method aggregates the features of the auto-boxing and parameterless variables of Java 5 to enable a variable to be passed to the Arrays.hashcode
method Quickly-just passing in the value, not the Object.
Arrays.sort: sorts the entire array or part of an array. You can also use this method to sort an array of objects with a given comparer.
Arrays.tostring: Prints the contents of the Array.
All collections can be T[] Collection.toArray( T[] a )
copied to the array using this METHOD. is usually called in this way,
1 |
<span class = "hljs-keyword" > return coll.toArray( <span class = "hljs-keyword" > new T[ coll.size() ] );</span></span> |
This method allocates a large enough array to store all the collections so that toArray
no more space is allocated when the value is Returned.
How do I iterate through the elements in the collection?
A:
Iterator It=al.iterator ();
while (it.hasnext ())
{
SOP (it.next ());
}
B:
For (Iterator it=al.iterator (); it.hasnext ();)
{
SOP (it.next ());
}
C:
for (String Alist:al)
{
System.out.println (alist);
}
Our discussion of the collection is here for the time being and we can add the content Later.
Discussion of collections in basic Java learning