Translate

Archive for August, 2010

Map function in Java

Tuesday, August 31st, 2010

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<T> 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<T> {
   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 <T> void map(List<T> list, MapFunction<T> mapFunction) {
int size=list.size();
   for (int i=0; i < size; i++) {
      mapFunction.map(list.get(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<Message> messages = ...;
Utils.map(messages, new MapFunction<Message> {
   public void map(Message message) {
      Engine.INSTANCE.SERIALIZER.save(message);
   }
});

done deal.

Update

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 <T> void map(List<T> list, IndexedMapFunction<T> mapFunction) {
		int size = list.size();
		for (int i = 0; i < size; i++) {
			mapFunction.map(i, list.get(i));
		}
}

The interface for IndexedMapFunction looks like this

public interface IndexedMapFunction<T> {
	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.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Building The Taj Mahal with LEGOs

Tuesday, August 3rd, 2010

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)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


  • Categories

  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • October 2009
  • September 2009
  • July 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004