Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39123
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2019-08-27 04:59 -0700 |
| References | <89cadee9-0108-4c76-9e94-c8f716290582@googlegroups.com> <qk1ogm$2l5$1@gioia.aioe.org> |
| Message-ID | <4b74a080-6be8-4c50-9d2b-48f5cd3dfb42@googlegroups.com> (permalink) |
| Subject | Re: Best way to make a hashcode? |
| From | Eric Douglas <e.d.programmer@gmail.com> |
On Monday, August 26, 2019 at 7:04:02 PM UTC-4, Arne Vajhøj wrote:
>
> Have tried letting your IDE generate hashCode and equals based
> on the fields you want?
>
> Arne
Didn't realize it could do that...so here's what it comes up with.
import java.util.HashMap;
public class TestClass1 {
String s1 = "abc";
String s2 = "def";
String s3 = "ghi";
int x = 1;
int y = 2;
float z = 3;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((s1 == null) ? 0 : s1.hashCode());
result = prime * result + ((s2 == null) ? 0 : s2.hashCode());
result = prime * result + ((s3 == null) ? 0 : s3.hashCode());
result = prime * result + x;
result = prime * result + y;
result = prime * result + Float.floatToIntBits(z);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TestClass1 other = (TestClass1) obj;
if (s1 == null) {
if (other.s1 != null) return false;
} else if (!s1.equals(other.s1)) return false;
if (s2 == null) {
if (other.s2 != null) return false;
} else if (!s2.equals(other.s2)) return false;
if (s3 == null) {
if (other.s3 != null) return false;
} else if (!s3.equals(other.s3)) return false;
if (x != other.x) return false;
if (y != other.y) return false;
if (Float.floatToIntBits(z) != Float.floatToIntBits(other.z)) return false;
return true;
}
public static void main(String[] args) {
TestClass1 c1 = new TestClass1();
TestClass1 c2 = new TestClass1();
HashMap<TestClass1,String> m1 = new HashMap<>();
m1.put(c1, "test");
System.out.println(m1.containsKey(c2)); //true
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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