Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / Java
Tip/Trick

Filtering a List in Java

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
29 Jan 2013CPOL 52.2K   5   5
Filtering a List of custom instances

Introduction

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.

Implementation

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

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

And a FilterList class which returns the FilteredList.

Java
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)) {
                filterList.add(object);
            } else {
                continue;
            }
        }
        return filterList;
    }
} 

And here is our simple Employee bean class.

Java
public class Employee  {

    private String name;

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

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

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

Java
public class FilterListTest {
    public static void main(String main[]) {
        List<Employee> empList = new ArrayList<Employee>();
        //Testing value
        Employee emp = new Employee("John Miller");
        empList.add(emp);
        for (int i = 0; i < 5; i++) {
            emp = new Employee("John Miller");
            empList.add(emp);
            emp = new Employee("Miller John");
            empList.add(emp);
        }
        /**
        ** 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".

License

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


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Trupti00125-Apr-13 22:06
Trupti00125-Apr-13 22:06 
GeneralRe: My vote of 5 Pin
Shubhashish_Mandal25-Apr-13 22:51
professionalShubhashish_Mandal25-Apr-13 22:51 
GeneralRe: My vote of 5 Pin
Trupti00125-Apr-13 23:12
Trupti00125-Apr-13 23:12 
GeneralMy vote of 5 Pin
ishihidul14-Jan-13 12:03
ishihidul14-Jan-13 12:03 
Excllent
GeneralMy vote of 4 Pin
dgDavidGreene14-Jan-13 8:07
dgDavidGreene14-Jan-13 8: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.