What is a set

1. A broad concept

Set, short for set, is a basic concept in mathematics and the main research object of set theory. The basic theory of set theory was founded in the 19th century. The simplest statement about a set is the definition in naive set theory (the most primitive set theory), that is, a set is a “certain bunch of things”, and the “things” in a set are called elements. A modern set is generally defined as a whole composed of one or more definite elements

A set is a whole that contains a set of defined elements.

2. Concepts in Java

A collection is a data structure that holds objects.

There are three main categories:

  1. List collection: Elements are stored in order and can be repeated
  2. Set Set: Elements are stored unordered and cannot be repeated
  3. Map collection: The Map is stored by key-value. The key cannot be repeated but the value can be repeated

interface

This is not all the interfaces and implementation classes, but it is the main object of our study.

2. Interface introduction

1.Iterator

code

I have deleted all the notes to save space.

public interface Iterator<E> {
	 /** * What you need to do to implement this method * check if there is another data, return a Boolean */
    boolean hasNext(a);
   /* * This method returns the next object in the collection and throws an exception */ if there is no next object
    E next(a);
    /** By default an exception is thrown to implement this method, which removes the element */ on the current iterator
    default void remove(a) {
        throw new UnsupportedOperationException("remove");
    }
    /** The rest of the objects are removed */
    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while(hasNext()) action.accept(next()); }}Copy the code

added

  1. This is an iterator, usually inside an ordered collection class, created by iterator(), and returned by objects that implement the interface.
  2. Through the iterator’s hasNext() method, it is possible to iterate over the collection.

The implementation class

Generally in the implementation of the List interface, Set interface implementation class, as an inner class.

2.Iterable

code

In the Collection interface, this interface is implemented.

public interface 可迭代<T> {
	/** This is an Iterator construction method */
    Iterator<T> iterator(a);
    /** this is a loop over the method, if you are writing code, you will know it
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) { action.accept(t); }}/** This method is a Spliterator that splits iterators. This class has a set of values inside it that record the starting position and number of iterations that need to be performed
    default Spliterator<T> spliterator(a) {
        return Spliterators.spliteratorUnknownSize(iterator(), 0); }}Copy the code

added

  1. This is an iterator builder, and by implementing this interface, you can get the iterator
  2. As long as you implement the Iterable interface, you can use Java’s for(:) loop wrapped with forEach
  3. Iterable is a wrapper around Iterator that makes it easier to implement different Iterator building methods in different implementation classes

3.Collection

code

There’s too much code, and I’m not going to explain what it’s supposed to do by looking at the name of the method, okay

public interface Collection<E> extends 可迭代<E> {
	/** Return size, in order to conform to JavaBean programming ideas */
    int size(a);
	  /** Check whether the set is empty */
    boolean isEmpty(a);
	/** Determine whether the element exists in the collection */
    boolean contains(Object o);

    Iterator<E> iterator(a);
	/** What you need to implement converts the current collection to an array and returns */
    Object[] toArray();
	/** Convert the current collection to an array. If an argument is passed in, the array is assigned to the passed argument. If no argument is passed in, */ is returned
    <T> T[] toArray(T[] a);

    boolean add(E e);

    boolean remove(Object o);
	/** All elements in the set exist */
    boolean containsAll(Collection
        c);

    boolean addAll(Collection<? extends E> c);
	/** Removes elements that are the same as those passed into the collection, */
    boolean removeAll(Collection
        c);
	/** Return true */ if there are any elements that have been deleted
    default boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator<E> each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true; }}return removed;
    }
	/** The functionality that needs to be implemented preserves the same elements as the incoming collection, */
    boolean retainAll(Collection
        c);
	/** The method in Object */
    void clear(a);

    boolean equals(Object o);

    int hashCode(a);
	/**
	Object中的方法》
	*/
    @Override
    default Spliterator<E> spliterator(a) {
        return Spliterators.spliterator(this.0);
    }
    /** Stream is similar to a view in mysql. It takes up less memory
    default Stream<E> stream(a) {
        return StreamSupport.stream(spliterator(), false);
    }
    /** is a multithreaded stream method. The difference with stream() is that it is multithreaded */
    default Stream<E> parallelStream(a) {
        return StreamSupport.stream(spliterator(), true); }}Copy the code

added

  1. ForEach, for(:), for(:), forEach, for(:), forEach, for(:), for(:) An error is reported with the checkForComodification method on the interface implementation class (Arraylist)
  2. RemoveIf is a way of compensating for remove, and can be used in forEach, for(:)
  3. A Stream can only be traversed once, which is a declarative way of processing data. A Stream does not store elements, and the traversal is calculated on demand.

4.List

code

Because there are implementations in these interfaces, I don’t duplicate methods

public interface List<E> extends Collection<E> {
	/ * * this is to realize the replace function, this replacement is not a direct replacement But need to pass in a substitute rules, according to the rules Looks like the only basic data types can use * /
	default void replaceAll(UnaryOperator<E> operator) {
        Objects.requireNonNull(operator);
        final ListIterator<E> li = this.listIterator();
        while(li.hasNext()) { li.set(operator.apply(li.next())); }}/** Sort the set according to the implementation of the compare method in the Comparator */
    @SuppressWarnings({"unchecked", "rawtypes"})
    default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for(Object e : a) { i.next(); i.set((E) e); }}/** Need to implement the function to get the specified subscript object */
    E get(int index);
    /** we need to implement the function to get the first index of the passed object */
    int indexOf(Object o);
    /** we need to implement the function to get the last index of the passed object */
    int lastIndexOf(Object o);
    /** Return a list iterator. This iterator can be bidirectional */
    ListIterator<E> listIterator(a);
     /** Return a list iterator */ that starts with an object indexed by the parameters passed in
    ListIterator<E> listIterator(int index);
     /** Return a list */ starting with formIndex and ending with toindex-1
    List<E> subList(int fromIndex, int toIndex);
}
Copy the code

added

  1. IndexOf methods need to be compared to address or content depending on the implementation class.
  2. The get method just gets the address of the object

The implementation class

  1. LinkedList

LinkedList is based on linked lists and inherits the characteristics of linked lists. It is fast to add and delete and slow to query

  1. ArrayList

ArrayList is based on arrays and inherits the characteristics of arrays. It is fast to query and slow to add and delete.

  1. Vector

Vector is implemented based on arrays, similar to ArrayList and in many ways, except that Vector is thread-safe because its methods are synchronized.

5.Set

code

public interface Set<E> extends Collection<E> {
	/** The method of this interface is exactly the same as that of the Conlection interface. Maybe it's just a description of the implementation of the interface. I can't understand it. * /
}
Copy the code

The implementation class

  1. HashSet

A HashSet is implemented based on a hash table, and its construction method creates a HashMap object to hold the object. In addition to being unordered, a HashSet cannot have duplicate values. Access is fast.

public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }
Copy the code

This is the method for HashSet, and from this point of view, HashSet stores an object as a Key into a HashMap, so it can’t have duplicate values.

  1. TreeSet

TreeSet is implemented based on binary trees. It does not allow repetition, null, or disorder. Access efficiency is lower than HashSet.

  1. LinkedHashSet

LinkedHashSet is based on a linked list and a HashMap, which is different in that it is ordered and less efficient than HashSet.

6.Map

The Map interface code is too long to explain here, if you are interested in it yourself.

The implementation class

  1. HashMap

HashMap is based on hash algorithms, arrays, binary trees, fast inserts, fast queries, fast everything.

  1. TreeMap

TreeMap is implemented based on the hash algorithm and binary tree, slow insertion and query.

—————————————————————————————— The more you know, the more you don’t know.

If you have any questions about the content of this article, please comment directly or email me. If you think my writing is good, a “like” is also a sign of support

Shall not be reproduced without permission!