[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java programming

Posted on 2004-11-29
3
Medium Priority
?
1,964 Views
Last Modified: 2010-04-17
How do we add a class to a Collection object using hashtable(using a string as key)?
0
Comment
Question by:sraveend
3 Comments
 
LVL 48

Assisted Solution

by:Mikal613
Mikal613 earned 400 total points
ID: 12695927
0
 

Assisted Solution

by:sirono
sirono earned 400 total points
ID: 12696479
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 :)
0
 
LVL 8

Accepted Solution

by:
rk_radhakrishna earned 1200 total points
ID: 12696950
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:
http://www.javaworld.com/javaqa/2002-06/01-qa-0621-hashtable.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Screencast - Getting to Know the Pipeline

830 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