Linear table, linked list, hash table is a commonly used data structure, in the Java development, JDK has provided us with a series of corresponding classes to achieve the basic data structure.
These classes are in the Java.util package. This paper attempts to explain the roles of the classes and how to use them correctly by a simple description.
Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
Map
├Hashtable
├HashMap
└WeakHashMap
Collection interface
Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements). Some collection allow the same elements while others do not. Some can be sorted and others not. The Java SDK does not provide classes that directly inherit from collection, and the Java SDK provides classes that inherit from collection such as list and set.
All classes that implement the collection interface must provide two standard constructors: parameterless constructors are used to create an empty collection, and a constructor for a collection parameter is used to create a new collection. This new collection has the same element as the incoming collection. The latter constructor allows the user to replicate a collection.
How do I traverse every element in the collection? Regardless of the actual type of collection, it supports a iterator () method that returns an iteration that can be used to access each element of the collection one at a time. Typical usage is as follows:
Iterator it = collection.iterator(); // 获得一个迭代子
while(it.hasNext()) {
Object obj = it.next(); // 得到下一个元素
}
The two interfaces that are derived from the collection interface are list and set.
Main methods:
Boolean Add (Object o) Adding objects to the collection
Boolean remove (object o) deletes the specified object
int size () returns the number of elements in the current collection
Boolean contains (object O) to find if the specified object is in the collection
Boolean IsEmpty () to determine whether the collection is empty
Iterator iterator () returns an iterator
Boolean containsall (Collection c) to find out if the collection has elements in set C
Boolean AddAll (Collection c) adds all the elements in the collection C to the collection
void Clear () deletes all elements in the collection
void RemoveAll (Collection c) removes elements from the collection that are also in the C collection
void Retainall (Collection c) Removes elements that are not contained in collection C from the collection