Showing posts with label Hashtable. Show all posts
Showing posts with label Hashtable. Show all posts

Friday, June 17, 2016

Hashtable , HashMap, Set , List differences java

What is difference between HashMap and Hashtable?
Ans:

HashMap= HashMap is not synchronized.In case of single thread, using HashMap is faster than than the Hashtable. HashMap allows null keys and null values to be stored.Iterator in the HashMap is fail-fast.This means Iterator will produce exception if concurrent modifications are made to the HashMap.

Hashtable=Hashtable is synchronized by default.In case of multiple threads,using Hashtable is advisable.With a single thread ,Hashtable becomes slow. Hashtable does not allow null keys or values.Enumeration for the Hashtable is not fail-fast.This means even if concurrent modifications are done to Hashtable ,there will not be any incorrect results produced by Enumeration.

Can you make HashMap synchronized?
Ans:
Yes,we can make it using synchronizedMap() methods as shown:

Collections.synchronizedMap(new HashMap());

What is difference between a Set and a List?
Ans:

Set= A set represent a collection of elements.Order of the element may change in the set.Set will not allow duplicate values to be stored.Accessing elements by their index is not possible. Sets will not allow null elements.

Lists= A List represents ordered collection of elements.List preserves the order of elements in which they are entered.List will allow duplicate values.Accessing elements by index is possible.Lists allow null elements to be stored.

Hashtable Class Java Example

Hashtable Class

Hashtable is similar to HashMap which can store elements in the form of key-value pairs.But Hashtable is synchronized .So in case of multi-threaded environment will get reliable result.

Default Initial Capacity= 11
Load Factor=0.75

Hashtable Class Methods


  • value put(key,value)
  • value get(Object key)
  • Set<K> keySet()
  • Collection<V> values
  • value remove(Object key)
  • void clear()
  • boolean isEmpty()
  • int size()
Program Sample:


/**

 * 
 */
package com.collectionpack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author Abhinaw.Tripathi
 *
 */
public class HashtableDemo
{
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException
{
Hashtable<String, Integer> ht=new Hashtable<>();
ht.put("Ajay", 50);
ht.put("Abhinaw", 100);
ht.put("Kapil", 88);
ht.put("Dhoni", 555);
ht.put("Tendulkar", 80);
System.out.println("The Player Names:");
Enumeration enuum=ht.keys();
while(enuum.hasMoreElements())
{
System.out.println(enuum.nextElement());
}
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Player Name:");
String name=br.readLine();
name=name.trim();
Integer score=ht.get(name);
if(score!=null)
{
int sc=score.intValue();
System.out.println(name + "Scored : " +sc);
}
else
System.out.println("Player not found");
}
}

OutPut:

The Player Names:
Ajay
Tendulkar
Dhoni
Abhinaw
Kapil

Enter Player Name:
Kapil
KapilScored : 88