set:
Variable length, can only store reference data types, store basic data types, use packaging classes
collection: single-column list can be repeated set can not be repeated
Map: Double column
1.2 Collection class architecture [understanding]
Set
Collection: single Map: double row
List: Repeatable set: non-repeatable HashMap TreeMap
ArrayList LinkedList HashSet TreeSet 接口
Implementation class
1.3Collection Overview and Use [Application]
-
Collection overview
- It is the top-level interface of a singleton collection, which represents a group of objects, which are also called collection elements
- JDK does not provide any direct implementation of this interface. It provides more specific implementations of sub-interfaces (such as Set and List)
-
Create Collection Object
- Polymorphic way
- Specific implementation class ArrayList
-
Collection collection methods
Method name Description boolean add(E e) Add element boolean remove(Object o) Remove the specified element from the collection boolean removeIf(Object o) Remove according to conditions void clear() Clear the elements in the collection boolean contains(Object o) Determine whether the specified element exists in the collection boolean isEmpty() Determine whether the collection is empty int size() The length of the collection, which is the number of elements in the collection
Iterator:
1.4Collection of traversal [application]
-
Introduction to iterators
- Iterators, special traversal methods for collections
- Iterator iterator(): Returns the iterator of the elements in this collection, obtained by the iterator() method of the collection object
-
Common methods in Iterator
boolean hasNext(): Determine whether there is an element at the current position that can be taken out
E next(): Get the element at the current position and move the iterator object to the next index position -
Collection traversal
-
An iterator can only be used once
-
import java.util.ArrayList; import java.util.Iterator; public class MycollectonDemo5 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("b"); list.add("c"); list.add("d"); list.add("e"); //迭代->删除 只能用迭代器的删除方法 //一个迭代器只能用一遍 Iterator<String> it = list.iterator(); while(it.hasNext()){ String next = it.next(); if("b".equals(next)){ it.remove(); } } System.out.println(list); } }
1.5 Enhanced for loop [application]
-
Introduction
- It appeared after JDK5, and its internal principle is an Iterator iterator
- Only classes that implement the Iterable interface can use iterators and enhance for
- Simplify traversal of arrays and collections
-
format
for (data type variable name of elements in collection/array: collection/array name) {
// The element currently traversed has been encapsulated into a variable, just use the variable directly
}
-
Code
-
import java.util.ArrayList; import java.util.Iterator; public class MycollectonDemo6 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("e"); //迭代器遍历 Iterator<String> it = list.iterator(); while (it.hasNext()){ String next = it.next(); System.out.println(next); } System.out.println("-------------"); //for加强遍历,因为获取不到元素 for(String str : list){ System.out.println(str); } } }
2.List collection
2.1 Overview and characteristics of List collection [memory]
- Overview of List collection
- Ordered collection, where ordered refers to the order of access
- The user can precisely control the insertion position of each element in the list, and the user can access the element through the integer index and search for the element in the list
- Unlike Set collections, lists usually allow duplicate elements
- Features of List collection
- Orderly access
- Can be repeated
- Indexed
2.2 The unique method of List collection [application]
-
Method introduction
Method name description void add(int index,E element) Insert the specified element at the specified position 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 and return the modified element E get(int index) Returns the element at the specified index
import java.util.ArrayList;
import java.util.List;
public class MyListDemo2 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("ddd");
list.add("ccc");
//add
list.add(0,"ooo");
System.out.println(list);
//remove
list.remove("ooo");
System.out.println(list);
//修改指定索引
list.set(0,"ooo");
System.out.println(list);
//get返回指定的索引
String s = list.get(0);
System.out.println(s);
}
}
3. Data structure
3.1 Stack and queue of data structure [memory]
-
Stack structure
First in last out
-
Queue structure
First in, first out
3.2 Array and Linked List of Data Structure [Memory]
-
Array structure
Fast query, slow addition and deletion
-
Queue structure
Slow query, fast addition and deletion
4. List collection implementation class
4.1 Features of List Collection Subclass [Memory]
-
ArrayList collection
The bottom layer is the realization of the array structure, which is fast in query and slow in adding and deleting
-
LinkedList collection
The bottom layer is the realization of the linked list structure, which is slow in query and fast in adding and deleting
4.2 Unique functions of LinkedList collection [application]
-
Unique method
Method name Description public void addFirst(E e) Insert the specified element at the beginning of the list public void addLast(E e) Append the specified element to the end of this list public E getFirst() Returns the first element in this list public E getLast() Returns the last element in this list public E removeFirst() Remove from this list and return the first element public E removeLast() Remove from this list and return the last element