The Architecture of Collections and Collection Sets
-
The architecture of collections:
-
Single column collection: Collection interface
- List interface: element repeatable
ArrayList class, LinkedList class - Set interface: Elements cannot be duplicated
HashSet class, TreeSet class
- List interface: element repeatable
-
Double Column Set: Map Interface
HashMap class
-
-
Collection collection:
Create objects: Collection < String > C = new ArrayList ();
Common method: A common method for all single column sets.
Boolean add (E): Add elements, return values indicate whether the addition was successful, and return fixed values true using the ArrayList collection.
boolean remove(Object o): Remove the specified element from the collection, and the return value indicates whether the removal was successful
void clear(): empty the elements in the collection
boolean contains(Object o): Determines whether a specified element exists in a collection, and true denotes inclusion
boolean isEmpty(): Determines whether the collection is empty, and true represents empty
int size(): The length of the collection, which is the number of elements in the collection. -
Collection set traversal-iterator
//1. Obtain iterator object by iterator method of set. //2. Cycle to determine whether there is the next element. it.hasNext() method while(it.hasNext()){ //3. If you have the next element, you get the next one. it.next() method String s = it.next(); System.out.println(s); }
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class CollectionDemo { public static void main(String[] args) { // Create Collection Objects Collection<String> array = new ArrayList<String>(); // Adding elements array.add("Lin Qingxia"); array.add("The wind is clear and clear"); array.add("Zhang Manyu"); // Removing Elements // array.remove("wind blowing"); // Empty element // array.clear(); // System.out.println(array.contains("Lin Qingxia") // System.out.println(array.contains("Liu Dehua"); // System.out.println(array.isEmpty()); // System.out.println(array.size()); // System.out.println(array); Iterator<String> it = array.iterator(); while (it.hasNext()) { String s = it.next(); System.out.println(s); } } }
Traversal of list set and data structure
-
The characteristics of List collections:
1. Order: The order of storage and acquisition is the same.
2. Element repeatability
3. With index, you can use fori to traverse. -
List collection common methods:
void add(int index,E element): Insert the specified element at the specified location in this collection
E remove(int index): Delete the element at the specified index and return the deleted element
E set(int index,E element): Modify the element at the specified index to return the modified element
E get(int index): Returns the element at the specified index -
Summary: In development, we usually use List collections instead of Collection collections.
Method Classification:- Add/add:
Boolean add (e): Add element e at the end of the collection
Void add (int index, e): Add element e in index - Delete/remove:
Boolean remove (e): Delete by element e
E remove(int index): Delete elements according to index index - Change/Set:
E set(int index,E element): The element at index index is reset to an element, and the return value is a modified element. - Check/obtain:
E get(int index): Get elements by index
int size(): Gets the length of the collection - Traversal mode:
1. Preferred: fori traversal, only for indexed collections.
2. iterator, but not recommended, troublesome.
- Add/add:
-
Iterator concurrent modification exception: Concurrent ModificationException
Cause: There is a variable modCount in the collection that records the number of modifications (additions and removals). Adding and deleting elements modCount with a collection will be ++, also known as actual modification times ++;
There is also a variable expectedModCount in iterator iterator that expects the number of modifications. The default value is equal to the set modCount; whenever we call next() of iterator.
Method, expectedModCount and modCount are compared internally, and if not, Concurrent ModificationException concurrency is thrown
Modify exceptions.
ps: The length of the collection changes, and modCount in the collection is ++; -
Solution:
The first is to use fori traversal instead of iterator.The second is to traverse the collection by using ListItr, its subclass, instead of using Itr iterator, in the form of listIterator().
But adding and deleting elements use the add method and remove() provided in the iterator. -
Enhanced for loop: The third traversal method uses an iterator at the bottom, which is equivalent to a simplified traversal method of the iterator.
Syntax: Used to traverse collections and arrays For (Collection/Array Element Type Variable Name: Collection/Array Object){ // In a loop, variables are saved as a result of each traversal. }
Keyboard shortcut: collection / array object. for + carriage return
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ListDemo { public static void main(String[] args) { // Create List Collection Objects List<Student> list = new ArrayList<Student>(); // Adding elements list.add(new Student("The wind is clear and clear", 28)); list.add(new Student("Lau Andy", 49)); list.add(new Student("Eason Chan", 23)); list.add(new Student("Zhou Xingchi", 36)); // Variable Set Iterator<Student> it = list.iterator(); // Ergodic set for (int i = 0; i < list.size(); i++) { Student stu = list.get(i); System.out.println(stu); } System.out.println("--------"); while (it.hasNext()) { Student stu = it.next(); System.out.println(stu); } } }
Three Ergodic Ways of Sets
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ListDemo2 { public static void main(String[] args) { List<Student> list = new ArrayList<Student>(); list.add(new Student("Jiangxi University of Finance and Economics", 90)); list.add(new Student("Tsinghua University", 130)); list.add(new Student("Peking University", 100)); list.add(new Student("Jiangxi Agricultural University", 69)); // The first traversal for (int i=0; i<list.size(); i++) { Student stu = list.get(i); System.out.println(stu.getName() + ", " + stu.getAge()); } System.out.println("--------------------"); // The second traversal Iterator<Student> it = list.iterator(); while (it.hasNext()) { Student stu = it.next(); System.out.println(stu.getName() + ", " + stu.getAge()); } System.out.println("--------------------"); // The third traversal for (Student stu : list) { System.out.println(stu.getName() + ", " + stu.getAge()); } } }
ArrayList collection and LinkedList collection
-
Stack structure: FIFO. For example, cartridge clips
-
Queue structure: first show. For example: Waiting in line for meals.
-
Array Structure: Quick Query, Slow Addition and Deletion, ArrayList is an array structure at the bottom
-
LinkedList structure: slow query, fast addition and deletion, LinkedList bottom is linked list structure