Monday, August 10, 2015

Lambda Expressions - Java 8

Problem

Your have an arraylist. If a certain function returns true for a word, you need to remove the word from the list. How would you use lambda expressions for this?

Solution

 private boolean returnTrueOrFalse(String p)
 {
  Random r = new Random();
  int number = r.nextInt(100);
  boolean retValue = true;
  if(number < 10) retValue = false;
  System.out.println(retValue);
  return retValue;
 }

 private List removeIfTest(List wordList)
 {
  
  System.out.println(wordList);
  wordList.removeIf(p -> returnTrueOrFalse(p));
                System.out.println(wordList);
  return wordList;
 }

No comments:

Post a Comment