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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:38:42+00:00 2026-05-22T20:38:42+00:00

In looking at different options for overriding hashCode() , I was directed to Objects.hashCode(Object[])

  • 0

In looking at different options for overriding hashCode(), I was directed to Objects.hashCode(Object[]) in Google’s guava-libraries (javadoc). The javadoc states that it delegates to Arrays.hashCode(Object[]). Is it safe to use this method in many different object types? Isn’t this prone to hash collision, or is this not likely simply because containers usually only contain one type of object?

As a simple example, consider the following classes,

public class Student {
    private final String name;

    public Student(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(name);
    }
}

public class Teacher {
    private final String name;

    public Teacher(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(name);
    }
}

public class HashCodeDriver {
    public static void main(String[] args) {
        final String name = "moe";
        Student s = new Student(name);
        Teacher t = new Teacher(name);

        long studentHash = s.hashCode();
        long teacherHash = t.hashCode();
        System.out.println("studentHash=" + studentHash + " teacherHash=" + teacherHash);
        if(studentHash == teacherHash) {
            System.out.println("hash codes match");
        }
        else {
            System.out.println("hash codes don't match");
        }
    }
}

Output:

studentHash=108322 teacherHash=108322
hash codes match

The objects are two different types but are generating the same hash code. Isn’t this a problem? Should I pass in the class as the first parameter to prevent this collision? For example,

public class Student {
    private final String name;

    public Student(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(Student.class, name);
    }
}

public class Teacher {
    private final String name;

    public Teacher(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(Teacher.class, name);
    }
}

Is this why the javadoc warns about only supplying a single object to this method? From the javadoc,

Warning: When a single object is supplied, the returned hash code does not equal the hash code of that object.

  • 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-22T20:38:43+00:00Added an answer on May 22, 2026 at 8:38 pm

    It’s not a problem when 2 different objects of 2 different types have the same hash code.

    Hopefully, when you are going to build your HashMap you are not going to mix Students and Teachers as the keys to that map. And even in the case when you want to do HashMap<Object, Object> you will be OK, because

    assertFalse( new Teacher( "John Smith" ).equals( new Student( "John Smith" ) );
    

    This is why it’s important to override both hashCode and equals.

    The only drawback of delegating to Arrays.hashCode(Object[]) may be that sometimes it may be too expensive from the performance point of view.

    For example, in your case, this would be a much better hash method for either Teacher or Student.

    @Override
    public int hashCode() {
        return name.hashCode();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking around at the different options, and it seems that even some
Looking at different options: One is to just put the static pages in the
I am currently looking at different ORM solutions for a project that is planned
I'm looking for a way to poll different servers and check that SQL server
I'm looking at different options to get the sales reports and other data out
I'm currently looking into different options for unit testing Silverlight applications. One of the
I've been looking at some different options for reading RSS Feeds in .Net. I've
Looking to place different icons in front of items/options in a HTML select box.
I've got analysis paralysis looking at all the different functional testing options for a
I am looking for options on how to draw 2 rulers at different scales

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.