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 7418127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:49:40+00:00 2026-05-29T07:49:40+00:00

I have a class called Node that I’ve written. I overrode the hashCode() function

  • 0

I have a class called Node that I’ve written. I overrode the hashCode() function of it to take into account two fields of the Node (there is also a third field which doesn’t affect the my hashCode() function). I also wrote an equals() function which takes all three fields into account.

I’m trying to use the Hashtable class to store Nodes so that I can easily check later on when I make new nodes whether the new nodes are duplicates of ones in the hashtable or not. So far I have this

 Hashtable<Node,Node> hashTbl = new Hashtable<Node,Node>();
 ...
 Node node1 = // some new node
 hashTbl.put(node1,node1);
 ...

So now, say I make a new node called node2 which has the exact same hash value as node1, but is not equal to node1 as defined by the equals() method. I want to check whether node2 is a duplicate of anything in the hash table (it’s not), but if I use constainsKey(), won’t that give me a false positive? It seems like using containsValue() wouldn’t be utilizing the efficiency of the hash table. So how can I do this efficiently?

Also, my assumption is that when I call hashTbl.put(arg1,arg2), it calls the hashCode() function of arg1 and uses that value to find an index in an “array” to place arg2 in. Is this right?

Sorry for being kind of confusing. Thanks anyone.

  • 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-29T07:49:41+00:00Added an answer on May 29, 2026 at 7:49 am

    First, you probably want a HashSet (or something similar), not a Hashtable – all you are trying to do is check for membership, and a HashSet will allow you to do that without needing to provide a value for every key.

    To answer your question, the determines which slot in the array that the value gets put in, but every slot is actually a linked list. If the new key is not .equal to any other key in the linked list, the new key and value are put in their own node in the linked list. Simply returning 1 for all objects is a perfectly legal and correct .hashcode implementation. The only issue with that implementation is that it turns Hashtables and similar data structures into linked lists (which obviously causes you to lose all the performance benefits of the Hashtable).

    In short, your .hashcode method will work fine. If you put a large number of objects that aren’t .equal but have the same hashcode, performance will decrease, but the code will still function correctly.

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

Sidebar

Related Questions

I have an abstract class called Node . It contains a constructor that takes
I have a class LinkedList clas that has a class called Node class Node
I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have a class called User with static function loginRequired(), which returns false if
I have a class called UserInfo that contains details about a given user. There
I have a class called Question that has a property called Type. Based on
I have a class that inherits from TreeNode, called ExtendedTreeNode. To add an object
I have a class called Node and another class called ClassicNode which extends Node.
I have one class called Holder (holder.cs) that contain the following: string name; List<String>
i have a class implemented from QGraphicsItem, called Node. i have a rectangle shaped

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.