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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:57:54+00:00 2026-05-23T12:57:54+00:00

I have created the Event class. As you can see, both hashCode and equals

  • 0

I have created the Event class. As you can see, both hashCode and equals methods use only the id field of type long.

public class Event {
private long id;
private Map<String, Integer> terms2frequency;
private float vectorLength;

@Override
public long hashCode() {
    return this.id;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Event other = (Event) obj;
    if (id != other.id)
        return false;
    return true;
}

I will store the objects of this class in the HashSet Collection.

Set<Event> events = new HashSet<Event>();

Since for the hash computation only the field of the type long I’d like to retrieve the elements from the events hashset by computing the hash of the id. E.g.:

events.get(3);

Is it possible or should I use the hashMap for it:

Map<Long, Event> id2event = new HashMap<Long, Event>();

?

  • 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-23T12:57:54+00:00Added an answer on May 23, 2026 at 12:57 pm

    You should absolutely not rely on hash code uniqueness. A long has 264 possible values; an int only has 232. Therefore hash collisions are entirely possible. Don’t use hash codes as your sole equality test. That’s not what they’re designed for.

    Hash codes are designed to quickly get from a key to a set of potential matches, which are then checked more rigorously with normal equality.

    (As an aside, I don’t think it’s a great idea to use floatToIntBits to compute the hash code to start with. Look at what Long.hashCode() does.)

    EDIT: Of course, even if you did want to rely on that, HashSet<E> doesn’t expose a method for getting an element by its hash code, precisely because it’s a really bad idea in almost all cases… if you want a mapping, create a Map…

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

Sidebar

Related Questions

I have created this datastructure: class Event { public: Event(EVENT_TYPE type, void* pSender =
I have created a JFrame with a canvas in it, like you can see
I have simple SSIS package in which On Error event handler I have created
We have repositories which have a Save method. They also throw a Created event
I have a global event created and set/reset in a native C++ process that
I have an application that has created a number of custom event log sources
I have created a BasePage class that inherits from System.Web.Ui.Page. In that base class
I'm trying to use the Class methods of prototype.js to manage my object hierarchy
I have created a library which can download JSON data which is then placed
I have event manager process that dispatches events to subscribers (e.g. http_session_created, http_sesssion_destroyed). 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.