1 Collections Overview
- Set : a collection container provided in java, it can be used to store a plurality of data.
Collection and an array of differences: - Length of the array is fixed. The collection length is variable.
- Stored in the array element of the same type, you can store basic data type value. Collection of objects are stored. And the type of the object can be inconsistent. When the object is generally more time, use the collection is stored in development.
A set of frame 2
Memory structure according to which a collection may be divided into two categories, namely, a set of single java.util.Collection
and dual column set java.util.Map
, where the record about the main Collection
set.
- Collection : root interface separate collections for storing a series of elements that meet certain rules, it has two important sub-interfaces, respectively,
java.util.List
andjava.util.Set
. WhereinList
the characteristic element is ordered, the element can be repeated.Set
The disorder is characterized by an element, and not repeatable.List
The main interface implementation class therejava.util.ArrayList
andjava.util.LinkedList
,Set
the main interface implementation class hasjava.util.HashSet
andjava.util.TreeSet
.
As shown below:
the collection itself is a tool, it is stored in the java.util package. In Collection
the interface definition of the most common single set of frame content.
3 Collection commonly used functions
Collection of all single parent interface is set, thus defining a single common set of methods (and List Set) in Collection, these methods can be used to operate all separate collection. Methods as below:
public boolean add(E e)
: The given object to the current collection.public void clear()
: Empty the collection of all the elements.public boolean remove(E e)
: Delete the given object in the current collection.public boolean contains(E e)
: Analyzing current collection contains the given object.public boolean isEmpty()
: To determine whether the current collection is empty.public int size()
: Returns the number of elements in a set.public Object[] toArray()
: The set of elements stored in the array.
Method Example:
Collection<String> list = new ArrayList<>();//集合为空
list.add("a");//[a]
list.add("a");//[a,a]
list.clear();//[]
list.add("b");//[b]
list.add("b");//[b,b]
list.add("c");[b,b,c]
System.out.println(list.contains("c"));//true
System.out.println(list.isEmpty());//false
System.out.println(list.size());//3
Object[] objects = list.toArray();
for (Object object : objects) {
System.out.println(object);//b,b,c
}