RSS

LambdaJ as an alternative to query out from Collections (Stop Iterations!!)

06 Aug

I remember the first time i was taught Data Structure during my college time, there was a way to generate all data from a collection (ArrayList, LinkedList, etc). At that time, i was thinking that it was the best way to populate the data until a position where i learnt how to build a web application and optimization. It is not optimized at all to iterate all the data everytime we triggered an event (let say ‘key event’ when we type ‘A’ and all data consisting ‘A’ will appear). Microsoft .NET have LINQ, but how about Java? Don’t worry for all of you, there are a lot also tools to do things like this. I will highlight my current post today for LambdaJ. LambdaJ is a library to manipulate a collections without any iterations. You can also implement a ‘SQL’like syntax into it. To use LambdaJ, you have to download 3 dependencies library for this : 1. CGLib (download from here) 2. LambdaJ (download from here) 3. Hamcrest Matcher (download from here) NOTE:for Hamcrest Matcher lib, download the one with keyword all For example if you have a collections below :


List<String> listOfNames = new ArrayList<String>();
listOfNames.add("Hans");
listOfNames.add("Kristanto");

If you want to merge all the data inside of the list, instead of using for, you can use a method from LambdaJ like below :


package test.lambdaj;

import java.util.List;
import java.util.ArrayList;
import static ch.lambdaj.Lambda.*;

/**
 *
 * @author Hans Kristanto
 */
public class Main {
    
    public static void main(String[] args){
    
        List<String> list = new ArrayList<String>();
        list.add("Hans");
        list.add("Kristanto");
        
        String names = "";
        names = join(list,"-");
        
        System.out.println(names);
    }
}   

The result will be :

Hans-Kristanto

Of course it is not all LambdaJ can do, here come the funkiest stuff😀. Let say we have this list below given :


List<String> listOfNames = new ArrayList<String>();
listOfNames.add("Orange");
listOfNames.add("Mango");
listOfNames.add("Guava");
listOfNames.add("Banana");
listOfNames.add("Papaya");
listOfNames.add("Strawberry");

You want to populate all data containing letter of ‘o’ into new List, you can do like this below :


package test.lambdaj;

import java.util.List;
import java.util.ArrayList;
import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*; //IMPORTANT TO DO A MATCHER STUFF

/**
 *
 * @author Hans Kristanto
 */
public class Main {
    
    public static void main(String[] args){
    
        List<String> listOfNames = new ArrayList<String>();
        listOfNames.add("Orange");
        listOfNames.add("Mango");
        listOfNames.add("Guava");
        listOfNames.add("Banana");
        listOfNames.add("Papaya");
        listOfNames.add("Strawberry");
        
        List<String> listOfNewFruit;
        listOfNewFruit = select(listOfNames, having(on(String.class),containsString("o")));
        
        String names = join(listOfNewFruit,"~");

        System.out.println(names);
    }
}   

And the result will be :

Mango

Awesome is it? It will be much more useful for you if you have a case like you have bunches of data generated from DB for example, but you dont want to periodically retrieve from the DB since the data always the same. This can be much more helpful for you. You can refer more examples from javaone slides below : LambdaJ Slides.

 
3 Comments

Posted by on August 6, 2011 in English, Programming

 

Tags: , , , , , , , ,

3 responses to “LambdaJ as an alternative to query out from Collections (Stop Iterations!!)

  1. Le Thi Minh Thuy

    May 25, 2012 at 7:41 pm

    Thank you. This is what I found

     
  2. Hans Kristanto

    May 25, 2012 at 8:00 pm

    Hi Le Thi Minh Thuy,

    Do you mean you built this library?

     
  3. Paddy

    September 12, 2012 at 2:27 am

    Great post, thanks!

     

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: