List, Stack, Queue, Set and Map definitions and common methods

Insert picture description here

set

Two main types in the Java container library

1. Only one element is stored in each slot
  Collection:描述所有序列容器的共性的根接口;
  List:以特定的顺序保存一组元素;
  Set:元素不能重复;
  Queue:只允许在容器的一端插入对象,并从另一端移除对象。
2. Two objects are saved in each slot
  Map:键与之相关联的值。

List

1. Method summary:
  • add(E e): append the specified element to the end of this list (optional operation);
  • contains(Object o): If this list contains the specified element, it returns true;
  • remove(int index): delete the element at the specified position in the list (optional operation), the equels() method will be used;
  • indexOf(Object o): Returns the index of the first occurrence of the specified element in this list. If this list does not contain an element, it returns -1. The equels() method will be used;
  • subList(int fromIndex, int toIndex): Returns the view between fromIndex (inclusive) and toIndex specified in this list;
  • containsAll(Collection<?> c): If this list contains all the elements of the specified collection, it returns true.
  • retainAll(Collection<?> c): Only retain the elements in this list contained in the specified collection (optional operation), the resulting behavior depends on equals();
  • removeAll(Collection<?> c): Remove all elements contained in the specified collection from this list (optional operation), the resulting behavior depends on equals();
  • set(int index, E element): Replace the element at the specified position in this list with the specified element (optional operation);
  • isEmpty(): If this list contains no elements, it returns true;
  • clear(): delete all elements from this list (optional operation);
2. ArrayList: It is longer than random access elements, but it is slower to insert and remove elements in the middle of the List;
3. LinkList: It provides optimized access through low-cost insertion and deletion operations in the middle of the List.
  • Implemented the basic List interface
  • getFirst() and element() return the head of the list instead of removing it. If the List is empty, a NoSuchElementException will be thrown; if the list is empty, the peek() method will return null;
  • removeFirst() and remove() remove and return the head of the list, and throw NoSuchElementException when the list is empty; poll() returns null if the list is empty;
  • removeLast() removes and returns the last element of the list.

Stack

1. The first-in-last-out (LIFO) LinkList has a method to directly implement all the functions of the stack, so you can directly use the LinkedList as a stack. Sometimes a real "stack" can make things clear.
	public class Stack<T> {
    
      
    private LinkedList<T> storage = new LinkedList<T>();  
    public void push(T v){
    
     storage.addFirst(v); }  
    public T peek(){
    
     return storage.getFirst(); }  
    public T pop(){
    
     return storage.removeFirst(); }  
    public boolean empty(){
    
     return storage.isEmpty(); }  
    public String toString(){
    
     return storage.toString(); }  
}

Queue

1. The queue is a typical first-in-first-out (FIFO) container, that is, things are put in one section of the container and taken out from the other end, and the order in which things are put into the container is the same as the order in which they are taken out.
2. Method summary:
* offer(E e) :如果在不违反容量限制的情况下立即执行,则将指定的元素插入到此队列中;
* peek() :检索但不删除此队列的头,如果此队列为空,则返回 null;
* element() :检索,但不删除,这个队列的头,为空会抛出NoSuchElementException; 
* poll() :检索并删除此队列的头,如果此队列为空,则返回 null 。
* remove() 检索并删除此队列的头,为空会抛出NoSuchElementException。
3. PriorityQueue

The priority queue declares that the next popped element is the most needed element (with the highest priority)

Set

1. HashSet: Use a rather complicated way to store elements, the fastest way to get elements, use a hash function;
2. TreeSet: Save the objects in ascending order of the comparison results and store them in the red-black tree data structure;
3. LinkedHashSet: Save objects in the order they are added; hashes are also used, and linked lists are used to maintain the insertion order of elements.

Map

1. Map can return a Set of its keys, its value is Collection, or its key-value pair is Set, keySet() returns a Set view of the keys contained in this map.
1. HashMap: Provides the fastest search technology, does not store elements in any obvious order;
2. TreeMap: Save the object keys in ascending order of the comparison result;
3. LinkedHashMap: Save keys in the order of insertion, while retaining the search speed of HashMap.

Iterator

1. Iterator is also a design pattern, it is an object, it traverses and selects objects in the sequence when it works;
2. Strange restrictions on iterators:
* 使用iterator()要求容器返回一个Iterator。Iterator将准备好返回序列的一个元素;
* 使用next()获得序列中的下一个元素;
* 使用hasNext()检查序列中是否还有元素;
* 使用remove()将迭代器信近返回的元素删除。

Guess you like

Origin blog.csdn.net/Zmd_23/article/details/108554176