How HashSet works?


Well earlier i wasnt sure how HashSet is created internally and which data structure is used. But when I looked at the class implemenation i was surprised because of following features i noticed:

Hashset is used to store the unique elements, in which their is no gurantee of the iteration order.

Hashset internally use HashMap .

Elements passed to Hashset are stored as a key of the HashMap with null as value. Since the objects passed to set are key so no extra check is done to identify duplicates. For eg after adding integer 1 and 2 if i add 1 again, no check is performed to identify whether 1 is present or not. The hashset simply performs the put with the same value( ‘1’) in this case as key.

Similariy when an element is removed from the Set the internal HashMap remove method is called.

So HashSet data structure is nothing but a HashMap with objects as key.

HashSet Implemenation from java.util package

  1. public HashSet() {
     map = new HashMap<E,Object>();
        }
  2.    public boolean add(E o) {
     return map.put(o, PRESENT)==null;
        }
  3.     /**
         * Removes the specified element from this set if it is present.
         *
         * @param o object to be removed from this set, if present.
         * @return <tt>true</tt> if the set contained the specified element.
         */
        public boolean remove(Object o) {
     return map.remove(o)==PRESENT;
        }

How to solve “error could not find java runtime 2 environment” while opening an IDE in windows


Many times it happen that the IDE which you are installing is not compatible with the old JDK version. And when you try to open the IDE this error pops up.

This is because the oldversion of JDK is set . There are many ways to get this correct of which the one i prefer most is as follows:

  • Open your Registory Editor by doing ‘Windows’ button + ‘R’ key
  • type regedit
  • goto :HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\
  • Change to current value of ‘CurrentVersion’ to the desired JDK version

And open the IDE now.

Java Performance Tip


Consider the case in which >, == and >= operator are used for comparison.

long starttime = System.currentTimeMillis();
for (int i = 0; i < 8888888; i++) {
if (i == 10 || i < 10) {

}
}
System.out.println(“Total time taken case1″+(System.currentTimeMillis()-starttime));

starttime = System.currentTimeMillis();
for (int i = 0; i < 8888888; i++) {
if (i <= 10) {

}
}
System.out.println(“Total time taken case2″+(System.currentTimeMillis()-starttime));

Output is :
Total time taken case1 31ms
Total time taken case2 16ms

I guess not many people fall for 1st case but still i have seen code implemented using case 1.
Just a cautionary Tip.

How the substring() function of String class works..


Consider the following code

String s1 = "Monday";
String s = s1.substring(0,3);
or
s1.substring(0,3).equals("Mon")

substring is clever. It does not make a deep copy of the substring the way most languages do. It just creates a pointer into the original immutable String, i.e. points to the value char[] of the base string, and tracks the starting offset where the substring starts and count of how long the substring is.
The downside of this cleverness is a tiny substring of a giant base String could suppress garbage collection of that big String in memory even if the whole String were no longer needed. (actually its value char[] array is held in RAM; the String object itself could be collected.)
If you know a tiny substring is holding a giant string in RAM, that would otherwise be garbage collected, you can break the bond by using

String s = new String(s1.substring(0,3));