Simple to use Java Lambda filter foreach of

Simple to use Java Lambda filter foreach of


 

List of Java in the collection by the time the query conditions, often think of using for loop.

Since the introduction of Java 8 Lambda, simplifies the use of the loop.

Lambda illustrates a simple use of the foreach and filter.

 

1, custom class Hero

/**
 * Created by Miracle Luna on 2020/3/8
 */
public class Hero {
    private String name;
    private String country;

    public Hero(String name, String country) {
        this.name = name;
        this.country = country;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Hero{" +
                "name='" + name + '\'' +
                ", country='" + country + '\'' +
                '}';
    }
}

 

2, foreach loop

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

/**
 * Created by Miracle Luna on 2020/3/8
 */
public class LambdaForeachDemo {
    public static void main(String[] args) {
        List<Hero> heroList = new ArrayList<>();
        List<Hero> resultList = new ArrayList<>();

        Hero liubei = new Hero("刘备", "蜀国");
        Hero guanyu = new Hero("关羽", "蜀国");=
        Zhangfei Heronew new Hero ( "Zhang", "Shu" ); 
        Hero Sunce = new new Hero ( "Sun Ce", "Wu" ); 

        heroList.add (liubei); 
        heroList.add (Guanyu); 
        heroList.add (Zhangfei) ; 
        heroList.add (Sunce); 

        // query is not a hero Wu 
        heroList.forEach (hero -> {
             IF ( "Wu"! .equals (hero.getCountry ())) { 
                resultList.add (hero); 
            } 
        }); 

        // print the results set 
        resultList.forEach (Hero -> System.out.println (hero.toString ())); 
    } 
}

 

3, filter filter

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Miracle Luna on 2020/3/8
 */
public class LambdaFilterDemo {
    public static void main(String[] args) {
        List<Hero> heroList = new ArrayList<>();
        List<Hero> resultList = new ArrayList<>();

        Hero liubei = new Hero("刘备", "蜀国");
        Hero guanyu = new Hero("关羽", "蜀国");
        Zhangfei Hero = new new Hero ( "Zhang", "Shu" ); 
        Hero Sunce = new new Hero ( "Sun Ce", "Wu" ); 

        heroList.add (liubei); 
        heroList.add (Guanyu); 
        heroList.add (Zhangfei); 
        heroList.add (Sunce); 

        // query is not a hero Wu 
        resultList = heroList.stream () 
                (hero .filter ! -> "Wu" .equals (hero.getCountry ())) 
                .collect ( Collectors.toList ()); 

        // print the results set 
        resultList.forEach (Hero -> System.out.println (hero.toString ())); 
    } 
}

 

4, the results are as follows:

Hero {name = 'Bei', country = 'Shu' } 
Hero {name = 'Guan', country = 'Shu' } 
Hero {name = 'Zhang', country = 'Shu'}

 

Guess you like

Origin www.cnblogs.com/miracle-luna/p/12445627.html