Hashes in Java are robust. They all seems to be similar however there are some differences we should pay close attention to them before selecting one to use in the design. Below is the list of these Hashes with specific features on how they different from each other:
Map
. it is an interface
. An object that maps keys to values
. A map cannot contain duplicate keys; each key can map to at most one value.
. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.
.example
[java]
Map<String, Integer> m = new HashMap<String, Integer>();
[/java]
HashMap
. not an interface
. it is unsynchronized. So come up with better performance
. Permits nulls
. allows null values as key and value
. does not guarantee that the order of the map will remain constant over time.
.example
[java]
HashMap<Integer,String> productMap = new HashMap<Integer,String>();
productMap.put(1, "Keys");
productMap.put(2, null);
[/java]
HashTable
. doesn’t allows null values as key and value. You will get NullPointerException if you add null value.
. is synchronized. So it comes with its cost. Only one thread can access in one time
.example:
[java]
Hashtable<Integer,String>; cityTable = new Hashtable<Integer,String>();
cityTable.put(1, "New York");
cityTable.put(2, "San Franscisco");
cityTable.put(3, null); /* NullPointerEcxeption at runtime*/
System.out.println(cityTable.get(1));
System.out.println(cityTable.get(2));
System.out.println(cityTable.get(3));
[/java]
HashSet
. does not allow duplicate values.
. It provides add method rather put method.
. it can be used where you want to maintain a unique list.
. example
[java]
HashSet<String> stateSet = new HashSet<String>();
stateSet.add ("CA");
stateSet.add ("WI");
stateSet.add ("NY");
if (stateSet.contains("PB")) /* if CA, it will not add but shows following message*/
System.out.println("Already found");
else
stateSet.add("PB");
[/java]
put your comments/feedback in the comment box.
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Thursday, October 20, 2011
comparison of Java Map vs HashMap vs HashTable vs HashSet
Wednesday, March 23, 2011
What is absolute value of Integer.MIN_VALUE ?
what does below code return?
[java]
int ret = Math.abs(Integer.MIN_VALUE);
[/java]
In the number system, there are more negative numbers than positive ones. Not all -ve numbers have their positive counterparts (e.g. -3 & +3). Ignoring 0. The minimum possible number in 32 bit number is 10000000000000000000000000000000(31 zeroes, say 1000...0).
Here is how absolute value is calculated.
we take Two's compliment by inverting the whole number (replace 1 with 0 and 0 with 1) and then add 1 to the final value.
Lets do it together:
[java]
Integer.MIN_VALUE = Math.abs(Integer.MIN_VALUE)
[/java]
Note: Math.abs(...) doesn't guarantee to always return positive number. The same is true for "%" operator as it gives remainder but not modulo. Remainder could be negative.
[java]
int ret = Math.abs(Integer.MIN_VALUE);
[/java]
In the number system, there are more negative numbers than positive ones. Not all -ve numbers have their positive counterparts (e.g. -3 & +3). Ignoring 0. The minimum possible number in 32 bit number is 10000000000000000000000000000000(31 zeroes, say 1000...0).
Here is how absolute value is calculated.
we take Two's compliment by inverting the whole number (replace 1 with 0 and 0 with 1) and then add 1 to the final value.
Lets do it together:
in 2*32 bit number... 1000...0 is -ve
1) 2s compliment would be:
~100000...0 = 011111...1
2) add 1, would become 01111...11 + 1 = 1000000...0
so its back to the same number Integer.MIN_VALUE
Absolute value of MIN_VALUE is itself.
[java]
Integer.MIN_VALUE = Math.abs(Integer.MIN_VALUE)
[/java]
Note: Math.abs(...) doesn't guarantee to always return positive number. The same is true for "%" operator as it gives remainder but not modulo. Remainder could be negative.
Subscribe to:
Comments (Atom)