How to use Hashtable in Java

2 Answers

0 votes
import java.util.Hashtable;
import java.util.Enumeration;

public class MyClass {
    public static void main(String args[]) {
        Hashtable<String, String> ht = new Hashtable<String, String>();
     
        ht.put("Key1", "java");
        ht.put("Key2", "c");
        ht.put("Key3", "c#");
        ht.put("Key4", "python");
        ht.put("Key5", "c++");
        ht.put("Key6", "c++"); 
         
        Enumeration en = ht.keys();
        String key;
        while (en.hasMoreElements()) {
            key = (String) en.nextElement();
            System.out.println("Key: " + key + " Value: " + ht.get(key));
        }
    }
}



/*
run:

Key: Key4 Value: python
Key: Key3 Value: c#
Key: Key2 Value: c
Key: Key1 Value: java
Key: Key6 Value: c++
Key: Key5 Value: c++

*/

 



answered Apr 29, 2020 by avibootz
0 votes
import java.util.Hashtable;

public class MyClass {
    public static void main(String args[]) {
        Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
     
        ht.put(4, "java");
        ht.put(6, "c");
        ht.put(1, "c#");
        ht.put(3, "python");
        ht.put(2, "c++");
        ht.put(5, "c++"); 
         
       System.out.println(ht);
    }
}



/*
run:

{6=c, 5=c++, 4=java, 3=python, 2=c++, 1=c#}

*/

 



answered Apr 29, 2020 by avibootz

Related questions

1 answer 132 views
132 views asked Apr 28, 2020 by avibootz
1 answer 110 views
1 answer 165 views
1 answer 133 views
133 views asked Apr 29, 2020 by avibootz
1 answer 138 views
138 views asked May 10, 2020 by avibootz
5 answers 369 views
369 views asked Sep 11, 2019 by avibootz
1 answer 207 views
...