Three ways to traverse collections

Article directory

About the author: love to eat animal milk

Motto: Complaining about being in the dark is worse than moving forward with a lamp

Content introduction: Today I will tell you about the traversal method of the collection.


1. Iterator traversal

Iterator (Iterator) is an object that allows us to iterate over the elements in a collection object. The collection classes in Java (such as ArrayList, LinkedList, HashSet, TreeSet, etc.) all implement the Iterable interface, so iterators can be used to traverse the elements in the collection.

step

1. Create an iterator object

2. Determine whether there is a next element

3. Get the element and move the pointer, the pointer points to the starting position by default

2. Enhanced for traversal

The enhanced for loop is a loop structure in the Java language, also known as a for-each loop, and the underlying layer is implemented by an iterator. It can be used to iterate over the elements in an array or collection, the syntax is more concise than the traditional for loop, and the code is easier to read. The syntax format of the enhanced for loop is as follows: 

for (元素类型 变量名 : 数组或集合) {
// 循环体
}

Among them, the element type is the data type of the elements in the array or collection, the variable name is the variable name of each element, and the array or collection is the object to be traversed. Inside the loop body, the value of each element can be accessed using the variable name. The enhanced for loop is suitable for traversing all elements in an array or collection, but cannot access the subscript or index of the array or collection. 

3. forEach method

​​​​​​​​​​The forEach method in a collection is a new way of traversing collection elements introduced in Java 8, which allows us to traverse the elements in a collection more conveniently, and can use Lambda expressions or method references to Process the elements in the collection.

4. Lambda expression

Lambda expressions are a new syntax introduced in Java 8 that can be used to simplify the implementation of functional interfaces. The syntax of Lambda expression is very concise, which allows us to write the implementation code of functional interface more conveniently.

Format

The arrow symbol (->) is the parameter list on the left and the function body on the right.

() -> {}

() : Matches the parameters of the method

{} : matches the method body of the method

 Situations that can be omitted

1. Omit the parameter type: If there is only one parameter in the parameter list of the Lambda expression,
And the type of the parameter can be inferred from the context, then the parameter type can be omitted.
(String x) -> x + 1
(x) -> x + 1
2. Omit parameter brackets: If there is only one parameter in the parameter list of the Lambda expression, parameter brackets can be omitted.
copy code
(x) -> x + 1
x -> x + 1
3. Omit the curly braces: If there is only one statement in the function body of the Lambda expression, you can omit the curly braces.
copy code
x -> {System.out.println(x)}
x -> System.out.println(x)
4. Omit the return keyword: If there is only one statement in the function body of the Lambda expression,
And the result of the statement can be returned directly, then the return keyword can be omitted.
(x,y) - > return x+y
(x, y) -> x + y
5. Method reference : If only a certain method or constructor is called in the function body of the Lambda expression, you can use method reference to simplify the code
x -> System.out.println(x)
System.out::println
It should be noted that Lambda expressions can only be used to implement functional interfaces .
That is, an interface with only one abstract method

 final form

 Functional interface: A functional interface is an interface with only one abstract method

There is only one abstract method accept in the Consumer interface, so the function is an interface, and Lambda expressions can be used

Why Lambdas can only be used with functional interfaces

Lambda expressions can only be used in functional interfaces because the essence of a Lambda expression is a function, which requires a clear target type to determine its parameter type and return value type. If the Lambda expression can be used in any type of interface, then the compiler cannot determine the type of the Lambda expression, and also cannot determine its parameter type and return value type, so that it cannot correctly infer the type of the Lambda expression, that is, Cannot be type checked and compiled correctly. Therefore, Lambda expressions can only be used with functional interfaces.


Summarize

It’s time for the happy summary again. It’s still the same sentence, go and type it yourself, everyone must type the code

like. Such people are not fools, they know what they want and what they can do.

                                                                                                                              ----   Favorite to eat animal milk

Guess you like

Origin blog.csdn.net/weixin_73869209/article/details/130851740
Recommended