Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39136

Re: Best way to make a hashcode?

From Marcel Mueller <news.5.maazl@spamgourmet.org>
Newsgroups comp.lang.java.programmer
Subject Re: Best way to make a hashcode?
Date 2019-09-06 19:42 +0200
Organization albasani.net
Message-ID <qku59q$bbs$1@news.albasani.net> (permalink)
References <89cadee9-0108-4c76-9e94-c8f716290582@googlegroups.com> <qk1jmg$8ck$1@dont-email.me> <5d65bc48$0$1715$e4fe514c@newszilla.xs4all.nl>

Show all headers | View raw


Am 28.08.19 um 01:27 schrieb Silvio:
> On 26-08-19 23:41, Eric Sosman wrote:
>>      public int hashCode() {
>>          long bits = java.lang.Double.doubleToLongBits(getX());
>>          bits ^= java.lang.Double.doubleToLongBits(getY()) * 31;
>>          bits ^= Objects.hashCode(getStringThing()) * 29;
>>          bits ^= Objects.hashCode(getStrungOut()) * 37;
>>          bits ^= Objects.hashCode(getStringBikini()) * 59;
> 
> Don't do this. Use the multiplier (no need for more than one if you pick 
> a good prime) for the accumulator and not the individual hash-codes.

+1

You can accumulate an arbitrary number of hash codes e.g. this way:

hash = firstHashCode;
hash = 1566083941 * hash + secondHashCode;
hash = 1566083941 * hash + thirdHashCode;
...


Marcel

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Best way to make a hashcode? Eric Douglas <e.d.programmer@gmail.com> - 2019-08-26 13:51 -0700
  Re: Best way to make a hashcode? Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-08-26 17:41 -0400
    Re: Best way to make a hashcode? Eric Douglas <e.d.programmer@gmail.com> - 2019-08-27 06:24 -0700
      Re: Best way to make a hashcode? Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-08-27 09:39 -0400
        Re: Best way to make a hashcode? Eric Douglas <e.d.programmer@gmail.com> - 2019-08-27 07:29 -0700
          Re: Best way to make a hashcode? Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-08-27 11:49 -0400
            Re: Best way to make a hashcode? Eric Douglas <e.d.programmer@gmail.com> - 2019-08-27 11:05 -0700
              Re: Best way to make a hashcode? Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-08-27 15:16 -0400
                Re: Best way to make a hashcode? Eric Douglas <e.d.programmer@gmail.com> - 2019-08-27 13:04 -0700
                Re: Best way to make a hashcode? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-08-27 23:51 +0200
    Re: Best way to make a hashcode? Silvio <silvio@internet.com> - 2019-08-28 01:27 +0200
      Re: Best way to make a hashcode? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2019-09-06 19:42 +0200
  Re: Best way to make a hashcode? Arne Vajhøj <arne@vajhoej.dk> - 2019-08-26 19:03 -0400
    Re: Best way to make a hashcode? Eric Douglas <e.d.programmer@gmail.com> - 2019-08-27 04:59 -0700
      Re: Best way to make a hashcode? Arne Vajhøj <arne@vajhoej.dk> - 2019-08-27 21:08 -0400
        Re: Best way to make a hashcode? ross.finlayson@gmail.com - 2019-09-05 20:27 -0700
  Re: Best way to make a hashcode? trisha guillot <guillottrisha347@gmail.com> - 2020-01-18 07:25 -0800

csiph-web