Map function in Java

I read on some email signature something along the lines of:
“If I had a dollar for every for(int i=0; i < size; i++) { ... } I've written I'd be rich" After coding on Android and learning about some of the tips for performance, like "With an ArrayList, a hand-written counted loop is about 3x faster" If you do use ArrayLists a lot you then have that tip in the back of your head and you end up with a lot of code like the following:

List; myList = ... ;
int size = myList.size();
for (int i=0; i < size; i++) {
  T elem = myList.get(i);
  //do something with elem

Eventually there was a moment when I said, I need a freaking map function, I’m sick of this little pattern, surprisingly I couldn’t find a map() function anywhere on the java collection framework. So I went and did this.

Warning: These mapping utilities are meant only for Lists with RandomAccess capabilities (.get(i) happens in constant time). Do not use with LinkedList or other lists that don’t implement RandomAccess, otherwise you’ll end up with up to O(n^2) times. Thanks to Roger Kapsi for noticing this issue. [you can now tell how much we like to use ArrayList]

//what I think would be the equivalent of a lambda or closure in Python...
public interface MapFunction {
   public void map(T t);

And then on one of my utils classes I added a static method map that looks like this:

public static  void map(List list, MapFunction mapFunction) {
int size=list.size();
   for (int i=0; i < size; i++) {;

So now, everytime I need to iterate over a whole list and do something to each element it's a lot cleaner

//let's serialize all the messages...
List messages = ...;, new MapFunction {
   public void map(Message message) {;

done deal.


Here's another Map utility that let's your map() function know about the index of your current element. You may have to treat certain elements differently based on their position in the list.
This time your function takes a "IndexMapFunction" implementation.

public static  void map(List list, IndexedMapFunction mapFunction) {
		int size = list.size();
		for (int i = 0; i < size; i++) {, list.get(i));

The interface for IndexedMapFunction looks like this

public interface IndexedMapFunction {
	public void map(int i, T obj);

In your implementation of "map(int i, T obj)", "i" represents the position of the element being treated. You could do different things depending on what position of the List you are. For example, you could know if you're at the beggining or end of the list, or maybe you could other data structure telling you things about some positions in the list in question.

Building The Taj Mahal with LEGOs

It had been ages since I didn’t play LEGO, after building a smaller project to see if we could get along as a team we decided to tackle a larger project.

Some life lessons you learn while playing with LEGOs:

1. Organization is key.
The more you can organize your blocks before doing any actual building, the better. The thinking is that you’ll have to find and use each and every piece on that box, you might as well do the indexing first and then spend no time looking for the pieces you need for your building.
Eventually we ended up separating every single kind of block and we put them in zip-lock bags or tupperware containers.

2. Attention to detail.

3. Focus.

4. Divide and conquer
But do so wisely, if you have to build the same module several times, that’s a perfect tasks to divide and do in parallel. If there’s a unique and very complex module to build, it’s better that you do it by yourself while your team builds other unique pieces. If they want to participate you could take turns, and spot each other to avoid mistakes. (sounds a lot like pair coding)

[flickr-gallery mode=”photoset” photoset=”72157624521849673″ pagination=”0″]