For those who are disappointed for the lack of lambda expressions and some other functional features in Java

Today I just found a cool java library which made me really happy. It’s called op4j: Bending the Java spoon.  It is a Java library aimed at improving quality, semantics, cleanness and readability of Java code, especially auxiliary code like data conversion, structure iteration, filtering, mapping, etc.

For example…

We have a list of string and all of the strings are dates in dd/MM/yyyy format and we wanted to convert them into java.util.Calendar objects. And some string may be null and we don’t want nulls to be included in final results and one more things, we don’t want dates in the futures, only the past ones in the final results.

So here three things we need to do:

  • Convert all the string dates in java.util.Calendar object
  • Remove nulls
  • Remove futures dates.

So let’s see how we can do it in normal java.

                List<String> list = new ArrayList<String>();

		Calendar now = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Calendar calendar = Calendar.getInstance();

		Set<Calendar> set = new LinkedHashSet<Calendar>();
		Date date = null;
		for (String element : list) {
			if (element != null) {
				try {
					date = dateFormat.parse(element);
				} catch (ParseException e) {
				if (!calendar.after(now)) {

Same thing we can do it with op4j but writing only 2 line of code

Calendar now1 = Calendar.getInstance();
		Set set1 = Op.on(list).toSet()

and a lot of other example are here:

and adding op4j


Happy Coding !!


Leave a Reply

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

You are commenting using your 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