Java is an object-oriented language, so the most common operation of objects when we write programs is objects. For this reason, Java provides some class libraries specifically used to process objects. We call the collection of these class libraries a collection framework. The Java Collection Toolkit is located under the Java.util package and contains many commonly used data structures, such as arrays, linked lists, stacks, queues, collections, hash tables, etc. Under the framework of learning Java collections, it can be roughly divided into the following five parts: List list, Set collection, Map map, iterator (Iterator, Enumeration), and tool classes (Arrays, Collections).
Java's collection class is mainly derived from two interfaces: Collection and Map. Collection and Map are the root interfaces of the Java collection framework, and these two interfaces include some interfaces or implementation classes.
It can be seen that Collection does not directly implement the class, but provides a more specific subinterface implementation. Through the query API, the basic functions of Collection can be summarized.
1. Add
boolean add(E e): add an element
boolean addAll(Collection<? extends E> c): add an element of a collection
Sample code
package collection;import java.util.ArrayList;import java.util.Collection;public class CollectionDemo1 { public static void main(String[] args) { Collection collection1 = new ArrayList(); // Collection is an interface, instantiating its subclass collection1.add("Dream of Red Mansions"); collection1.add("Romance of the Three Kingdoms"); System.out.println(collection1); Collection collection2 = new ArrayList(); collection2.add("Journey to the West"); collection2.add("Water Margin"); collection1.add(collection2); System.out.println(collection1); }}Output result:
[Dream of Red Mansions, Romance of the Three Kingdoms]
[Dream of Red Mansions, Romance of the Three Kingdoms, [Journey to the West, Water Margin]]
2. Delete
void clear(): Clear all elements in the collection
boolean remove(Object o): removes the specified element
boolean removeAll(Collection<?> c): Removes elements of a collection
Sample code
package collection;import java.util.ArrayList;import java.util.Collection;public class CollectionDemo2 { public static void main(String[] args) { Collection collection1 = new ArrayList(); // Collection is an interface, instantiating its subclass collection1.add("Dream of Red Mansions"); collection1.add("Romance of the Three Kingdoms"); collection1.add("Journey to the West"); collection1.add("Water Margin"); System.out.println("Initial status:" + collection1); boolean b1 = collection1.remove("Dream of Red Mansions"); System.out.println("Does the element be removed: " + b1); System.out.println("Remove an element: " + collection1); System.out.println(); Collection collection2 = new ArrayList(); collection2.add("Romance of the Three Kingdoms"); collection2.add("Journey to the West"); collection2.add("Ordinary World"); boolean b2 = collection1.removeAll(collection2); // Remove the intersection of two sets, and return true as long as one element is removed System.out.println("Remove a collection: " + collection1); System.out.println("Whether the element is removed: " + b2); System.out.println(); Collection collection3 = new ArrayList(); collection3.add("Ordinary World"); collection3.add("Siege"); boolean b3 = collection1.removeAll(collection3);// Remove the intersection of two collections, and return true as long as an element is removed System.out.println("Remove a collection: " + collection1); System.out.println("Whether the element is removed: " + b3); System.out.println(); collection1.clear(); System.out.println("Clear all elements in the collection: " + collection1); }}Output result
Initial state: [Dream of the Red Chamber, Romance of the Three Kingdoms, Journey to the West, Water Margin]
Whether to remove elements: true
Remove an element: [Romance of the Three Kingdoms, Journey to the West, Water Margin]
Remove a collection: [Water Margin]
Whether to remove elements: true
Remove a collection: [Water Margin]
Whether to remove the element: false
Clear all elements in the collection: []
3. Judgment
boolean contains(Object o): determines whether the set contains the specified element
boolean containsAll(Collection<?> c): determines whether the set contains the specified collection element
boolean isEmpty(): determines whether the set is empty
Sample code
package collection;import java.util.ArrayList;import java.util.Collection;public class CollectionDemo3 { public static void main(String[] args) { Collection collection1 = new ArrayList(); // Collection is an interface, instantiating its subclass collection1.add("Dream of Red Mansions"); collection1.add("Romance of the Three Kingdoms"); collection1.add("Journey to the West"); collection1.add("Water Margin"); System.out.println("Does it contain Dream of Red Mansions:" + collection1.contains("Dream of Red Mansions")); Collection collection2 = new ArrayList(); collection2.add("Romance of the Three Kingdoms"); collection2.add("Journey to the West"); collection2.add("Ordinary World"); System.out.println("Does it contain elements in a collection: " + collection1.containsAll(collection2)); // All that contains System.out.println("Does the collection be empty: " + collection1.isEmpty()); }}Output result
Does it include Dream of Red Mansions: true
Whether to include elements in a collection: false
Whether the set is empty: false
4. Obtain the length
int size(): Get the number of elements in the set
Sample code
package collection;import java.util.ArrayList;import java.util.Collection;public class CollectionDemo4 { public static void main(String[] args) { Collection collection = new ArrayList(); // Collection is an interface, instantiating its subclass collection.add("Dream of Red Mansions"); collection.add("Romance of the Three Kingdoms"); collection.add("Journey to the West"); collection.add("Water Margin"); System.out.println("Number of elements:" + collection.size()); }}Output result
Number of elements: 4
5. Intersection
boolean retainAll(Collection<?> c): retains elements in this collection that are also included in the specified collection (optional operation). In other words, remove all elements in this collection that are not included in the specified collection.
Sample code
package collection;import java.util.ArrayList;import java.util.Collection;public class CollectionDemo5 { public static void main(String[] args) { Collection collection1 = new ArrayList(); // Collection is an interface, instantiating its subclass collection1.add("Dream of Red Mansions"); collection1.add("Romance of the Three Kingdoms"); collection1.add("Journey to the West"); collection1.add("Water Margin"); Collection collection2 = new ArrayList(); collection2.add("Journey to the West"); collection2.add("Water Margin"); System.out.println(collection1.retainAll(collection2)); System.out.println("collection1:" + collection1); System.out.println("collection2:" + collection2); Collection collection3 = new ArrayList(); collection3.add("Journey to the West"); collection3.add("Ordinary World"); System.out.println(collection1.retainAll(collection3)); System.out.println("collection1: " + collection1); System.out.println("collection2: " + collection2); }}Output result
true
Collection1: [Journey to the West, Water Margin]
Collection2: [Journey to the West, Water Margin]
true
Collection1: [Journey to the West]
collection2: [Journey to the West, Water Margin]
From the above results analysis, it is known that the collection1 collection and the collection2 collection do intersection operation, and the final result remains in collection1, while the elements in collection2 remain unchanged.
6. Set to array
Object[] toArray(): Set to array
Sample code
package collection;import java.util.ArrayList;import java.util.Collection;public class CollectionDemo6 { public static void main(String[] args) { Collection collection = new ArrayList(); // Collection is an interface, instantiating its subclass collection.add("Dream of Red Mansions"); collection.add("Romance of the Three Kingdoms"); collection.add("Journey to the West"); collection.add("Water Margin"); Object[] objects = collection.toArray(); for (Object object: objects) { System.out.println(object); } }}Output result:
Dream of the Red Chamber of the Three Kingdoms Journey to the West Water Margin
7. Traversal
Iterator<E> iterator(): Returns an iterator that iterates on the element of this collection. Iterator is a collection-specific traversal method.
Iterator is an interface, which has 3 methods
boolean hasNext(): Return true if there are still elements that can be iterated.
E next(): Returns the next element of the iteration and moves to the next position.
void remove(): Remove the last element returned by the iterator from the collection pointed to by the iterator
Sample code
package collection;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class CollectionDemo7 { public static void main(String[] args) { Collection collection = new ArrayList(); // Collection is an interface, instantiating its subclass collection.add("Dream of Red Mansions"); collection.add("Romance of the Three Kingdoms"); collection.add("Journey to the West"); collection.add("Water Margin"); Iterator it = collection.iterator(); while (it.hasNext()) { Object object = it.next(); System.out.println(object); } it.remove(); // If the next method is not called, an IllegalStateException exception will be thrown System.out.println(collection); }}Output result:
Dream of the Red Chamber of the Three Kingdoms Journey to the West Water Margin
[Dream of Red Mansions, Romance of the Three Kingdoms, Journey to the West]
At this point, the method of the Collection interface has been introduced.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.