A collection can be thought of as a container, for example, all Java-related books can be considered as a collection. It is easy to store the objects in the collection in the collection, and it is easy to remove them from the collection, and they can be placed in a certain order. Different collection classes are available in Java, which have different ways of storing objects and provide methods to facilitate the user to iterate over the collection, add, delete, and find the specified object.
The collection class is also called a container, and it is natural for the container to think of the array, so what are the differences between the collection class and the group? First, the length of the array is fixed, and the length of the collection is variable; second, the array is used to hold the basic data type, and the collection is used to hold the object's reference. Commonly used collections are divided into two major categories, the Map class and the collection class, and the collection class includes the Set class and the list class, the inheritance relationship between them can be expressed in the following diagram:
The collection interface is the root interface in the hierarchy, and its constituent units are called elements. The collection interface is usually not used directly, but provides methods for adding elements, deleting elements, and managing data. As can be seen from the above inheritance graph, the set and list classes inherit the collection interface, so these methods are also common to set and list classes. So, next, let's look at the common methods of the collection class.
The common methods of collection collection are as follows:
Boolean Add (Object element); Adding elements to the collection
void Clear (); Emptying the collection
Boolean contains (Object O); Determines whether an element is contained in the collection
Boolean isEmpty (); Determine if there are elements in the collection
Iterator<e> Iterator (); Gets the iterator object on which the collection depends
Boolean remove (Object O); Delete an element in a collection
int size (); Gets the number of elements in the collection
Object[] ToArray (); Convert a collection to an array
Next we look at the use of these methods. See the following procedure:
Import java.util.*;
PublicClasscollectiontest01{
PublicStaticvoidMainString[] (args) {
1. Create a Collection
Collection c=NewArrayList ();
2. Adding elements
C.Add1);
C.AddNewInteger (100));
C.AddNewObject ());
Customer cus=NewCustomer ("JACK,"20);
3. Get the number of elements
System.Out.println (C.size ());4
System.Out.println (C.isempty ());False
4. Converting a collection to an array of type Object
Object[] Objs=c.toarray ();
ForIntI=0;i<objs.length;i++) {
System.Out.println (Objs[i]);
}
5. Clear
C.clear ();
System.Out.println (C.size ());0
6. Determine if the collection is empty
System.Out.println (C.isempty ()); //true
}
}
Class customer{
String name;
int Age ;
Customer (String name,int age ) {
This.name=name;
This.age=age;
}
//Override the ToString method in object
Public String toString () {
return "customer[name=" +name+", age=" +age+"]";
}
}
Run output after compiling:
3
false
1
100
[email protected]15db9742
0
true
In the above code,
1. Create a collection, note here, because Collection is an interface, so you can not directly Collection c=new Collection (); We can look for an implementation class below it (refer to the diagram of the inheriting class above) ArrayList, i.e. collection c=new ArrayList ();
2. Add elements, C.add (1); Here is the knowledge of automatic boxing, forget to turn to the front to see Ah, C.add (new object); Equivalent to Object O=new object (); C.add (o); That means collection can only store reference types. We then give ourselves a different type of customer, and rewrite the ToString () method in object, and then add the element C.add (CUS) in the main () method;
3. Get the number of elements, using c.size ();
4. Convert the collection to an array of type object, using the method ToArray (); Then use a For loop to traverse;
1
100
[Email protected]
0
5. Clear the collection, using C.clear (); System.out.println (C.size ()) will be found after retrieving the number of elements again; the output is 0;
6. In the middle we interspersed c.isempty (); That is to judge whether the set is empty;
Next we look at the iterator iterator () method. Look at the following code:
Import java.util.*;
PublicClasscollectiontest02{
PublicStaticvoid Main ( string[) args) {
//Create collection Object
collection c= New linkedlist ();
//add element
c. Add ();
c. Add ( 3.14);
c. Add ( false);
//Iterate, traverse
//1. Get iterator Object
iterator it=c.iterator ();
//2. Start calling method, complete traversal, iteration
while (It.hasnext ()) {
object Element=it.next ();
system. out.println (Element);
&NBSP;}
///or use for loop
for (Iterator it=c.iterator (); It.hasnext ();) {
object O=it.next ();
&NBSP;SYSTEM.OUT.PRINTLN (o);
&NBSP;} */
}
}
In the above code,
1. Gets the iterator object, the collection object C has been created, and the iterator () method is called directly when iterating, that is, the iterator =c.iterator (); Here C may be arraylist,hashset,linkedlist the various collection objects, so call iterator () implements a different iterator, but all iterators implement the iterator interface, so you can directly iterator it= C.iterator (); in other words, iterators are programming directly to the interface, and there is no specific implementation class, it is a reference, the memory address is saved, and the iterator object in the heap is pointed.
2. Start calling method, complete traversal, iterate
Iterator iterator inside there are three methods, Hasnext () to determine whether there are more elements in the iterator, if any, return true, otherwise return false; The next () method moves the iterator down one bit, and takes out the pointed element, but note that before the next () method is called, the Hasnext () method must be called to determine if there are any elements before invoking a problem. The two traversal modes of while and for loops are described here.
Java collection Classes