RSS

Tag Archives: List

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

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 :D. 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: , , , , , , , ,