Showing posts with label HashMap. Show all posts
Showing posts with label HashMap. Show all posts

Thursday, May 6, 2010

HashMap vs ConcurrentHashMap

Both HashMap and ConcurrentHashMap are inherits some characteristics of Hashtable. But they have some prominent differences in context with performance, scalability.

HashMap
  1. Since Java 1.2
  2. Allows null key and value
  3. Poor performance in highly threaded applications
  4. Not much scalable
  5. Throws a ConcurrentModificationException
  6. Faster in non-multi threading applications

ConcurrentHashMap
  1. Since Java 1.5
  2. Doesn't allow null key or value
  3. Better performance in Highly threaded applications
  4. Highly scalable
  5. Do not throw ConcurrentModificationException
  6. Slower in non-multi threading applications


Wednesday, April 14, 2010

Retrieve key from HashMap using value

Below is the code to retrieve the key from HashMap using value
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class TestClass {

    public static void main(String[] args) {
        Map<Integer, Person> people = new HashMap<Integer, Person>();
        Person person1 = new Person();
        person1.setName("Mack");
        person1.setPost("Dev");
       
        Person person2 = new Person();
        person2.setName("John");
        person2.setPost("Dev");
       
        people.put(1, person1);
        people.put(2, person2);

        Set<Entry<Integer, Person>> peopleSet = people.entrySet();
        for (Entry<Integer, Person> entry : peopleSet) {
            Integer key = entry.getKey();
            Person value = entry.getValue();
            if (value.equals(person2)) {
                System.out.println("Key = " + key);
            }
        }
    }
}


Friday, March 26, 2010

What is difference between HashMap and HashTable and HashSet and TreeSet?

Hashtable
Hashtable is basically a data structure to retain values of key-value pair
  •  It didn’t allow null for both key and value. You will get NullPointerException if you add null value.
  • It is synchronized. So it comes with its cost. Only one thread can access in one time. So It is slow.
HashMap
Like Hashtable it also accepts key value pair.
  • It allows null for both key and value. 
  • It is unsynchronized. So come up with better performance(fast)

HashSet
It is from Set family.
  • HashSet does not allow duplicate values. 
  • It provides add method rather put method. 
  • You also use its contains method to check whether the object is already available in HashSet. 
  • HashSet can be used where you want to maintain a unique set but not the order. 


TreeSet
 It is also from Set family.
  • Likely HashSet, TreeSet does not allow duplicate values.
  • It provides add method rather put method. 
  • You also use its contains method to check whether the object is already available in TreeSet. 
  • TreeSet can be used where you want to maintain a unique set as well as the order.