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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:55:39+00:00 2026-06-13T22:55:39+00:00

What do you think about using Objects#hash(Object…) in a hashCode() method? int a =

  • 0

What do you think about using Objects#hash(Object...) in a hashCode() method?

int a = 1; 
boolean b = true; 
Date c = new Date(); 
String d = "1234";
Object e = new ch.example.blabla.Foo();

// Java 7
public int hashCode() {
  return Objects.hash(a, b, c, d, e);
}

// or using Java 6 
public int hashCode() {
  return Arrays.hashCode(new Object[] {a, b, c, d, e});
}

Of course under normal circumstances such as having an equals(Object) method too and so on. Joshua Bloch writes in his book how to write a good hashCode() method using his rules/recipe such as shifting bits and so on.

Above example does not follow these rules for primitive datatypes… so my question is, is it ok to handle primitive datatypes like objects (autoboxing) instead of following Bloch’s recipe or using Apache Commons HashCodeBuilder?

Objects#hash(Object...) was introduced in Java 7 and invokes only Arrays.hashCode(Object[]), so this question is also focused to Java 6 users.

Thank you for your responses/ideas/suggestions!


I really don’t understand when older questions are getting closed as a duplicate because of newer ones.

However, there is a question with better answers here:

  • Use Objects.hash() or own hashCode() implementation?
  • 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-06-13T22:55:40+00:00Added an answer on June 13, 2026 at 10:55 pm

    Wrapper classes like Integer, Double and Boolean do indeed implement the rules which are described in Effective Java of Joshua Bloch. Thank you to Marko Topolnik for highlighting that out.

    He’s also mentioning a downside which could lead into performance issues. However, Jörn Horstmann assumes that this might be optimized by mordern JVMs.

    We’re going with this approach (Objects.hash(a, b, c)) since it’s way more readable than writing our own hashCode() method. And if really necessary (if we run into performance issues) we do implement primitive hash code by ourselves.

    Marko, Jörn, thank you for your comments. If you’re going to write an answer, I’ll change the accepted answer to the best answer.

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

Sidebar

Related Questions

One thing I have continually found very confusing about using an object database like
What do you think about the concept of using ORO instead of traditional Garbage
I think this is a moderately neophyte question. I've been using NHibernate/FluentNHibernate for about
VB.NET 2010, .NET 4 Hello, I recently read about using SynchronizationContext objects to control
I started to think about tracking changes in complex object graph in disconnected application.
I'm new to MVC and am unsure about proper design. I have class objects
Think about doing this: import matplotlib.pyplot as plt plt.plot(x_A,y_A,'g--') plt.plot(x_B,y_B,'r-o') plt.show() How would you
Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).
I cannot think about a proper question title to describe the problem. Hopefully the
How do you think about data access code like this: public void AddCusotmer(Cusotmer customer)

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.