In this tutorial, we will see “How to Filter a List in Java?”. We are going to use various techniques to filter an ArrayList like using Loops, Java 8 Streams and more. You can pick any approach to use in your projects. All approaches have their own advantages.
Unique ways to FILTER a LIST in Java
Check out: ArrayList ListIterator in Java with Examples
Unique ways to FILTER a LIST in Java...!!! Share on X
In this tutorial, we are going to use below approaches to Filter a List:
- For Loop
- Google Guava
- Apache CollectionUtils
- Java 8 Streams
1. Filter List using ‘For Loop’
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /** * Filter a List using For Loop * @author Deepak Verma */ import java.util.ArrayList; import java.util.List; public class Filter_List_Using_For_Loop { public static void main(String[] args) { List<Actor> listOfActors = getActors(); var filteredResult = new ArrayList<Actor>(); for (Actor actor: listOfActors) { if (actor.getAge() > 45) { filteredResult.add(actor); } } System.out.println("Filtered Result:\n"+filteredResult.toString()); } static List <Actor> getActors() { List <Actor> actors = new ArrayList <Actor> (); actors.add(new Actor(5, "yashu")); actors.add(new Actor(64, "gaurangee")); actors.add(new Actor(93, "isha")); actors.add(new Actor(8, "deepak")); actors.add(new Actor(87, "yashi")); return actors; } } class Actor { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Actor(int age, String name) { this.name = name; this.age = age; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Actor{"); sb.append("name=").append(name); sb.append(", age=").append(age); sb.append('}'); return sb.toString(); } } |
1 2 | Filtered Result: [Actor{name=gaurangee, age=64}, Actor{name=isha, age=93}, Actor{name=yashi, age=87}] |
2. Filter List using ‘Google Guava’
In order to use Google Guava, you first need to add this Library to your project. Simply add the following Maven Dependency to your project POM.xml and you are all set.
1 2 3 4 5 | <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>31.1-jre</version> </dependency> |
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /** * Filter a List using Google Guava * @author Deepak Verma */ import java.util.ArrayList; import java.util.List; import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; public class Filter_List_Using_Google_Guava { public static void main(String[] args) { List<Actor> listOfActors = getActors(); Predicate<Actor> filterByAge = actor -> actor.getAge()>45; var filteredResult = FluentIterable.from(listOfActors) .filter(filterByAge) .toList(); System.out.println("Filtered Result:\n"+filteredResult.toString()); } static List <Actor> getActors() { List <Actor> actors = new ArrayList <Actor> (); actors.add(new Actor(5, "yashu")); actors.add(new Actor(64, "gaurangee")); actors.add(new Actor(93, "isha")); actors.add(new Actor(8, "deepak")); actors.add(new Actor(87, "yashi")); return actors; } } class Actor { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Actor(int age, String name) { this.name = name; this.age = age; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Actor{"); sb.append("name=").append(name); sb.append(", age=").append(age); sb.append('}'); return sb.toString(); } } |
Output:
1 2 | Filtered Result: [Actor{name=gaurangee, age=64}, Actor{name=isha, age=93}, Actor{name=yashi, age=87}] |
3. Filter List using ‘Apache CollectionUtils’
In order to use Apache Collections, you first need to add this Library to your project. Simply add the following Maven Dependency to your project POM.xml and you are all set.
1 2 3 4 5 | <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> |
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | /** * Filter a List using Apache CollectionUtils * @author Deepak Verma */ import java.util.ArrayList; import java.util.List; import org.apache.commons.collections.CollectionUtils; public class Filter_List_Using_Apache_Collection_Utils { public static void main(String[] args) { List<Actor> listOfActors = getActors(); var filteredResult = new ArrayList<>(listOfActors); CollectionUtils.filter(filteredResult, o -> ((Actor) o).getAge() > 45); System.out.println("Filtered Result:\n"+filteredResult.toString()); } static List <Actor> getActors() { List <Actor> actors = new ArrayList <Actor> (); actors.add(new Actor(5, "yashu")); actors.add(new Actor(64, "gaurangee")); actors.add(new Actor(93, "isha")); actors.add(new Actor(8, "deepak")); actors.add(new Actor(87, "yashi")); return actors; } } class Actor { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Actor(int age, String name) { this.name = name; this.age = age; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Actor{"); sb.append("name=").append(name); sb.append(", age=").append(age); sb.append('}'); return sb.toString(); } } |
Output:
1 2 | Filtered Result: [Actor{name=gaurangee, age=64}, Actor{name=isha, age=93}, Actor{name=yashi, age=87}] |
4. Filter List using ‘Java 8 Streams’
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | /** * Filter a List using Java8 Streams * @author Deepak Verma */ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Filter_List_Using_Java8_Streams { public static void main(String[] args) { List<Actor> listOfActors = getActors(); var filteredResult = listOfActors.stream() .filter(person -> person.getAge() > 45) .collect(Collectors.toList()); System.out.println("Filtered Result:\n"+filteredResult.toString()); } static List <Actor> getActors() { List <Actor> actors = new ArrayList <Actor> (); actors.add(new Actor(5, "yashu")); actors.add(new Actor(64, "gaurangee")); actors.add(new Actor(93, "isha")); actors.add(new Actor(8, "deepak")); actors.add(new Actor(87, "yashi")); return actors; } } class Actor { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Actor(int age, String name) { this.name = name; this.age = age; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Actor{"); sb.append("name=").append(name); sb.append(", age=").append(age); sb.append('}'); return sb.toString(); } } |
Output:
1 2 | Filtered Result: [Actor{name=gaurangee, age=64}, Actor{name=isha, age=93}, Actor{name=yashi, age=87}] |
Filtering a List is very common whenever you are working on any programming project. I hope above examples could help you to get a better understanding on various approaches on how to filter an ArrayList in Java.
Unique ways to FILTER a LIST in Java...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8
- Java 8 program to calculate average of N numbers
- Reverse a word in a string in Java 8
- Finding prime number using Java 8
- Double the numbers of specified ArrayList using Streams