Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Comparative Study on HashTable HashMap and HashSet

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 469
    Comment on it

    Hashtable

    This class has implemented Map interface, before using this data structure you should know their internal implementation

    Hashtable Class Decleration-

    <p>Public class Hashtable<k,v> implements Map<k,v>
    

    You can use this collection if you are going to store data as not null keys with associate not null values, if you look internal structure of Hashtable then you will see, instance of Hashtable contains two parameters initial capacity and LoadFactor.

    Initial Capicity- Initially created number of bucket in a hashtable, when it has been declared LoadFactor-The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.

    Hashtable method are synchronize- It does mean only one thread can access hashtable at a time, if threading is main concern then only you can use this collection.

    Hashtable Example

    public class HashTableDemo {
        public static void main(String arg[]){
    
     Map<String,Integer> contactList=new Hashtable<String,Integer>();
            contactList.put("Aman",12345);
            contactList.put("Vikas",23456);
            contactList.put("Rohit",34567);
            contactList.put("Aman", null);
            contactList.put(null, null);
    
            System.out.println("size of Hashtable is"+contactList.size());
            System.out.println("instance hascode value   "+" " +contactList.hashCode());
            System.out.println("Associated all keys and Value"  +contactList);      
        }
    
    }
    

    Output - It will throw "NullPointerException" because it does not allow null key or value.

    HashMap

    Both HashMap and Hashtable have implemented Map interface, major difference between them is HashMap methods are not synchronize that's why HashMap access speed is better. although you can make HashMap synchronize using with Collections wrapper class.

    Collections.synchronizedMap(contactList);
    

    but if you have used this wrapper class with HashMap this class internal almost behaviors has been changed like Hash table get and put method would have been synchronize.

    Example.

    public class HashMapDemo {
    
        public static void main(String arg[]){
            Map<String,Integer> contactList=new HashMap<String,Integer>();
    
            contactList.put("Aman",12345);
            contactList.put("Vikas",23456);
            contactList.put("Rohit",34567);
            contactList.put("Aman", null);
            contactList.put(null, null);
    
            System.out.println("size of Hashmap is"+contactList.size());
            System.out.println("instance hascode value   "+" " +contactList.hashCode());
            System.out.println("Associated all keys and Values"  +contactList);     
        }
    }
    

    Output- size of Hashmap is4 instance hascode value 163819028 Associated all keys and Values {null=null, Rohit=34567, Aman=null, Vikas=23456}

    HashSet

    HasSet is an implementation of Set Interface,This collection is not sorted and does not give any order guarantee.This collection is fastest for lookup, insertion and deletion for larger Set.
    HashSet is the fastest implementation of a Set and it ensures the uniqueness of elements using (first) their hash value returned by the hashCode() method and (then) their equals() method for perform Object equality.

    HashSet Class Decleration-

    public class HashSet implements Set

    Let us take an example-

    public class HashSetDemoExample {
    
        public static void main(String arg[]){
    
       Student obj1= new Student(24,"Aman");
    
       Student obj2= new Student(26,"Malik");
    
       Student obj3= new Student(24,"Aman");
    
        Set<Student> s= new HashSet<Student>();
    
        s.add(obj1);
        s.add(obj2);
        s.add(obj3);
    
        System.out.println("Actual size of HashSet after inserting Students Object"  + "  "  + s.size());
         System.out.println("Stored HashSet Objects " + s);
       }
    }    
    
    public class Student{
    
       private Integer age;
        private String name;
    
        public Student(Integer age,String name)
        {
            this.age=age;
            this.name=name;
    
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
         public int hashCode() {
            final int seed = 21;
            int result = 1;
            result = seed * result + ((getName().equals(null)) ? 0 : getName().hashCode());
            result = seed * result + getAge();
            return result;
        }
    
         @Override
       public boolean equals(Object studentObj)
        {
            if((this == studentObj) && (studentObj!=null)){
                return true;
               }else if(!(studentObj instanceof Student) && (studentObj.getClass() != this.getClass())){
                    return false;
               }else{ Student student = (Student)studentObj;
               return getAge().equals(student.getAge()) &&  (getName().equalsIgnoreCase(student.getName()));
               }
           }
        }    
    

    Output Actual size of HashSet after inserting Students Object 2 Stored HashSet Objects [com.test.findnerd.example.Student@5cc2c535, com.test.findnerd.example.Student@28f10be]

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: