Java List filter odd value

Description

Java List filter odd value


import java.util.ArrayList;
import java.util.stream.Stream;

public class Main {

  public static void main(String[] args) {

    // Create a list of Integer values.
    ArrayList<Integer> myList = new ArrayList<>();
    myList.add(7);/*from  w  ww.  ja  va 2  s.c  o  m*/
    myList.add(8);
    myList.add(1);
    myList.add(2);
    myList.add(7);
    myList.add(5);

    System.out.println("Original list: " + myList);

    // Display only the odd values by use of filter().
    Stream<Integer> oddVals = myList.stream().sorted().filter((n) -> (n % 2) == 1);
    System.out.print("Odd values: ");
    oddVals.forEach((n) -> System.out.print(n + " "));
    System.out.println();
  }
}



PreviousNext

Related