How to filter an array in Java

Java 8Object Oriented ProgrammingProgramming

You can use List.removeAll() method to filter an array. 

example

import java.util.ArrayList;
import java.util.List;
public class Tester {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      list.add("E");
      list.add("F");
      list.add("G");
      list.add("H");
      List<String> filters = new ArrayList<>();
      filters.add("D");
      filters.add("E");
      filters.add("F");
      System.out.println("Original List " + list);
      list.removeAll(filters);
      System.out.println("Filtered List " + list);
   }
}

Output

Original List [A, B, C, D, E, F, G, H]
Filtered List [A, B, C, G, H]
raja
Ali
Published on 06-Feb-2018 15:40:40
Advertisements