Introduction to Java 200 examples 96 of Java Lambda expression for beginners

Introduction to the author

Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!

introduction

Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    95.Java anonymous class
► next    97. Several abbreviations of Java lambda

summary

Lambda expression is a new feature in java8. It can replace the use of anonymous inner classes, simplify the code, and highlight the most important part of the original anonymous inner classes that contains real logic code.

The standard syntax of Lambda expression is as follows:

(parameter list) - >{
//Lambda expression body
}

->It is called the arrow operator, which splits the Lambda expression into two parts:

  1. Left: list of parameters for Lambda expression.
  2. Right: the functions to be performed in the Lambda expression are wrapped with {}, that is, the Lambda body.

Example 1

Thread instances in the previous section

package demo.demo96;
public class ThreadTest {

	public static void main(String[] args) {
		
		Thread thread = new Thread(new Runnable() {
			//Override run method
			public void run() {
				int i=10;
				while (i>0) {
					System.out.println("Thread printing:"+i);
					i--;
				}
			}
		});
		//Start thread
		thread.start();
	}
}

Example 2

Modify the above example with Lambda expression

package demo.demo96;
public class ThreadTest2 {

	public static void main(String[] args) {
		
		Thread thread = new Thread(() ->{
			int i=10;
			while (i>0) {
				System.out.println("Thread printing:"+i);
				i--;
			}
		});
		//Start thread
		thread.start();
	}
}

Operation results:

Thread printing: 10
Thread printing: 9
Thread printing: 8
Thread printing: 7
Thread printing: 6
Thread printing: 5
Thread printing: 4
Thread printing: 3
Thread printing: 2
Thread printing: 1

After the transformation, the running results are the same, but the code is much simpler. If you are familiar with JavaScript, you will find that this is very similar to the arrow function of js.

Example 3

Iterate over the collection using lambda expressions.

package demo.demo96;

import java.util.ArrayList;
import java.util.List;

public class ListTest {

	public static void main(String[] args) {
		List<String> names = new ArrayList<String>();
		names.add("Ming Shiyin");
		names.add("Zhong Wuyan");
		names.add("Little Luban");
		
        System.out.println("java8 General loops were used previously");
        for(String each:names) {
            System.out.println(each);
        }
       
        System.out.println("java8 Later use Lambda Cycle after");
        names.forEach((name) -> System.out.println(name));
	}
}

Operation results:

Java 8 used to use regular loops
Ming Shiyin
Zhong Wuyan
Little Luban
Loop after using Lambda after java8
Ming Shiyin
Zhong Wuyan
Little Luban

Example 4

Iterating Map using Lambda expressions

package demo.demo96;

import java.util.HashMap;
import java.util.Map;

import javax.security.auth.kerberos.KerberosKey;

public class MapTest {

	public static void main(String[] args) {
		Map<String,String> names = new HashMap<String,String>();
		names.put("a","Ming Shiyin");
		names.put("b","Zhong Wuyan");
		names.put("c","Little Luban");
		
        System.out.println("java8 Previously used Map iteration");
     // Traverse the keys in the map
        for (String key : names.keySet()) {
            System.out.println(key+":"+names.get(key));
        }

        System.out.println("java8 Later use Lambda Posterior Map iteration");
        names.forEach((key,value) -> System.out.println(key+":"+value));
	}
}

Operation results:

java8 previously used Map iterations
a: Ming Shiyin
b: Zhong Wuyan
c: Little Luban
Map iteration after using Lambda after java8
a: Ming Shiyin
b: Zhong Wuyan
c: Little Luban

Summary

This section summarizes the "Java Lambda expression". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning java with brother Xiao Ming, [pay attention to a wave] won't get lost.

Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    95.Java anonymous class
► next    97. Several abbreviations of Java lambda

Popular column recommendation

1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory

Tags: Java

Posted on Sat, 09 Oct 2021 23:16:31 -0400 by skorp