Single Post

Header

Saturday, September 7, 2024

Java Collections - Sep 2024

What is the difference between hashset and hashmap ? 

  1. Hashset implements Set interface, where as HashMap implements Map interface

  2. Hashset stores individual objects, HashMap stores key based values

  3. Hashset does not allow duplicate elements, HashMap does not allow keys, but it contains duplicate values

  4. Hashset allows only one null value, HashMap allows one null key and multiple null values


HashMap and Hashtable : 

// Java HashMap class implements the Map interface which allows us to store key and value pair, where keys should be unique.

// You cannot store duplicate keys in HashMap. However, if you try to store duplicate key with another value, it will replace the value.

// HashMap is non synchronized. It is not-thread safe and can't be shared between many threads without proper synchronization code.

// Hashtable is synchronized. It is thread-safe and can be shared with many threads.

// Hashtable allows one null key and multiple null values.

// Hashtable doesn't allow any null key or value.


import java.util.*;

import java.util.HashMap;

import java.util.Map;

class HashMapExample {

    

    public static void main(String[] args) {

      HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

      hashMap.put(456, "abcd");

      hashMap.put(234, "bcde");

      hashMap.put(4574, "cdef");

      hashMap.put(456, "xyz");

      hashMap.put(null, "jkl");

       hashMap.put(563, null);

        hashMap.put(897, null);

      

      System.out.println("Iterating Hashmap...");  

       System.out.println(hashMap.entrySet());   

   for(Map.Entry m : hashMap.entrySet()){    

    System.out.println(m.getKey()+" "+m.getValue());    

   }  

   

    System.out.println(hashMap.keySet());   

   for(Integer i : hashMap.keySet()) {    

     System.out.println(hashMap.get(i));    

   } 

   

    Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();

      hashTable.put(456, "abcd");

      hashTable.put(234, "bcde");

      hashTable.put(4574, "cdef");

      hashTable.put(456, "xyz");

    //   hashTable.put(null, "jkl");

    //   hashTable.put(563, null);

    //     hashTable.put(897, null);

      

      System.out.println("Iterating Hashmap...");  

       System.out.println(hashTable.entrySet());   

   for(Map.Entry m : hashTable.entrySet()){    

    System.out.println(m.getKey()+" "+m.getValue());    

   }  

   

    System.out.println(hashTable.keySet());   

   for(Integer i : hashTable.keySet()) {    

     System.out.println(hashTable.get(i));    

   } 

    }

}


Output : 

Iterating Hashmap...

[null=jkl, 897=null, 563=null, 456=xyz, 234=bcde, 4574=cdef]

null jkl

897 null

563 null

456 xyz

234 bcde

4574 cdef

[null, 897, 563, 456, 234, 4574]

jkl

null

null

xyz

bcde

cdef

Iterating Hashmap...

[4574=cdef, 456=xyz, 234=bcde]

4574 cdef

456 xyz

234 bcde

[4574, 456, 234]

cdef

xyz

bcde



List Program : 

 import java.util.*;  

public class Main {  

 public static void main(String args[]){  

  List<String> list1=new ArrayList<String>();//Creating arraylist  

  list1.add("abcd");//Adding object in arraylist    

  list1.add("defg"); 

  // list1.add(1);  // syntax error

  list1.add("bcde");

  list1.add("bcde");

  list1.add("bcde");

  //Traversing list through for-each loop  

  System.out.println("old list ");   

  for(String s:list1)    

    System.out.println(s);    

  

  // Remove list value

  list1.remove("bcde");

  list1.remove(2);

  System.out.println("\n after remove list"); 

  for(String s:list1)    

    System.out.println(s); 

    

    // Sort the list

    Collections.sort(list1);

    System.out.println("\n after sort list"); 

    for(String s:list1)    

    System.out.println(s); 

    

    // Change list value

    list1.set(1, "xyz");

    System.out.println("\n after change list"); 

     for(String s:list1)    

    System.out.println(s); 

    

    // Get the list values

    System.out.println("\n get the list"); 

    for(int i=0;i<list1.size();i++) {

        System.out.println(list1.get(i)); 

    }

 }  

}   

output : 

old list abcd defg bcde bcde bcde after remove list abcd defg bcde after sort list abcd bcde defg after change list abcd xyz defg get the list abcd xyz defg    

No comments:

Post a Comment