Advertisement
FahimFaisal

Hashtable Lab

Feb 23rd, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package lab6_fileReadWrite;
  7.  
  8. import java.util.Enumeration;
  9. import java.util.Hashtable;
  10.  
  11. /**
  12.  *
  13.  * @author acer
  14.  */
  15. public class hashTableIntro {
  16.    
  17.     public static void main(String[] args) {
  18.        
  19.         Hashtable <String,String> stds=new Hashtable<String,String>();
  20.         Hashtable <Integer,String> stds2;
  21.        
  22.         stds.put("One","A");
  23.         stds.put("One", "newA");
  24.         stds.put("Two", "B");
  25.        
  26.    
  27.         System.out.println(stds);
  28.         System.out.println("hash code is : "+stds.hashCode());
  29.        
  30.         System.out.println("------------GET----------");
  31.              System.out.println("A's val "+stds.get("One"));
  32.              
  33.              System.out.println("if hash set contains newA "+stds.contains("newA"));
  34.              
  35.              Hashtable <Integer,String> stds1=new Hashtable<Integer,String>();
  36.          
  37.               System.out.println("------------CLEAR----------");
  38.              
  39.         stds1.put(1,"A");
  40.         stds1.put(2, "B");
  41.         stds1.put(3, "C");
  42.         stds2 = (Hashtable<Integer, String>)stds1.clone();
  43.              
  44.         System.out.println("new set std1 "+stds1);
  45.         stds1.clear();
  46.         System.out.println("Empy after clear "+stds1);
  47.        
  48.         System.out.println("--------------------Contains val and key-------------");
  49.         System.out.println("contains val newA "+stds.containsValue("newA"));
  50.         System.out.println("contains key One "+stds.containsKey("One"));
  51.        
  52.         System.out.println("--------------------ToString-------------");
  53.         System.out.println("String hashTable"+stds.toString());
  54.        
  55.         System.out.println("-------------------Clone-------------");
  56.         System.out.println(stds.clone());
  57.         System.out.println(stds2);
  58.        
  59.         System.out.println("------------Size of stds1 table----------");
  60.         System.out.println("The size of the stds table is " +  
  61.         stds.size());
  62.         System.out.println("The size of the STDS2 table is " +  
  63.         stds2.size());
  64.         System.out.println("The size of the 'cleared' STDS1 table is " +  
  65.         stds1.size());
  66.        
  67.         System.out.println("--------------------KEYS-------------");
  68.         Enumeration enu = stds.keys();
  69.  
  70.         System.out.println("The enumeration of keys are:");
  71.        
  72.  
  73.         // Displaying the Enumeration
  74.         while (enu.hasMoreElements()) {
  75.             System.out.println(enu.nextElement());
  76.         }
  77.        
  78.         System.out.println("------------Compute used for UPDATE--------------");
  79.         Hashtable<String, Integer> table = new Hashtable<>();
  80.         table.put("Pen", 10);
  81.         table.put("Book", 500);
  82.         table.put("Clothes", 400);
  83.         table.put("Mobile", 5000);
  84.  
  85.         // print map details
  86.         System.out.println("hashTable: " + table.toString());
  87.  
  88.         // remap the values of hashTable
  89.         // using compute method
  90.         table.compute("Pen", (key, val)
  91.                                  -> val + 15);
  92.         table.compute("Clothes", (key, val)
  93.                                      -> val - 120);
  94.  
  95.         // print new mapping
  96.         System.out.println("new hashTable: " + table);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement