Wednesday, September 5, 2012

Lambdaj - Usage Examples

Lambdaj simplify your task to manipulate Collection in Java programming. Convert from List into Map, select and filtering collection, joining String, grouping, etc, those are Lambdaj expertise. 

To use it you have to import Lambdaj library into your classpath and then import Lambda as a static class into your java program like this :
import static ch.lambdaj.Lambda.*;

Dummy Class (Person.java)
class Person {
private int id;
private String name;
private int age;
private String city;

public Person() {}

public Person(int id, String name, int age, String city) {
this.id = id;
this.name = name;
this.age = age;
this.city = city;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDetails() {
return "ID= "+id+"; Name= "+name+"; Age= "+age+"; City= "+city;
}
}



And these are my Lambdaj usage examples :

USE JOIN FOR BUILDING QUERY
List<String> list = Arrays.asList("hari1","hari2","hari3","hari4");
//-------------------------------------------------------------------------------------------
String result = join(list,"','");
String query = "select t from T_PERSON t where "+(result.length()>0 ? "t.name in ('"+result+"')" : "0=0");
//-------------------------------------------------------------------------------------------
System.out.println(query);


CONVERT LIST OBJECT INTO MAP
List<Person> persons = Arrays.asList(
new Person(13,"Steve",22,"London, UK"),
new Person(25,"Greg",28,"New York, USA"),
new Person(5,"Emily",22,"Bali, Indonesia"),
new Person(9,"Malih",14,"Jakarta, Indonesia"));

//-------------------------------------------------------------------------------------------
Map<Integer,Person> personMap = index(persons,on(Person.class).getId());
//-------------------------------------------------------------------------------------------
System.out.println("Person name with ID 5: " + personMap.get(5).getName());


EXTRACTING FIELDS INTO NEW LIST
List<String> personNames = extract(persons, on(Person.class).getName());
//-------------------------------------------------------------------------------------------
System.out.println("personNames: "+personNames);


SELECTING PERSON LIST WHERE CITY CONTAINS "Indonesia"
List<Person> filteredPerson = select(persons, having(on(Person.class).getCity(), StringContains.containsString("Indonesia")));
//-------------------------------------------------------------------------------------------
for (Person person : filteredPerson)  
            System.out.println(person.getDetails());



COMBINE EXTRACT AND SELECT
List<String> filteredPersonNames = extract(select(persons,having(on(Person.class).getAge(), IsEqual.equalTo(22))), on(Person.class).getName());

//-------------------------------------------------------------------------------------------
System.out.println("filteredPersonNames: "+filteredPersonNames);


GROUPING
Group<Person> groupAgeOfPerson = group(persons,by(on(Person.class).getAge()));
Set<String> groupAgeKeys  = groupAgeOfPerson.keySet();
//-------------------------------------------------------------------------------------------
System.out.println("groupAgeKeys: "+groupAgeKeys);
System.out.println("groupOfAge: "+groupAgeOfPerson.find(22));


for (String ageKey : groupAgeKeys) 
for (Person person : groupAgeOfPerson.find(ageKey))
System.out.println(person.getDetails());


3 comments:

  1. Good examples of Lambdaj.
    Keep it up. Really helpful post
    Thanks

    ReplyDelete
  2. I see this 7 years later and still find it helpful.
    Thanks a lot.

    ReplyDelete