Click here to Skip to main content
11,924,995 members (52,499 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Filtering a List in Java

, 30 Jan 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Filtering a List of custom instances


The main purpose of this tip is to filter any List containing custom instances. Suppose you have a list of 100 Employees and you want a filtered list on the basis of names starting with "John". May be, there are lots of existing ways to implement this. However, I have tried to implement and want to share what I have got.


To implement filtering, we need an interface called Filter, which basically carries the filter logic.

interface Filter<T,E> {
    public boolean isMatched(T object, E text);

And a FilterList class which returns the FilteredList.

public class FilterList<E> {
    public  <T> List filterList(List<T> originalList, Filter filter, E text) {
        List<T> filterList = new ArrayList<T>();
        for (T object : originalList) {
            if (filter.isMatched(object, text)) {
            } else {
        return filterList;

And here is our simple Employee bean class.

public class Employee  {

    private String name;

    public Employee(String name) {
     * @return the name
    public String getName() {
        return name;

     * @param name the name to set
    public void setName(String name) { = name;

Now we have to create our filter first. Here is the FilterListTest class to test the FilterList.

public class FilterListTest {
    public static void main(String main[]) {
        List<Employee> empList = new ArrayList<Employee>();
        //Testing value
        Employee emp = new Employee("John Miller");
        for (int i = 0; i < 5; i++) {
            emp = new Employee("John Miller");
            emp = new Employee("Miller John");
        ** Creating Filter Logic
        Filter<Employee,String> filter = new Filter<Employee,String>() {
            public boolean isMatched(Employee object, String text) {
                return object.getName().startsWith(String.valueOf(text));
	* Pass filter to filter the original list
        List<Employee> sortEmpList = new FilterList().filterList(empList, filter, "John");

All done. Your Employee list is now filtered with "John".


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Make the world open source

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
truptihota25-Apr-13 23:06
membertruptihota25-Apr-13 23:06 
GeneralRe: My vote of 5 Pin
Shubhashish_Mandal25-Apr-13 23:51
memberShubhashish_Mandal25-Apr-13 23:51 
GeneralRe: My vote of 5 Pin
truptihota26-Apr-13 0:12
membertruptihota26-Apr-13 0:12 
GeneralMy vote of 5 Pin
ishihidul14-Jan-13 13:03
memberishihidul14-Jan-13 13:03 
GeneralMy vote of 4 Pin
Member 969271914-Jan-13 9:07
memberMember 969271914-Jan-13 9:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.3 | Last Updated 30 Jan 2013
Article Copyright 2013 by Shubhashish_Mandal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid