The specific code is as follows:
import java.util.ArrayList;import java.util.List;import java.util.function.Predicate;import java.util.stream.Collectors;public class LambdaStudy{ public static void main(String[] args) { //Initialize list collection List<String> list = new ArrayList<String>(); list.add("test data 1"); list.add("test data 2"); list.add("test data 3"); list.add("test data 12"); //Use λ expression to traverse the collection list.forEach(s -> System.out.println(s)); //Filter elements in combination with Predicate and filter conditions Predicate<String> contains1 = n -> n.contains("1"); Predicate<String> contains2 = n -> n.contains("2"); //Tranquility through the collection list.stream().filter(contain1).forEach(n -> System.out.println(n)); list.stream().filter(s -> contains1.test(s)).forEach(s -> System.out.println(s)); list.stream().filter(contain1.and(contain2)).forEach(n -> System.out.println(n)); list.stream().filter(contain1.or(contain2)).forEach(n -> System.out.println(n)); //Replace the filtered elements in a collection List<String> newList = list.stream().filter(contain1.and(contain2)).collect(Collectors.toList()); newList.forEach(s -> System.out.println(s)); }} Summarize
The above is the skills (JDK1.8) that the editor introduced to you about Java using lambda expressions to operate List collections. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!