Java8 uses Lambda to process List collections

With the newly added Lambda expressions in Java 8, we can easily operate large collections in parallel. We have
just come into contact with java8, and Lambda is still very useful, and I have not touched much at present. Let me give you a few examples (there will be supplements about Lambda in the future). I will also continue to update this blog)
Without further ado, let's go directly to the example

1. Use stream().forEach() to process List in a loop;

 

[java] view plain copy
 
print ?
  1. List<String> list = Lists.newArrayList(); //Guava package provided by google for creating a new List com.google.common.collect;  
  2.         list.add("1");  
  3.         list.add("2");  
  4.         list.add("3");  
  5.   
  6.         list.stream().forEach(string ->{  
  7.             System.out.println(string);  
  8.         });  
  9. operation result:  
  10. 1  
  11. 2  
  12. 3  
List<String> list = Lists.newArrayList();//Guava package provided by google for creating a new List com.google.common.collect;
        list.add("1");
        list.add("2");
        list.add("3");

        list.stream().forEach(string ->{
            System.out.println(string);
        });
operation result:
1
2
3


2. Use stream().map() to process List and assign a value to another List:

 

 

[java] view plain copy
 
print ?
  1. List<String> list1 = Lists.newArrayList();  
  2.         List<String> list2 = Lists.newArrayList();  
  3.         list1.add("1");  
  4.         list1.add("2");  
  5.         list1.add("3");  
  6.   
  7.         list2 = list1.stream().map(string -> {  
  8.             return "stream().map() after processing: "  + string;   
  9.         }).collect(Collectors.toList());  
  10.           
  11.         list2.stream().forEach(string -> {  
  12.             System.out.println(string);  
  13.         });  
  14. operation result:  
  15. After stream().map() processing: 1  
  16. After stream().map() processing: 2  
  17. After stream().map() processing: 3  
List<String> list1 = Lists.newArrayList();
        List<String> list2 = Lists.newArrayList();
        list1.add("1");
        list1.add("2");
        list1.add("3");

        list2 = list1.stream().map(string -> {
            return "stream().map() after processing: " + string;
        }).collect(Collectors.toList());
        
        list2.stream().forEach(string -> {
            System.out.println(string);
        });
operation result:
After stream().map() processing: 1
After stream().map() processing: 2
After stream().map() processing: 3

 

 

3. Use stream().filter() to process the List and filter the List:

 

[java] view plain copy
 
print ?
  1. List<String> list1 = Lists.newArrayList();  
  2.         List<String> list2 = Lists.newArrayList();  
  3.         list1.add("1");  
  4.         list1.add("1");  
  5.         list1.add("2");  
  6.         list1.add("3");  
  7.   
  8.         list2 = list1.stream().filter(s -> s != "1").collect(Collectors.toList());  
  9.         System.out.println(list2.toString());  
  10. operation result:  
  11. [23]  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326367034&siteId=291194637