23 March 2010

Creating a list from its elements

Valid since: op4j 1.0

Description
Easily create a list containing some objects.

Scenario
Having the strings "hello", "hola", "ola" and "olá", we want to create a List<String> object containing them.

Recipe
Create a list operator on the strings and don't execute any action on it, just return the result:

List<String> stringList = 
    Op.onListFor("hello", "hola", "ola", "olá").get();

Comments
Compare this to the the non-op4j Java equivalent:

List<String> stringList = new ArrayList<String>();
stringList.add("hello");
stringList.add("hola");
stringList.add("ola");
stringList.add("olá");

No comments:

Post a Comment