Wednesday, July 28, 2010

Puzzle 63 - Out of the Box

Java 1.5 introduced Autoboxing and since then a lot of code has been indiscriminately been using Autoboxing. I've picked up a simple case this time around (in fact its based on Joshua Bloch's - Effective Java). We'll look at a more real world problem next week.

package com.test; import java.util.Comparator; public class Order { public static void main(String[] args) { Comparator<Integer> naturalOrder = new Comparator<Integer>(){ public int compare(Integer first, Integer second) { return first < second ? -1 :(first == second ? 0:1); } }; /*This one is obviously broken - outputs 1 instead of 0 - Why?*/ System.out.println( Integer(42), new Integer(42))); /*This one works but is still broken - outputs 0 as expected! - Why is this broken?*/ System.out.println(,42)); } }

Sunday, July 18, 2010

Puzzle 62 - WARNING - Is this the End?

The world is about to end, but maybe, just maybe there is some Hope. So it give it a spin and try it. After all the fate of Earth rests in your hands!
(The code is well commented, I think you'll find everything you need out there.)

package com.test; import java.util.ArrayList; import java.util.List; /*This class had been set to trigger on December 21, 2012. *Due to a programming this class will trigger off in the next 15 mins *of you reading it. *Yes its a know issue and we've sorry about it, but right now there are other major *issues to solve and this can wait for later! **/ public class DoomsDayEarth { /*No point in returning anything once the world has been destroyed!*/ public static void destroyWorld(List<EvilObject> evilObjects) { /*You do get a chance to save the world. Try it!*/; /*This code destroys the world and prints world destroyed. *Prevent that from happening and if possible get it to print world saved * */ for(Destroy d : evilObjects) { d.destroy(); } } public static void main(String[] args) { List<EvilObject> evil = new ArrayList<EvilObject>(); evil.add(new EvilObject()); DoomsDayEarth.destroyWorld(evil); } } /*This is the only class that you can modify. Whatever happens there is always Hope!*/ class Hope { /*Write some code here that will save the world. Remember you just have 15 mins *to save the world, before main starts up, so be quick. **/ public static void save(List<EvilObject> evilObjects) { } } interface Destroy{ void destroy(); } class EvilObject implements Destroy { public void destroy() { //This method has the power to destroy the world! //To prevent any possible misuse, the code has been censored //and is not published on twisters! System.out.println("The world is destroyed!"); } } class GoodObject implements Destroy { public void destroy() { //This method does nothing. Its sole purpose is to replicate the EvilObject //without doing any evil!! System.out.println("The world is saved!"); } }

Sunday, July 11, 2010

Puzzle 61 - Set for Optimization?

I'm back with an optimization question. Lately I've been dealing with some huge volume of data and I've realized how performance can start to be a serious problem as the data increases. Here is a small illustration with relatively less data (50,000 values). Hopefully the question should be self explanatory from the code.

package com.test; import java.util.List; import java.util.ArrayList; class MyCollection{ public static void populateList(List<Long> l, int multiple){ for(int i=0; i<50000; i++){ Long value = Long.valueOf(i*multiple); l.add(value); } } public static void main(String[] args) { List<Long> listOf2 = new ArrayList<Long>(); populateList(listOf2, 2); List<Long> listOf3 = new ArrayList<Long>(); populateList(listOf3, 3); long startTimestamp, endTimestamp; List<Long> commonA = null, commonB = null, commonC = null; //First Attempt - Runs in 60 seconds startTimestamp = System.currentTimeMillis(); commonA = new ArrayList<Long>(listOf2); commonA.retainAll(listOf3); endTimestamp = System.currentTimeMillis(); System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); //Second Attempt - Runs in 73 seconds - //There are fewer elements in commonB shouldn't this run faster? startTimestamp = System.currentTimeMillis(); commonB = new ArrayList<Long>(listOf3); commonB.retainAll(listOf2); endTimestamp = System.currentTimeMillis(); System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); System.out.println("Are Equal : " + (commonA.equals(commonB))); //Third Attempt - Runs in 2 seconds startTimestamp = System.currentTimeMillis(); /* This part has been intentionally left blank. * That is because I need to have a question for the puzzle, * and I felt like leaving out the Third part would be the right * thing do to. * I've done most of the hard work so this should be easy to fill. * Yes this must run 10 times faster than the solution I have provided * and should be done just as many lines (3 to 5 lines of code should be fine!). * Well I never said anything about life being fair, did I? * */ endTimestamp = System.currentTimeMillis(); System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); System.out.println("Are Equal : " + (commonA.equals(commonC))); } }

Just keep in mind that both the "Are Equal" print statements print true and that you don't use the reference commonA or commonB when writing the code for the third part. You are free to make any other changes (in the place where the comments are there).

Update - The commonA, commonB were incorrectly pointing to the objects listOf2/listof3. That has been corrected to create new Objects -- it now reads commonA = new ArrayList(listOf2); instead of commonA = listOf2;.
Thanks to Colin Hebert for pointing it out.

Puzzle 60 (Go Soft) - Solution

Last week we looked at soft-reference and the answer that naturally comes is what the Java Doc has to say about soft references "As long as the referent of a soft reference is strongly reachable, that is, is actually in use, the soft reference will not be cleared".
Looking at the code - we have strongly reachable reference to the MemoryIntensiveObject(), O and one would expect the soft reference to exists. However as Jeremy Manson points out in his blog, this might not necessarily be the case.

As mentioned before this question was based on the Jeremy Manson blog post.

Monday, July 5, 2010

Go Soft?

An interesting post that I read sometime ago rakes up this discussion. I'll add the reference to the original post next week.

To give a bit of background we first see what a SoftReference object is. SoftReference is an object which is cleared at the discretion of the garbage collector in response to memory demand. Soft references are most often used to implement memory-sensitive caches. An object that is reachable (only) from a SoftReference is eligible for Garbage collection. The java doc is pretty clear - I recommend reading it!

The problem is the pretty little three line code below,
Object someMethod() { Object o = new MemoryIntensiveObject(); SoftReference<MemoryIntensiveObject> ref = new SoftReference<MemoryIntensiveObject>(o); //Assume garbage collector at this point and needed to free up memory. return ref.get(); }

Is the soft reference bound to return the object that was originally pointed by o ?