Click here to Skip to main content
13,902,146 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 4 Jan 2013
Licenced CPOL

Filtering a List in Java

, 29 Jan 2013
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
ishihidul14-Jan-13 12:03
memberishihidul14-Jan-13 12:03 

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.

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