Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Hashtable in Java?

Hi, i want to use Java's hashtable class in a college assignment. We are making a virtual market (like Amazon.com) and every product added to the market, must have product properties. (like a notebook PC having dual core processor).

We also have to use a hashtable to search the market. For example when the costumer searches for the keyword "dual" , every product with this word in its properties, must come to the screen.

It's like searching in Amazon.com. I know the hashtable codes in java, but how can i implement that? Thanks

Update:

Ted, you mean a hashtable containing vectors? That's what i have to use then, but i wish i could assign different objects to the same key, that would be easier.

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    Define a class (ProductList) which you can give a 'type' such as dual-core, and that contains a reference to every product of that type. The class is probably nothing more than a String (property name) and an array of pointers to objects. The methods are probably just add(), get_first(), and get_next().

    Create a Hashtable object, Plist lets say, that will be used to reference ProductList objects.

    When you create a new product, you find the ProductList for each of the product's attributes and add the product to the list. If no ProductList object exists for any of the attributes it is created, the product is added and it is added to the Plist hashtable object.

    When a user searches for an attribute, you use your hash to get the ProductList object that matches that attribute, and then display/print all of the products that the ProductList object references.

  • 5 years ago

    Or just use generics if you are using jdk1.5 or greater. When you declare Hashtable, declare it like this. Hashtable<String,Student> table = new Hashtable<String,Student>(); You dont need to change anything else in your code.

Still have questions? Get your answers by asking now.