Java - 重写hashCode和toString(Java - Overriding hashCode and toString)
当两个对象在等级A中具有相同的
ele
值时,那么这两个对象是相等的。 所以我重写了toString和hashCode来返回对象的ele
(不考虑s
的值)。public class A { private int ele; private String s; public int getEle() { return ele; } public void setEle(int ele) { this.ele = ele; } public String getS() { return s; } public void setS(String s) { this.s = s; } @Override public int hashCode(){ return ele; } @Override public String toString() { return String.valueOf(ele); } }
public static void main(String[] args) { Map<A, String> map = new HashMap<>(); A a1 = new A(); a1.setEle(10); a1.setS("abc"); A a2 = new A(); a2.setEle(10); a2.setS("efg"); map.put(a1, "val1"); map.put(a2, "val2"); System.out.println(map.get(a1)); System.out.println(map.get(a2)); }
输出:
val1 val2
但是,如果我将
a1
和a2
值放在地图中,我期望map.get(a1)
map.get(a2)
map.get(a1)
和map.get(a2)
map.get(a1)
都返回val1
或val2
。When two objects have same value of
ele
in class A then those two objects are equal. So I have overridden toString and hashCode to return the object'sele
(not considering the value ofs
anywhere).public class A { private int ele; private String s; public int getEle() { return ele; } public void setEle(int ele) { this.ele = ele; } public String getS() { return s; } public void setS(String s) { this.s = s; } @Override public int hashCode(){ return ele; } @Override public String toString() { return String.valueOf(ele); } }
public static void main(String[] args) { Map<A, String> map = new HashMap<>(); A a1 = new A(); a1.setEle(10); a1.setS("abc"); A a2 = new A(); a2.setEle(10); a2.setS("efg"); map.put(a1, "val1"); map.put(a2, "val2"); System.out.println(map.get(a1)); System.out.println(map.get(a2)); }
Output:
val1 val2
But if I put value of
a1
anda2
in a map, I was expecting eitherval1
orval2
to be returned for bothmap.get(a1)
andmap.get(a2)
.
原文:https://stackoverflow.com/questions/36290579
最满意答案
它被称为砰操作员。
这意味着,使用这种类型的默认属性。
Its called the Bang Operator.
It means, use the default property of this type.
It was very common in VB6 code, used to access the fields of a Recordset and avoid trouble with field names that were also a keyword. An alternative to the dot operator and [brackets]. The bang still works:
value = row!column1
instead of
value = row("column1")
Consider it a typing aid, use at your discretion.
相关问答
更多-
是什么意思 !(What is the meaning of ! in the code?)[2023-03-16]
它被称为砰操作员。 这意味着,使用这种类型的默认属性。 https://stackoverflow.com/questions/1845646/vb-net-doesnt-anyone-use-the-dictionary-member-access-expression-aka-the-b Its called the Bang Operator. It means, use the default property of this type. It was very common in VB6 code ... -
以下是什么意思?(What is the meaning of the following?)[2023-06-28]
不, sampleArray并不真正指向数组的第一个元素。 sampleArray 是数组。 出现混淆的原因是,在使用sampleArray大多数地方,它将被指向数组的第一个元素的指针替换。 “大多数地方”的意思是“任何地方,它不是sizeof或一元&操作符的操作数”。 由于sampleArray是数组本身,并且作为一元操作数( &是它维护个性的地方之一,这意味着&sampleArray是指向整个数组的指针。 No, sampleArray does not really point to the firs ... -
用于应用程序执行控制的一组页面。 PageSet管理,存储,匹配和执行Pages。 这就是我能在这个词上找到的全部内容。 基本上如果说你有一个博客,你可以有一个用于修改条目的页面集,如添加,编辑,删除。 这些操作中的每一个都将具有不同的页面和控件来运行该操作。 页面集将是那些组。 A set of Pages used for application execution control. A PageSet manages, stores, matches and executes Pages. That ...
-
这是什么意思”?(What is the meaning of “this”?)[2022-05-16]
在你的代码片段中, this(port, 50, null)表示对同一个类中的构造函数的显式调用,该构造函数有三个参数,其中两个参数传递硬编码参数( 50和null ) In your code snippet, this(port, 50, null) denotes an explicit call to a constructor in the same class, which has three parameters, for two of which you pass hardcoded arg ... -
它必须是if ((integer & 2) != 0) 。 Java不像C++那样将正整数识别为true ,将0识别为false 。 否则,它测试是否设置了右边的第二个位,即它是1。 It must be if ((integer & 2) != 0). Java does not recognize positive integers as true and 0 as false like in C++. Otherwise it tests if the second bit from the righ ...
-
这段代码是什么意思?(What is the meaning of this code?)[2022-03-10]
您可以在MSDN中看到“ String.IndexOf Method(String) ”。 You can see in MSDN, "String.IndexOf Method (String)". -
是什么意思(void *)&(what is the meaning of (void*)&)[2023-05-22]
指向由&给出的internalIPItr->second.outboundPortIPs地址的指针将转换为void* 。 The pointer to the address of internalIPItr->second.outboundPortIPs, given by &, is converted to void*. -
是什么意思 '#'?(What is the meaning of '#'?)[2022-03-18]
你需要一个语言扩展甚至能够使用它,这是多么特别! 它没有做任何事情,它只是一个看起来很特别的角色,GHC人们喜欢用低级别的可能与拳击相关的东西。 有(# a, b #)这是a和b的未装箱元组的类型,这是你在那里看到的 - 它不是( #State# , ... ,它是(# State#, ... ...... You need a language extension to even be able to use it, that's how special it is! It doesn't do anyt ... -
用语言: 注释标志已设置( (?x) )。 字符串( ^ )的开头必须是第一个,后面必须跟( ?= )零个或多个字符( .* ),然后是( (||) )一个数字( \d ),一个标点符号( \p{P} )或符号( \p{S} )。 字符串开始后,必须有6个或更多字符( .{6,} ) 这解释了吗? In words: The comments flag is set ((?x)). The start of the string (^) must be first, which must be follow ...
-
要知道它是哪个字符,请将光标放在^K并在正常模式下按g a 。 你可能会得到: <^K> 11, Hexa 0b, Octal 013 它有ascii代码11所以它可能是vertical tab 。 您可以使用此组合Ctrl-v + Ctrl-k创建它 To know which character it is, put the cursor on ^K and press ga in normal-mode. You will get probably: <^K> 11, Hexa 0b, ...