Skip to content

JFilter….

September 3, 2012

I was going through one question during a conversation.

How to search the contents in a List or a Map and possibly filter the contents….

As usual, “googled” and found – http://code.google.com/p/jfilter/

Following the code snippet.

Employee.java

public class Employee {

private int empId;
private String fName;
private String sName;
private int age;
private String address1;
private String address2;
private String zipCode;

public Employee(int empId, String fName, String sName, int age,
String address1, String address2, String zipCode) {
super();
this.empId = empId;
this.fName = fName;
this.sName = sName;
this.age = age;
this.address1 = address1;
this.address2 = address2;
this.zipCode = zipCode;
}

public int getEmpId() {
return empId;
}

public void setEmpId(int empId) {
this.empId = empId;
}

public String getfName() {
return fName;
}

public void setfName(String fName) {
this.fName = fName;
}

public String getsName() {
return sName;
}

public void setsName(String sName) {
this.sName = sName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getAddress1() {
return address1;
}

public void setAddress1(String address1) {
this.address1 = address1;
}

public String getAddress2() {
return address2;
}

public void setAddress2(String address2) {
this.address2 = address2;
}

public String getZipCode() {
return zipCode;
}

public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}

}

EmployeeFilters .java

import gk.jfilter.JFilter;

import java.util.ArrayList;
import java.util.List;

public class EmployeeFilters {

public static void main(String[] args) {

List<Employee> empList = new ArrayList<Employee>();

int count = 50;

for (int i = 0; i < count; i++) {
Employee emp = new Employee(i, “Jagan” + i, “Asokan” + i, i + 100,
“Address Line 1 ” + i, “Address Line 2 ” + i, “30000” + i);
empList.add(emp);
}
for (int i = 0; i < count; i++) {
Employee emp = new Employee(i, “Satya” + i, “Asokan” + i, i + 200,
“Address Line 1-1 ” + i, “Address Line 2-1 ” + i, “50000” + i);
empList.add(emp);
}
for (int i = 0; i < count; i++) {
Employee emp = new Employee(i, “Lakshmi” + i, “Priya” + i, i + 300,
“Address Line 1-2 ” + i, “Address Line 2-2 ” + i, “10000” + i);
empList.add(emp);
}
for (int i = 0; i < count; i++) {
Employee emp = new Employee(i, “Venu” + i, “Karthik” + i, i + 400,
“Address Line 1-3 ” + i, “Address Line 2-3 ” + i, “40000” + i);
empList.add(emp);
}

JFilter<Employee> empFilter = new JFilter<Employee>(empList, Employee.class);

/** Select employees  where age code is less than or equals to 200. */
List<Integer> ageList = empFilter.filter(“{‘age’:{‘$le’:’?1′}}”, 200).<Integer> map(“age”).out(new ArrayList<Integer>());

/** Select employees  whose address1 is ADDRESS LINE 1. */

List<String> fAddressList = empFilter.filter(“{‘address1’:{‘toUpperCase’:’?1′}}”, “ADDRESS LINE 1”).<String> map(“address1”).out(new ArrayList<String>());

/** Select employees  whose address1 starts with Address Line 1. */
List<String> fAddressList1 = empFilter.filter(“{‘address1’:{‘$sw’:’?1′}}”, “Address Line 1”).<String> map(“address1”).out(new ArrayList<String>());

System.out.println(ageList.size());
System.out.println(fAddressList.size());
System.out.println(fAddressList1.size());
}

}

pom.xml

<dependency>
<groupId>gk.jfilter</groupId>
<artifactId>jfilter</artifactId>
<version>0.5.0-SNAPSHOT</version>
</dependency>

Result

51
0
200

I got the code from http://jfilter.googlecode.com/svn/trunk/ and installed the dependency in my local maven Repo and hence was able to add that one in my pom.xml

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: