Filter List in Java
In this post, we’ll illustrate how to filter a list in Java.
1. Java 7 and before
A naive approach is to iterate through the list using the for-each loop and filter it using a conditional statement.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { // Program to filter list in Java public static void main(String[] args) { List<String> list = Arrays.asList("RED","BLUE","BLACK","GREEN","BROWN"); // Java 7 and before: // create an empty list List<String> filteredList = new ArrayList<>(); // iterate through the list for (String entry: list) { // filter values that start with `B` if (entry.startsWith("B")) { filteredList.add(entry); } } System.out.println(filteredList); } } |
Output:
[BLUE, BLACK, BROWN]
The above solution creates a separate list for filtered elements. We can also filter the same list using an iterator.
The following code uses the remove() method provided by the Iterator class to filter the list elements. Please note that ConcurrentModificationException will be thrown if the remove() method of List interface is used, as it is not allowed to modify a list while iterating over it except by iterator’s own remove method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { // Program to filter list in Java public static void main(String[] args) { List<String> list = new ArrayList<>( Arrays.asList("RED", "BLUE", "BLACK", "GREEN", "BROWN")); // Java 7 and before: // get an iterator to the list Iterator<String> itr = list.iterator(); // iterate through the list while (itr.hasNext()) { String curr = itr.next(); // filter values that start with `B` if (!curr.startsWith("B")) { itr.remove(); } } System.out.println(list); } } |
Output:
[BLUE, BLACK, BROWN]
2. Using Java 8 Stream
In Java 8 and above, the recommended approach is to convert the list into a stream, apply a filter on it, and finally, collect the filtered elements in a String.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { // Program to filter list in Java public static void main(String[] args) { List<String> list = Arrays.asList("RED","BLUE","BLACK","GREEN","BROWN"); // Java 8 and above: String filteredList = list.stream() .filter(entry -> entry.startsWith("B")) .collect(Collectors.joining(", ", "[", "]")); System.out.println(filteredList); } } |
Output:
[BLUE, BLACK, BROWN]
We can also convert the filtered stream back to a list by using a list collector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { // Program to filter list in Java public static void main(String[] args) { List<String> list = Arrays.asList("RED","BLUE","BLACK","GREEN","BROWN"); // Java 8 and above: List<String> filteredList = list.stream() .filter(entry -> entry.startsWith("B")) .collect(Collectors.toList()); System.out.println(filteredList); } } |
Output:
[BLUE, BLACK, BROWN]
That’s all about filtering List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)