java programming

Posted on 2004-11-29
Last Modified: 2010-04-17
How do we add a class to a Collection object using hashtable(using a string as key)?
Question by:sraveend
    LVL 48

    Assisted Solution


    Assisted Solution

    or, just to write is out:

    public void addToHashTable()
        HashTable myHashTable = new HashTable();
        String key = "this is the key";
        Object value = new Object();

        myHashTable.put(key, value);

    that's it :)
    LVL 8

    Accepted Solution

    import java.util.*;
    class Hashme {

            public static void main (String arg[]) {

                    Hashtable h = new Hashtable ();


                    //put new elements in a hash table

                    h.put("one", new Integer(32510) );

                    h.put("Two", new Integer(52510) );

                    h.put("Three", new Integer(12013) );

                    System.out.println("Hash table contains: " + h);


                    // Check if the hash table is empty

                    System.out.println("Hash table is empty? " + h.isEmpty());


                    // getting value from a hash table against a key

                    System.out.println("Two contains Value: " + h.get("Two"));


                    //finding a value

          System.out.println("The value 32510 exists? " + h.contains (new Integer(32510)));


                    //finding a key
          System.out.println("The key three exists? " + h.containsKey ("Three"));

          //removing an object against a key
                System.out.println("Removing three: " + h.remove ("Three"));

          //finding the latest status of hash table
    System.out.println("Hash table now contains: " + h);

          System.out.println("Size is: " + h.size());



    find some info. about Hashtables:

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    This article will show, step by step, how to integrate R code into a R Sweave document
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now