What is an interface?

The interface defines extension functionality in the inheritance architecture.

Here’s an example :(cat and dog case)

A dog is generally a guard, and a cat is generally a pet. But now there are many trainers or animal trainers who can train cats to drill through rings of fire, dogs to jump high, dogs to do calculations and so on. These extra movements are not something that all dogs and cats start out with, but rather something that has been specially trained to do. Therefore, these additional actions are not appropriate to be defined in animals, or to be defined directly in cats or dogs, because only some cats and dogs have these functions. Therefore, in order to reflect the expansibility of the functions of things, Java provides interfaces to define these additional functions without giving specific implementation. In the future, which cats and dogs need to be trained, only these cats and dogs need to realize these additional functions.

Iterable interface

1. Position and role in the collection system

At the top level, as the most basic interface.

Implement this interface to iterate through an entire collection through an iterator.

2. Methods in the Iterable interface

Iterator iterator(); // Returns an iterator that iterates over a set of elements of type T.

Iterable simply returns an instance of the Iterator interface. Why not combine the two interfaces and define hasNext(),next(), and other methods in Iterable? This is because classes implementing Iterable can implement multiple Iterator inner classes, such as ListItr and DescendingIterator in LinkedList, which implement two-way traversal and reverse traversal, respectively. It is more flexible to implement different traversal methods by returning different iterators. If you merge the two interfaces, you can’t return different Iterator implementation classes.

Two new methods have been added to JDK1.8:

default void forEach(Consumer<? Super T > action); // Use foreach() to traverse the collection.

How to use forEach() :

Public static void printList(list <Integer> list) {/** * num can be specified. */ list.foreach (num -> system.out.println (num)); }Copy the code

default Spliterator spliterator(); // // parallel iterator

Spliterator a Spliterator is a splitable iterator that can be seen in conjunction with the iterator sequentially traversed iterator. The tryAdvance() method or forEachRemaining() method can be used to perform bulk traversal. (tryAdvance is still called internally.) The tryAdvance() method is used to walk through the interior and do hasNext() and next() at the same time. Common Spliterator methods include tryAdvance(), forEachRemaining(), and trySplit().

Iii. Collection interface

1. Position and role in the Collection system

Go out to the Iterable interface, which is the lowest level interface in the collection system. All its implementation classes and subinterfaces must implement or inherit its methods. The interface was also modified to specify common methods to be implemented by subclasses of linear tables (arrays and linked lists), queues, Hash, and tree-structured collections. All its implementation classes are implemented in this constraint and interface.

2. Function overview of Collection

Boolean Add (Object obj): Add an element Boolean addAll(Collection C): add an element to a Collection Delete void clear(): remove all elements Boolean RemoveAll (Collection C): Contains (Object O): contains(Object O): contains(Object O): contains(Object O) Boolean containsAll(Collection C) : containsAll(Collection C) : containsAll(Collection C) : containsAll(Collection C) Iterator Iterator () Iterator () length int size(): number of elements Does a string have a length() method? Does the set have a length() method? Boolean retainAll(Collection C): Elements in both collections? What does the Boolean return mean when you think about where the element went? The collection is converted to an array (more blog.csdn.net/IdealSpring…). Object[] toArray()

            T[] toArray(T[] a) 

The above is the most basic method in Collection, which is introduced after JDK1.8:

default boolean removeIf(Predicate<? Super E> filter) // Remove all elements of the set that meet the given criteria

Collection<Integer> c = new ArrayList<>(); c.add(1); c.add(2); c.add(3); c.add(4); c.add(5); /** * removeIf(Predicate<? Super E> filter), the Predicate type arguments belong to the FunctionalInterface (explicitly labeled @functionalinterface on the class) * the passed x -> x % 2 == 0 is created by Lambda expressions (filtering out remainder 0). * This is essentially an anonymous inner class of the Predicate interface */ c. removeif (x -> x % 2 == 0); System.out.println(c);Copy the code

Default Spliterator Spliterator () // Parallel iterator — This method inherits from the Iterable interface, as described above.

The default Stream Stream () / / return the flow object (see: blog.csdn.net/IdealSpring…).

Default Stream parallelStream() // parallelStream()

A Spliterator is a splitable iterator. Spliterator is an iterator designed to traverse elements in parallel. The collections framework implements Spliterator by default.

A Stream is a collection of elements, which makes it look a bit like an Iterator; Streams can be aggregated sequentially and in parallel. We won’t go into the details of what a Stream means or how to use it or the benefits of using it. We’ll just think of it as an efficient Iterator. We’ll turn the operation on a set into a flow operation.

List interface

Position and function in the system of Collection

The family system has a large number of branches, lists and sets. Lists are branches based on linear table structures. The List interface inherits the Collection interface and extends some of its own methods in addition to the functionality of the parent interface.

Void add(int index,Object element) void add(int index, Collection
c) : all elements in the collection of specified in the index position add B: capture the Object get (int index) : access to specify the location of the element int indexOf (Object o) : the position of the element first appeared int LastIndexOf (Object o): the last occurrence position of the element C: ListIterator ListIterator ListIterator () : ListIterator ListIterator (int index): ListIterator List subList(int fromIndex, Int toIndex): returns a view of the current collection from fromindex toIndex D: removes the function Object remove(int index) : E: Modify function Object set(int index,Object Element): Modify an element according to the index to return the modified element

New features in JDK1.8:

Default Spliterator Spliterator () //1.8 Adds parallel iterators

Default void replaceAll(UnaryOperator operator) //1.8 New replacement method

default void sort(Comparator<? Array.sort (a, (Comparator) c)

The List interface standardizes the collection based on the storage structure of linear table, which can be divided into array and LinkedList. The subclass of List ArrayList is the collection based on the array structure, and LinkedList is the collection based on the LinkedList structure. There is also a Vector collection. Because the underlying data structure is an array, queries are fast, and additions and deletions are slow. Thread safety and low efficiency features are rarely used in actual development.

 

 

5. Set interface

Location in the Collection architecture

 

The SET interface, like the List interface, inherits the Collection interface. There are no methods that extend beyond the methods in the Collection interface. The main implementation classes are HashSet and TreeSet, and the LinkedHashSet is extended on the basis of HashSet. The questions that may be asked in the interview are only whether the implementation methods of HashSet and TreeSet are orderly. What are the differences between LinkedHashSet and HashSet, what are the benefits of extension, etc.

Overall Collection ruled out the big specification the two small specification, one is the linear table specification, the other is a Set of norms, have their own respective system USES, for our future is engaged in the optimization, software development, design framework and a series of operation, in choosing a data structure, also is the choice of the container I exactly what must be considered when using, Which is better, consider whether time efficiency is important or space efficiency is important and so on a series of extended problems.