Java8 How to filter list based on another List:


Scenario:


    given 1:  List of Employees Information 

    given 2: List of Cards acceptable

    when: filter out employees who is having only acceptable card

    then:  Only valid card holder will be available


Sample Program:


import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;


public class Demoprogram {

public static void main(String[] args) {

//Card List

   List<String> cardList = Arrays.asList("credit","Debit");

   


//EmployeeList

   List<Person> personList = new ArrayList();

   

   personList.add(new Person("pandi","M","married","Debit"));

   personList.add(new Person("pandian","M","single","credit"));

   personList.add(new Person("muthu","F","married","postbankcard"));

   personList.add(new Person("Ganesh","M","Single","credit"));

   personList.add(new Person("Vishakan","M","Single","Debit"));

   personList.add(new Person("Tamizhini","F","Single","Debit"));

   personList.add(new Person("Rajan","F","Single","Online"));

   

  //Filter Logic

   personList.stream().filter(a->( cardList.contains(a.getCardName()))).collect(Collectors.toList()).stream().

forEach(p->   System.out.println(p.getCardName()+":"+p.getName()));

  

     

}

}

Comments