Search This Blog

Loading...

Saturday, August 29, 2015

How to randomize elements in List in Java using shuffle method

java.util.Collections class provides shuffle() method which can be used to randomize object stored in a List in Java. Since List is an ordered collection and maintains the order on which objects are inserted into it, you may need to randomize elements if you need them in different order. Collections.shuffle() method uses default randomness to randomize element but you also has an overloaded version of shuffle() to provide an instance of  java.util.Random object, which can be used to randomize elements. Since this method except a List, you can also pass it to LinkedList, VectorCopyOnWriteArrayList and others, which doesn't implement RandomAccess method. In such cases, this method convert list to array before shuffling to avoid quadratic performance by shuffling sequential access list. Once shuffling is done it also converts back array to list. Shuffling has many usage e.g. shuffling deck of cards in a poker game simulation. You can also use shuffling to roll dice if you are developing any board game which requires dice e.g. Ludo.

Tuesday, August 25, 2015

2 ways to parse String to int in Java

Java provides Integer.parseInt() method to parse a String to an int value, but that's not the only way to convert a numeric String to int in Java. There is in fact a better way, which take advantage of parsing logic of parseInt() method as well as caching offered by Flyweight design pattern, which makes it more efficient and useful. Yes, you guessed it right, I am talking about Integer.valueOf() method, which implements Flyweight design pattern and maintains a cached pool of frequently used int values e.g. from -128 to 127. So every time you pass a numeric String which is in range of -128 to 127, Integer.valueOf() doesn't create a new Integer object but return the same value from cached pool. Only drawback is that Integer.valueOf() returns an Integer object and not an int primitive value like parseInt() method, but given auto-boxing is available in Java from JDK 5 onward, which automatically convert an Integer object to int value in Java. BTW, if you are learning Java and want to master fundamentals, I suggest you to take a look at Head First Java 2nd Edition, they explains the concept in the easiest way possible but also brings out important details.

Saturday, August 22, 2015

Difference between Public, Private and Protected modifier in Java?

In Java, you have got something called access modifier, which specifies accessibility of class, methods and variables. There are four access modifier in Java namely public, private, protected and the default access modifier, also known as package level modifier. Difference between these access modifier comes in their ability to restrict access to a class, method or variables, public is the least restrictive access modifier while package is the most restrictive access modifier, package and protected lies in between. Another key difference between public, protected, package and private modifier comes from the point where you can apply them, for example you cannot use private or protected modifier with a top level class but you can use public modifier there. default modifier is little bit special, when you do not specify any of the public, protected and private modifier, which is also the keywords, then Java automatically apply a default modifier (no it doesn't use default keyword), which means the said class, method or member will only be accessible inside the package it has declared. Any class, which is outside the said package cannot access that element. Good thing about these concept is that, difference between public, protected and private in Java is also one of the frequently asked Java interview question. If you are looking for a Java development position and preparing for one then you can also take help from the wonderful book Java Interview Exposed by Wrox. It is one of the rare complete guide for a Java developer and tells which topic is important from interview point of view.