Java Collections Framework (Note 18)

Java Collections Framework

An overview of the collection framework

Usually, a class of things with the same properties is aggregated into a whole, which can be called a set.
The Collections Framework is a unified standard architecture for representing and manipulating collections.
Any collection framework contains three major parts: the external interface, the implementation of the interface, and the algorithm for the collection operation.
Interface: An abstract data type that represents a collection. Interfaces provide the possibility for us to operate individually on the content represented in the collection.
Implementation: That is, the concrete implementation of the interface in the collection framework. Actually they are those reusable data structures.
Algorithm: A method that performs some useful computation on an object that implements an interface in a collections framework, such as searching, sorting, etc. These algorithms are usually polymorphic because the same method can behave differently when the same interface is implemented by multiple classes. In fact, algorithms are reusable functions.

Array: The length cannot be changed, all elements in the array are of the same class
Collection class: used to store a set of objects, variable length 

2. Introduction to Java Collection Framework Classes

Level 1: Java Collections Framework
Level 2: Collection (interface)-|- Map(interface)
Level 3: List   Set -|- HashMap TreeMap
四级:ArrayList LinkedList Vector HashSet   TreeSet -|- LinkedHashMap
            Level 5: LinkedHashSet

3. Collection interface

The Collection interface provides a set of methods for manipulating batches of objects
It provides basic operations like add, delete. It also supports query operations such as the isEmpty() method if it is empty. In order to support independent operations on Collections, Java's collection framework provides an Iterator, which enables you to operate a Collection generically without knowing what the specific implementation type of this Collection is.
The following gives all the functionality of Collection, that is, what you can do with Set and List (excluding methods that are automatically inherited from Object). (List also has some extra features.)
boolean add(Object): Make sure the container can hold the parameter you pass it. Returns false if it is not added. (This is an "optional" method, explained later in this chapter.)
boolean addAll(Collection): Add all elements contained in the parameter Collection. Returns true as long as an element is added.
void clear(): Clears all elements stored in the container. ("optional")
boolean contains(Object): Returns true if the container holds the parameter Object.
boolean containsAll(Collection): Returns true if the container holds all the elements contained in the parameter Collection.
boolean isEmpty(): Returns true if no elements are stored in the container.
Iterator iterator(): Returns an Iterator that can move between elements of the container.
boolean removeAll(Collection): Remove the elements contained in all parameter Collections in the container. Returns true whenever something has been deleted. ("optional")
boolean retainAll(Collection): Only save the elements included in the parameter Collection (the concept of "intersection" in set theory). Returns true if there has been a change. ("optional")
int size(): Returns the number of elements contained in the container.
Object[] toArray(): Returns an array containing all the elements in the container.
Object[] toArray(Object[] a): Returns an array containing all the elements in the container, and this array is not an ordinary Object array, its type should be the same as that of the parameter array a (type conversion is required).
Note that there is no get() method for random access here. This is because Collection also includes Set. And Set has it's own internal order (so random access is pointless). So if you want to check the elements of a Collection, you have to use an iterator.

The root interface in the Collection hierarchy. Collection represents a set of objects, these objects are also called the elements of the collection  
The JDK does not provide any direct implementation of this interface: it provides more specific implementations of subinterfaces such as Set and List
Common methods declared in the Collection interface
boolean add(E e) //Add elements to the collection
boolean addAll(Collection<? extends E> c) //Put another collection into the current collection
boolean remove(Object o) //Remove an element from the collection
boolean contains(Object o) //Determine whether a collection contains an element
boolean containsAll(Collection<?> c) ​​//Determine whether a collection contains all elements in another collection
boolean removeAll(Collection<?> c) ​​//Remove the same element from the collection as the element in the parameter collection
boolean retainAll(Collection<?> c) ​​//Take the intersection
clear() //Clear the collection
size() //get the size of the collection
toArray() // Convert the collection to an array
boolean isEmpty() //Determine whether the collection is empty
Iterator<E> iterator() //Used to iterate the collection

3.1 Add class operation of Collection
boolean add(E e) //Add elements to the collection
boolean addAll(Collection<? extends E> c)
static void demo1(){
//Collection c=new Collection(); => Error Collection is an interface and cannot be new
Collection c=new ArrayList();
c.add("first day");
c.add("The next day");
c.add("The third day");
c.add("fourth day");
c.add(true);
c.add(9999);
c.add(new Person());
System.out.println(c); //[Day 1, Day 2, Day 3, Day 4, true, 9999, Person@9e5c73]
Collection c2=new ArrayList();
c2.add("Watermelon");
c2.add("桃");
c.addAll(c2); //The parameter of this method is a Collection type
System.out.println(c); //[Day 1, Day 2, Day 3, Day 4, true, 9999, Person@9e5c73, Watermelon, Peach]
System.out.println(c.size());  //9
}

3.2 Collection delete class operation
boolean remove(Object o) //Remove the first occurrence of an element from the collection
boolean removeAll(Collection<?> c) ​​//Remove the same element from the collection as the element in the parameter collection
clear() //Clear the collection
static void demo2(){
Collection c=new ArrayList();
c.add("first day");
c.add("The next day");
c.add("The third day");
c.add("fourth day");
c.add(true);
c.add(9999);
c.remove(true); //Remove the value of true in the above set
c.remove("The third day"); 
System.out.println(c); //[first day, second day, fourth day, 9999]
Collection c2=new ArrayList();
c2.add("First day");
c2.add("The next day");
c2.add("Irrelevant");
c.removeAll(c2);
System.out.println(c); //[4th day, 9999]
c.clear();
System.out.println(c); //[] has no content
Collection c3=new ArrayList();
c3.add("一");
c3.add("一");
c3.add("一");
c3.remove("一");
System.out.println(c3); //[one, one] it removes the first occurrence of an element
}

3.3 Judgment operation of Collection
boolean contains(Object o) //Determine whether a collection contains an element
containsAll(Collection<?> c) ​​//Determine whether a collection contains all elements in another collection
boolean isEmpty() //Determine whether the collection is empty
static void demo3(){
Collection c = new ArrayList();
c.add("first day");
c.add("The next day");
c.add("The third day");
c.add("fourth day");
System.out.println(c.contains("The next day")); //true
Collection c2 = new ArrayList();
c2.add("First day");
c2.add("The next day");
c2.add("九");
System.out.println(c.containsAll(c2)); //false
System.out.println( c2.isEmpty());  //false
c2.clear();
System.out.println( c2.isEmpty());  //true;
System.out.println(c2.size()); //0 size() of an empty set is 0
}

3.4 Query operation of Collection
boolean retainAll(Collection<?> c) ​​//Take the intersection
static void demo4(){
Collection c = new ArrayList();
c.add("first day");
c.add("The next day");
c.add("The third day");
c.add("fourth day");
Collection c2 = new ArrayList();
c2.add("First day");
c2.add("The next day");
c2.add("fruit");
c2.add("糖");
c.retainAll(c2);  //?
System.out.println(c); // [first day, second day], if there is no intersection, the result will be empty
}

3.5 Iterator interface
static void demo4(){
Collection c = new ArrayList();
c.add("first day");
c.add("The next day");
c.add("The third day");
c.add("fourth day");
Iterator it =c.iterator(); //All collections inherited from Collection have this method, which can return an iterator
while(it.hasNext()){
String str=(String)it.next();
System.out.println(str); 
}
}
 
//This example demonstrates the inappropriate use of it.next()
static void demo5(){
Collection c = new ArrayList();
c.add("first day");
c.add("The next day");
c.add("The third day");
c.add("fourth day");
Iterator it =c.iterator(); //All collections inherited from Collection have this method, which can return an iterator
while(it.hasNext()){
System.out.println(it.next());  //java.util.NoSuchElementException
System.out.println(it.next());
System.out.println(it.next());
}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325565059&siteId=291194637