Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:12:05+00:00 2026-06-15T14:12:05+00:00

This first part is an example I found from the web and modified to

  • 0

This first part is an example I found from the web and modified to quickly try different solutions. As the title states I cant figure out how to pull the WordHold object so that I can continue to modify it when I need to retrieve it.

import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Set;

public class HashtableDemo {

    public static void main(String args[]) {
        WordHold temp = new WordHold("test1", "test2");
        WordHold temp2;
        // Creating Hashtable for example
        Hashtable companies = new Hashtable();

        // Java Hashtable example to put object into Hashtable
        // put(key, value) is used to insert object into map
        companies.put("Google", temp);
        companies.put("Nokia", "Finland"); 
        companies.put("Sony", "Japan");

        // Java Hashtable example to get Object from Hashtable
        // get(key) method is used to retrieve Objects from Hashtable
        temp2 = companies.get("Google");

        // Hashtable containsKey Example
        // Use containsKey(Object) method to check if an Object exits as key in
        // hashtable
        System.out.println("Does hashtable contains Google as key: "
            + companies.containsKey("Google"));

        // Hashtable containsValue Example
        // just like containsKey(), containsValue returns true if hashtable
        // contains specified object as value
        System.out.println("Does hashtable contains Japan as value: "
            + companies.containsValue("Japan"));

        // Hashtable enumeration Example
        // hashtabl.elements() return enumeration of all hashtable values
        Enumeration enumeration = companies.elements();

        while (enumeration.hasMoreElements()) {
            System.out
            .println("hashtable values: " + enumeration.nextElement());
        }

        // How to check if Hashtable is empty in Java
        // use isEmpty method of hashtable to check emptiness of hashtable in
        // Java
        System.out.println("Is companies hashtable empty: "
            + companies.isEmpty());

        // How to find size of Hashtable in Java
        // use hashtable.size() method to find size of hashtable in Java
        System.out.println("Size of hashtable in Java: " + companies.size());

        // How to get all values form hashtable in Java
        // you can use keySet() method to get a Set of all the keys of hashtable
        // in Java
        Set hashtableKeys = companies.keySet();

        // you can also get enumeration of all keys by using method keys()
        Enumeration hashtableKeysEnum = companies.keys();

        // How to get all keys from hashtable in Java
        // There are two ways to get all values form hashtalbe first by using
        // Enumeration and second getting values ad Collection

        Enumeration hashtableValuesEnum = companies.elements();

        Collection hashtableValues = companies.values();

        // Hashtable clear example
        // by using clear() we can reuse an existing hashtable, it clears all
        // mappings.
        companies.clear();
    }
}

The subclass being used.

public class WordHold
{
    // instance variables - replace the example below with your own
    private String wrongWord;
    private String correctWord;

    private WordHold next;

    public WordHold(String wordWrong, String wordCorrect)
    {
        wrongWord = wordWrong;
        correctWord = wordCorrect;
    }

    /**
     * Returns the wrong word
     * 
     * @return     The stored wrong word.
     */
    public String getWrongWord()
    {
        return wrongWord;
    }

    /**
     * Returns the correct word
     * 
     * @return     The stored wrong word.
     */
    public String getCorrectWord()
    {
        return correctWord;
    }

    /**
     * Returns the next element
     * 
     * @return     The next element in list.
     */
    public WordHold getNext()
    {
        return next;
    }

    /**
     * Sets next element in the list.
     */
    public WordHold setNext()
    {
        return next;
    }

    public String toString()
    {
        String temp = (wrongWord + " no " + correctWord);

        return temp;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:12:06+00:00Added an answer on June 15, 2026 at 2:12 pm

    I only skimmed through your code and found that you are not using generics with your collections. i guess, when you try to get the WorldHold object out of your Hashtable you will have to cast it to WorldHold, as if you dont use generics, it would return java.Lang.Object .

    Declaring your hashtable like:

    Hashtable<WordHold> companies = new Hashtable<WordHold>();
    

    would solve the problem.

    or if you dont want to use generics, you will have to explicitly cast the returned Object to WordHold.

        temp2 = (WordHold)companies.get("Google");
    

    I strongly suggest you to use generics if you are using java 1.5+. as they add compile time safety to your collections. if you dont use them, you could simply add any objects into your collections and also, you can get rid of such un-wanted casting while retriving the object back from the collection.

    Read about generics In java

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The first part of this question is actually a request for confirmation based on
The first part of this question is now its own, here: Analyzing Text for
This is a two-part question: First, I am interested to know what the best
This is kinda....a two part question. The first one is much more important than
This is a two part question. The first is how do you properly close
This is really a three-part question, but I've answered the first question myself: I'm
This is a two part question, but I want to make sure the first
This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo
I have this code. It's from the Zend Reading Mail example. $message = $mail->getMessage(1);
I have found following working example of CGI with bash. If I change first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.