Java 8 Lambda filter by Lists

前端 未结 4 751
生来不讨喜
生来不讨喜 2020-12-08 02:15

I have two list and i want filter thoose elements which are both list contains. And i want to do this with lambda expression.

Users getName and Clients getUserName b

4条回答
  •  囚心锁ツ
    2020-12-08 02:31

    I would like share an example to understand the usage of stream().filter

    Code Snippet: Sample program to identify even number.

    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public void fetchEvenNumber(){
            List numberList = new ArrayList<>();
            numberList.add(10);
            numberList.add(11);
            numberList.add(12);
            numberList.add(13);
            numberList.add(14);
            numberList.add(15);
    
            List evenNumberListObj = numberList.stream().filter(i -> i%2 == 0).collect(Collectors.toList());
            System.out.println(evenNumberListObj);
    }
    

    Output will be : [10, 12, 14]

    List evenNumberListObj = numberList.stream().filter(i -> i%2 == 0).collect(Collectors.toList());

    numberList: it is an ArrayList object contains list of numbers.

    java.util.Collection.stream() : stream() will get the stream of collection, which will return the Stream of Integer.

    filter: Returns a stream that match the given predicate. i.e based on given condition (i -> i%2 != 0) returns the matching stream.

    collect: whatever the stream of Integer filter based in the filter condition, those integer will be put in a list.

提交回复
热议问题