Redian新闻
>
请教电化学/化工专家
avatar
请教电化学/化工专家# ChemEng - 化学工程
b*g
1
All set cares uniqueness and equals() determine whether two object are
identical.
但是我test 了下
import java.util.HashSet;
public class testT {
private String name;
public testT(String name){
this.name=name;
}
public boolean equals(Object o){
if(!(o instanceof testT))return false;
testT person=(testT)o;
return person.name.equals(this.name);
}
public static void main(String[] args){
HashSet hs=new HashSet();
testT p1= new testT("jing");
testT p2=new testT("jing");
hs.add(p1);
hs.add(p2);
System.out.println(hs.size());
System.out.println(p1.equals(p2));
for(Object obj:hs)
System.out.println(obj);

}
}
输出是:
2
true
[email protected]
[email protected]
为啥两个object 被认为是equals的,缺还能加在同一个hashset里?
avatar
t*d
2
需要测微通道里电解质阻抗的变化.
我建了个电路,用电阻模拟可以测到很小的电阻变化(0.1%或更小).
但微通道里电解质阻抗的变化怎么也测不到,按道理变化应该大于0.1%.
我用的是10KHZ的AC.
谁有这方面经验,请不惜指教. 谢谢
avatar
g*g
3
You need to override hashCode function as well.

【在 b******g 的大作中提到】
: All set cares uniqueness and equals() determine whether two object are
: identical.
: 但是我test 了下
: import java.util.HashSet;
: public class testT {
: private String name;
: public testT(String name){
: this.name=name;
: }
: public boolean equals(Object o){

avatar
b*g
4
the statement is true:
the hash code method for a given class can be used to test for object
inequality,but not object equality, for that class.
hascode 和 inequality是什么关系?

【在 g*****g 的大作中提到】
: You need to override hashCode function as well.
avatar
g*g
5
A hashmap checks if an object exists by computing hashcode for the
object, inspect the bucket for given hashcode, and finally using
equals to determine equality.

【在 b******g 的大作中提到】
: the statement is true:
: the hash code method for a given class can be used to test for object
: inequality,but not object equality, for that class.
: hascode 和 inequality是什么关系?

avatar
b*g
6
所以hashcode 不等,两个object 仍然可以equal。hascode 并不是equal的前提条件。
HashSet认为的uniqueness是hashcode相等同时equals() return ture.
我得再消化消化。谢谢。

【在 g*****g 的大作中提到】
: A hashmap checks if an object exists by computing hashcode for the
: object, inspect the bucket for given hashcode, and finally using
: equals to determine equality.

avatar
g*g
7
Well, you should always implement hashcode and equals at the same time.
That's a required practice, not doing that is calling trouble.
Two objects can have the same hashcode but not equal, not vice versa.

【在 b******g 的大作中提到】
: 所以hashcode 不等,两个object 仍然可以equal。hascode 并不是equal的前提条件。
: HashSet认为的uniqueness是hashcode相等同时equals() return ture.
: 我得再消化消化。谢谢。

avatar
b*g
8
明白了.override 下 hashcode()就全明白了,多谢!

【在 g*****g 的大作中提到】
: You need to override hashCode function as well.
avatar
b*g
9
not vice versa??
two objects are equal but with different hashcode. is it ture?

【在 g*****g 的大作中提到】
: Well, you should always implement hashcode and equals at the same time.
: That's a required practice, not doing that is calling trouble.
: Two objects can have the same hashcode but not equal, not vice versa.

avatar
g*g
10
It should never happen, if it happens, it's a bug.

【在 b******g 的大作中提到】
: not vice versa??
: two objects are equal but with different hashcode. is it ture?

avatar
r*y
11
跟 hashcode 没关系, 你的 equals() 方法实现错误
的 equals() 中 -- return person.name.equals(this.name);
比较的就是 string pool 中的静态string "jing"
你两次new testT 都指向同一个 string object
当然 equals() 是 true
另外, 即使你 new String("jing") 两次, 用 string 的equals 还会是 true
因为 String 的 equals 在比较它内涵的 char[]

are

【在 b******g 的大作中提到】
: All set cares uniqueness and equals() determine whether two object are
: identical.
: 但是我test 了下
: import java.util.HashSet;
: public class testT {
: private String name;
: public testT(String name){
: this.name=name;
: }
: public boolean equals(Object o){

avatar
r*l
12
Goodbug is right. You always need to override equals() and hashCode()
together in such a way that:
1, If o1.equals(o2) returns true, o1.hashCode() MUST equal o2.hashCode().
2, if o1.hashCode() == o2.hashCode(), o1.equals(o2) can be true or false;

【在 g*****g 的大作中提到】
: It should never happen, if it happens, it's a bug.
avatar
r*l
13
The equals() can be written in this way as long as it has business sense,
and as long as hashCode() is written to conform this.

【在 r***y 的大作中提到】
: 跟 hashcode 没关系, 你的 equals() 方法实现错误
: 的 equals() 中 -- return person.name.equals(this.name);
: 比较的就是 string pool 中的静态string "jing"
: 你两次new testT 都指向同一个 string object
: 当然 equals() 是 true
: 另外, 即使你 new String("jing") 两次, 用 string 的equals 还会是 true
: 因为 String 的 equals 在比较它内涵的 char[]
:
: are

avatar
z*3
14
你都重写equals了,而且直接调用这个方法
跟hashcode一点关系都没有了
只是理论上说这两个方法必需保持一致
avatar
z*3
15
突然想起来楼主在问什么了
楼主如果用这个方法做
String a = new String("1");
String b = "1";
HashSet set = new HashSet();
set.add(a);
set.add(b);
然后循环后的结果就是只有一个对象输出
所以hashcode其实就是给hashset做逻辑判断用的
所以只要是自定义的类,放到这个hashset等类里面去
就需要思考hashcode的问题
因为常用的几个类,String什么都重写过hashcode方法
保证同一内容输出的是同一个hashcode
而自定义的类的hashcode默认是每个对象都不一样
所以hibernate里面要重写hashcode
avatar
z*3
16
其实说白了就是如何判断两个对象是一个东西
无论是hashcode还是equals
本质意义都是用来判断两个已经存在的对象是不是同一个实体
举个例子
一个学生只有一个学号
那么如果学号一致,那么肯定就是一个学生
这就对应数据库里面的主键
但是在多线程环境中,是很有可能产生这种重复的对象的
那么如何判断这两个是一个东西?
所以就需要你重写这两个方法
区别在于,hashcode是给collection用的
其它时候还是用equals判断
如果你不重写这两个方法
那么除非正好是同一个虚拟机上的同一个对象
也就是地址是一致的时候,才会被认为是同一个对象
其它时候,只要没有重写过这两个方法,就会被认为是不同对象
就好像一个学号有两个不同的人在用一样,这是不对的
尤其是对于自定义的对象来说
常用的String, Integer什么都重写过了
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。