mardi 10 juillet 2018

How to use Lambda expressions in java for nested if-else and for loop together

I have following Code where i will receive list of names as parameter.In the loop, first i'm assigning index 0 value from list to local variable name. There after comparing next values from list with name. If we receive any non-equal value from list, i'm assigning value of result as 1 and failing the test case.

Below is the Array list

List<String> names= new ArrayList<String>();
names.add("John");
names.add("Mark");

Below is my selenium test method

 public void test(List<String> names)
        String name=null;   
        int a=0;

        for(String value:names){
          if(name==null){
             System.out.println("Value is null");
             name=value;
          }
         else if(name.equals(value)){
             System.out.println("Received Same name");
             name=value;
             }
         else{
              a=1;
              Assert.fail("Received different name in between");
          }
         }

How can i convert above code into lambda expressions?. I'm using cucumber data model, hence i receive data as list from feature file. Since i can't give clear explanation, just posted the example logic i need to convert to lambda expression.

Aucun commentaire:

Enregistrer un commentaire