Java LAMDBA Learning 1. Objective
When I recently opened a project, I always felt a lot of time with code redundancy, so I'm going to study lamdba to make the code look more brief.
2. Explore Lambda
The most classic is the collection sort
Import java.util.arraylist;import Java.util.collections;import Java.util.list;public class Main {public St atic void Main (string[] args) {list<good> goods = new arraylist<> (); Goods.add (New Good ("Apple", 30)); Goods.add (New Good ("Pear", 10)); Goods.add (New Good ("Banana", 20)); System.out.println ("before sort"); Goods.foreach (G-System.out.println (g)); /** * * Compare Sort by lambda instead of Compator */collections.sort (goods, (P1, p2) integer.compare (p1.value , P2.value)); System.out.println ("after sort"); Goods.foreach (G-System.out.println (g)); } private static class Good {String name; int value; Public good (String name, int value) {this.name = name; This.value = value; } @Override Public String toString () {return name + "-+" + value; } }}
The parentheses can be omitted when the lambda parameter is 1, and the curly braces can be omitted when there is only one bar of the lambda function body.
When the compiler can infer the type, the parameter type can be omitted, because the Compator parameter type can only be good, so the data type of P1 and P2 must be good, so the parameter type can be omitted.
- Lambda expressions can be used not only when writing compator, such as the list's foreach, but also by using lambda expressions, such as printing each element of a list.
3.Lambad applications
First define a filter class
private static class GoodFilter { public void printSomeGood(List<Good> goods, Predicate<Good> p) { /** * 列表的foreach+java.util.function的Predicate筛选 */ goods.forEach(g -> { if (p.test(g)) System.out.println(g); }); } }
To filter operations
Predicate<Good> expensive = g -> g.value >= 20; Predicate<Good> cheap = g -> g.value < 20; GoodFilter filter = new GoodFilter(); System.out.println("Expensive Goods list"); filter.printSomeGood(goods, expensive); System.out.println("Cheap Goods list"); filter.printSomeGood(goods, cheap);
- predicate is a
java.util.function
function in the package, the other functions under the package please learn Baidu by yourself
- Compared with the large amount of code previously defined for interfaces and interfaces, it is undoubtedly a lot more concise.
4. Stream API Introduction
Stream is like an iterator (Iterator), one-way, non-reciprocating, the data can only be traversed once, traversed once and then exhausted, like flowing water from the front flow, gone forever. In contrast to iterators, streams can parallelize operations, and iterators can only be ordered and serialized.
/** * 流式操作,先筛选,再将符合条件的元素转化为其value值,最后相加打印 */ int sum = goods.stream() .filter(g -> g.value >= 20) .mapToInt(g -> g.value) .sum(); System.out.println(sum);
/** * 流式操作,先筛选,再将符合条件的商品转化为其姓名,最后应用变为小写的操作,最后通过foreach打印 */ List<String> ret = goods.stream() .filter(g -> g.value >= 20) .map(g -> g.name) .map(String::toLowerCase) .collect(Collectors.toList()); ret.forEach(p->System.out.println(p));
- Note that the goods element does not change, and each time the original stream object is converted, a new stream object (which can have multiple conversions) is returned.
Java Lambda Learning