0

I'd like to get objects of DataObj which age greater than 32 and address is city2.
How to filter like this?

Following is tried code.

package com.company;

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {

        ArrayList<DataObj> arr_datalist = new ArrayList<DataObj>();
        ArrayList<DataObj> arr_filteredlist;
        arr_datalist.add(new DataObj(1, "aka", 30, "city1"));
        arr_datalist.add(new DataObj(2, "akb", 31, "city2"));
        arr_datalist.add(new DataObj(3, "akc", 32, "city2"));
        arr_datalist.add(new DataObj(4, "akd", 33, "city1"));
        arr_datalist.add(new DataObj(5, "ake", 34, "city3"));

        // age greater than 32 and address is city2.
        arr_filteredlist = arr_datalist.filter(???);

    }
}

class DataObj {
    int i_id;
    String str_name;
    int i_age;
    String str_address;

    DataObj(int id, String name, int age, String address) {
        this.i_id = id;
        this.str_name = name;
        this.i_age = age;
        this.str_address = address;
    }
}
1

5 Answers 5

5

You have several common options:

Do it yourself:

List<DataObj> arr_filteredlist = new ArrayList<>();
for(DataObj e : arr_datalist) {
    if(e.i_age > 32 && e.str_address.equals("city2")) {
        arr_filteredlist.add(e);
    }
}

Guava:

Collection<DataObj> filteredColl = Collections2.filter(arr_datalist,
    new Predicate<DataObj>() {
        @Override
        boolean apply(DataObj e) {
            return e.i_age > 32 && e.str_address.equals("city2");
        }
        // + overrides equals
    });
List<DataObj> arr_filteredlist = new ArrayList<>(filteredColl);

Java 8:

List<DataObj> arr_filteredlist = 
    arr_datalist.stream()
                .filter(e-> e.i_age > 32 && e.str_address.equals("city2"))
                .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

1 Comment

solution of each version. :D Thank you for your answer.
4

Starting with Java 8, we will have lambdas. Your task can be solved by:

List<DataObj> filteredList = arr_datalist.stream()
                              .filter(dataObj -> dataObj.str_address.equals("city2") && dataObj.i_age > 32)
                              .map(DataObj::new)
                              .collect(Collectors.toList());

1 Comment

@S.J.Lim Yes, can't wait for General Availability. You can download an early release too: jdk8.java.net/download.html
0
ArrayList<DataObj> filteredList = new ArrayList<DataObj>();
for (DataObj dataObj : arr_datalist) {
    if (32 < dataObj.getAge()  && "city2".equals(dataObj.getCity()) {
        filteredList.add();
    }
}

Comments

0

There is no filter mechanism in java7 or previous versions. but you can easily do this yourself by checking each object:

    ArrayList<DataObj> arr_datalist = new ArrayList<DataObj>();
    ArrayList<DataObj> arr_filteredlist = new ArrayList<DataObj>();
    arr_datalist.add(new DataObj(1, "aka", 30, "city1"));
    arr_datalist.add(new DataObj(2, "akb", 31, "city2"));
    arr_datalist.add(new DataObj(3, "akc", 32, "city2"));
    arr_datalist.add(new DataObj(4, "akd", 33, "city1"));
    arr_datalist.add(new DataObj(5, "ake", 34, "city3"));

    // age greater than 32 and address is city2.
    for(DataObj obj : datalist) {
        if(obj.getAge() > 32 && "city2".equals(obj.getAddress()) {
            filteredlist .add(obj);
        }
    }

I assmue your DataObj has private attributes and a getter for each attribute.

Comments

0

Define a predicate

Predicate<DataObj> filterPredicate = new Predicate<DataObj>() {
   public boolean apply(DataObj obj) {
    if(obj.getAge() > 32 && obj.getAddress.equals("city2") {
       return true;
    }
    return false;
}

Define your generic function

public static <T> Collection<T> filter(Collection<T> source, Predicate<T> predicate) {
    Collection<T> result = new ArrayList<T>();
    for (T element: source) {
       if (predicate.apply(element)) {
          result.add(element);
      }
    }
   return result;
 }

Call It

 Collection<DataObj> filteredObjects = filter(arr_datalist, filterPredicate);

2 Comments

Note: you need Guava :)
@SilviuBurcea, no need to use guava in this case as you can create your Predicate class with apply method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.