There is no doubt that the most commonly used occasion for lambda expressions is to replace anonymous inner classes, and implementing the Runnable interface is a classic example of anonymous inner classes. Lambda expressions are quite powerful. You can use ()-> to replace the entire anonymous inner class! Please look at the code:
If you use anonymous inner classes:
@Test public void oldRunable() { new Thread(new Runnable() { @Override public void run() { System.out.println("The old runable now is using!"); } }).start(); }
And if you use lambda expressions:
@Test public void runable() { new Thread(() -> System.out.println("It's a lambda function!")).start(); }
The final output:
The old runable now is using!
It's a lambda function!
Isn't it terrifyingly powerful? Is it terrifyingly simple? Is it frighteningly clear and focused? This is the scary thing about lambda expressions. It uses very little code to accomplish what the previous class did!
Java’s collection classes are often used in daily development, and it is even said that there is no Java code that does not use collection classes. . . The most common operation on collection classes is iterative traversal. Please see the comparison:
@Test public void iterTest() { Listlanguages = Arrays.asList("java","scala","python"); //before java8 for(String each:languages) { System.out.println(each); } //after java8 languages.forEach(x -> System.out.println(x)); languages.forEach(System.out::println); }
If you are familiar with scala, you must be familiar with forEach. It iterates over all the objects in the collection and brings lambda expressions into them.
languages.forEach(System.out::println);
This line looks a bit like the way scope resolution is written in C, and it is also possible here.
When it comes to functional programming and lambda expressions, how can we not mention map. . . Yes, java8 is definitely supported. Please see the sample code:
@Test public void mapTest() { Listcost = Arrays.asList(10.0, 20.0,30.0); cost.stream().map(x -> x + x*0.05).forEach(x -> System.out.println(x)); }
The final output result:
10.5
21.0
31.5
The map function can be said to be the most important method in functional programming. The function of map is to transform one object into another. In our example, the cost is increased by 0.05 times the size through the map method and then output.
Since map is mentioned, how can we not mention reduce. Like map, reduce is also one of the most important methods in functional programming. . . The function of map is to change one object into another, while the function of reduce is to merge all values into one. Please see:
@Test public void mapReduceTest() { Listcost = Arrays.asList(10.0, 20.0,30.0); double allCost = cost.stream().map(x -> x+x*0.05).reduce((sum,x) -> sum + x).get(); System.out.println(allCost); }
The final result is:
63.0
If we use a for loop to do this:
@Test public void sumTest() { Listcost = Arrays.asList(10.0, 20.0,30.0); double sum = 0; for(double each:cost) { each += each * 0.05; sum += each; } System.out.println(sum); }
I believe that using map reduce lambda expressions is more than one level higher.
Filter is also an operation we often use. When operating a collection, it is often necessary to filter out some elements from the original collection.
@Test public void filterTest() { Listcost = Arrays.asList(10.0, 20.0,30.0,40.0); List filteredCost = cost.stream().filter(x -> x > 25.0).collect(Collectors.toList()); filteredCost.forEach(x -> System.out.println(x)); }
Final result:
30.0
40.0
will Does it feel like python or scala has been written in java? Isn’t he so handsome?
In addition to supporting functional programming style at the language level, Java 8 also adds a package called java.util.function. It contains many classes to support functional programming in Java. One of them is Predicate. Using the java.util.function.Predicate functional interface and lambda expressions, you can add logic to API methods and support more dynamic behaviors with less code. The Predicate interface is very suitable for filtering.
public static void filterTest(Listlanguages, Predicate condition) { languages.stream().filter(x -> condition.test(x)).forEach(x -> System.out.println(x + " ")); } public static void main(String[] args) { List languages = Arrays.asList("Java","Python","scala","Shell","R"); System.out.println("Language starts with J: "); filterTest(languages,x -> x.startsWith("J")); System.out.println("\nLanguage ends with a: "); filterTest(languages,x -> x.endsWith("a")); System.out.println("\nAll languages: "); filterTest(languages,x -> true); System.out.println("\nNo languages: "); filterTest(languages,x -> false); System.out.println("\nLanguage length bigger three: "); filterTest(languages,x -> x.length() > 4); }
The final output result:
Language starts with J:
JavaLanguage ends with a:
Java
scalaAll languages:
Java
Python
scala
Shell
RNo languages:
Language length bigger three:
Python
scala
Shell
As you can see, the filter method of the Stream API also accepts a Predicate, which means that our customized filter() method can be replaced by one written in it Inline code, this is also the magic of lambda expressions.
The above is the detailed content of Java8 nanny-level lambda expression source code analysis. For more information, please follow other related articles on the PHP Chinese website!