The idea of a set is that

Origin of sets

  • When a single data content needs to be recorded in a Java program, a variable is declared.
  • Declare a one-dimensional array when you need to record multiple data contents of the same type in a Java program.
  • When you need to record multiple types of data content in a Java program, you create an object.
  • When you need to record object data of multiple different types in a Java program, prepare a collection

Collection framework architecture

  • The top layer of the Collection framework in Java is the java.util.Collection Collection and the java.util.map Collection.
  • The basic unit for accessing elements in a Collection is a single element.
  • The basic unit for accessing elements in the Map set is: single pair elements.

The Collection Collection

The basic concept

  • The java.util.Collection interface is the parent of the List, Queue, and Set interfaces, and therefore the methods defined in this interface

Can be used to manipulate lists as well as queues and sets.

Common methods

Method statement Function is introduced
boolean add(E e); Adds objects to the collection
boolean addAll(Collection<? extends E> c); Use to add all elements from the collection c specified by the argument to the current collection
boolean contains(Object o); Checks whether the specified object is included
boolean containsAll(Collection<? > c); Checks whether all objects specified by the argument are included
boolean retainAll(Collection<? > c); Preserves all objects that exist in the current collection and exist in the parameter collection
boolean remove(Object o); Removes objects from the collection
boolean removeAll(Collection<? > c); Removes all objects specified by the argument from the collection
void clear(); Empty the collection
int size(); Returns the number of contained objects
boolean isEmpty(); Check whether it is empty
boolean equals(Object o); Determine equality
int hashCode(); Gets the hash code value of the current collection
Object[] toArray(); Convert a collection to an array
Iterator iterator() Gets an iterator for the current collection

The Iterator interface

The basic concept

  • Iterator the java.util.Iterator interface is used to describe Iterator objects that can iterate over all elements in a Collection.
  • The java.util.Collection interface inherits from the Iterator interface, so all implementation classes that implement the Collection interface can use the Iterator object.

Common methods

Method statement Function is introduced
boolean hasNext() Determine if there are elements in the collection that can be iterated/accessed
E next() Used to fetch one element and point to the next
void remove() Used to delete the last element accessed

For each cycle

The basic concept

  • Java5 introduced enhanced for loops that apply array and collection traversal.
  • Is a “simplified” version of the classic iteration.

Syntax format

for(Element type variable name: array/collection name) {loop body; }Copy the code

Execute the process

  • Continually fetching an element from the array/collection to the variable name and executing the body of the loop until all the elements are fetched.

The List collection

The basic concept

  • The java.util.List Collection is a subcollection of the Collection, which allows repeating elements to be placed in order.
  • The main implementation classes of this collection are ArrayList, LinkedList, Stack, and Vector.
  • The bottom layer of ArrayList class uses dynamic array for data management, which supports subscript access and makes it inconvenient to add and delete elements.
  • The bottom layer of LinkedList class adopts bidirectional LinkedList for data management, which makes it inconvenient to access and convenient to add and delete elements.
  • ArrayList and LinkedList can be considered to be logically identical except for performance differences. ArrayList is better suited for random access, while LinkedList is better suited for insert and delete. This difference can be ignored in cases where performance requirements are not particularly demanding.
  • The bottom layer of Stack class is the use of dynamic array for data management, which is mainly used to describe a data structure with last-in, first-out characteristics, called Stack (Last in First out LIFO).
  • The bottom layer of Vector class uses dynamic array for data management. Compared with ArrayList, this class is a thread-safe class with low efficiency, so it is basically not used in future development.

Common methods

Method statement Function is introduced
void add(int index, E element) Adds an element to the collection at the specified location
boolean addAll(int index, Collection<? extends E> c) Adds all elements to the collection
E get(int index) Gets the specified location element from the collection
int indexOf(Object o) Finds the object specified by the argument
int lastIndexOf(Object o) Reversely finds the object specified by the argument
E set(int index, E element) Modifies the element at the specified location
E remove(int index) Deletes the element at the specified location
List subList(int fromIndex, int toIndex) Used to get the child List

Set the Queue

The basic concept

  • The java.util.Queue Collection is a subcollection of the Collection and is hierarchical with the List Collection.
  • The main purpose of this set is to describe data structures with first-in, first-out characteristics, called queues (first in First out FIFO).
  • The primary implementation class for this collection is the LinkedList class because it has an advantage in addition and deletion.

Common methods

Method statement Function is introduced
boolean offer(E e) Adds an object to the end of the queue, returning true on success
E poll() Removes and returns an element from the head of the queue
E peek() Returns the element at the head of the queue (but does not delete it)