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

  • Home
  • SEARCH
  • 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 6014275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:40:07+00:00 2026-05-23T02:40:07+00:00

This is the code in question, simplified for this example: /** A version of

  • 0

This is the code in question, simplified for this example:

/** A version of Hashtable that lets you do
 * table.put("dog", "canine");, and then have
 * table.get("dogs") return "canine". **/
public class HashtableWithPlurals extends Hashtable {
  /** Make the table map both key and key + "s" to value. **/
  public Object put(Object key, Object value) {
    super.put(key + "s", value);
    return super.put(key, value);
  }
}
  • 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-05-23T02:40:08+00:00Added an answer on May 23, 2026 at 2:40 am

    Oh goodness you overwritten a hashtable. Yeah the documentation isn’t especially useful in this case.

    I’ll just cite Peter Norvig on that one, since he said it better than I could:

    public class HashtableWithPlurals extends Hashtable {
    
      /** Make the table map both key and key + "s" to value. **/
      public Object put(Object key, Object value) {
        super.put(key + "s", value);
        return super.put(key, value);
      }
    }
    

    You need to be careful when passing to
    super that you fully understand what
    the super method does. In this case,
    the contract for Hashtable.put is that
    it will record a mapping between the
    key and the value in the table.
    However, if the hashtable gets too
    full, then Hashtable.put will allocate
    a larger array for the table, copy all
    the old objects over, and then
    recursively re-call table.put(key,
    value). Now, because Java resolves
    methods based on the runtime type of
    the target, in our example this
    recursive call within the code for
    Hashtable will go to
    HashtableWithPlurals.put(key, value),
    and the net result is that
    occasionally (when the size of the
    table overflows at just the wrong
    time), you will get an entry for
    “dogss” as well as for “dogs” and
    “dog”. Now, does it state anywhere in
    the documentation for put that doing
    this recursive call is a possibility?
    No. In cases like this, it sure helps
    to have source code access to the JDK.

    The solution? Don’t extend HashTable but use a wrapper class that stores internally a HashTable and forwards the necessary methods to it (not a 100% perfect solution but it’ll be good enough in most cases and doesn’t have those problems). Well or look at the source code extremely well and make sure you understand exactly what’s going on.. and write lots of tests (and some fuzztests)

    PS: That’s pretty much my favorite example when arguing with people that think OOP makes everything so much easier and completely foolproof – there’s still no silver bullet 😉

    PPS: Considering that both examples are pretty much the same – care to tell us where you got it? Just curious, because it seems someone took the norvig post as inspiration here 😉

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

Sidebar

Related Questions

This is a code review question more then anything. I have the following problem:
I was trying to make a simplified version of my code for this question
This question is partly about delegates, and partly about generics. Given the simplified code:
I have a question concerning this code which I want to run on QNX:
[EDIT: Simplified question and code example I hope!] Hi. I need a bit of
I have some troubles with the application of polymorphism in this example. This question
Given this highly simplified example: abstract class Animal { } class Dog : Animal
In answering this code golf question , I ran across a problem in my
This isn't a code question for once, but it definitely has me confused. Basically,
This is a silly question, but you can use this code to check if

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.