Why does my class not work correctly in a Java HashSet?

微信扫一扫,分享到朋友圈

Why does my class not work correctly in a Java HashSet?

I am working on a project that involves me using a HashSet
of a class I made, which I will name Test
. I defined the stated HashSet
like so:

HashSet<Test> t = new HashSet<Test>();
t.add(new Test("asdf", 1));
t.add(new Test("hello", 2));
t.add(new Test("hello", 3));

I tried using

t.contains(new Test("asdf", 1));

but it returns false
. However, when I use a HashSet<Character>
it seems to work fine. I tried overriding the previous equals
declaration, but it didn’t work. I tried leaving equals
alone, but i still got false
. I need to know what i am doing wrong?

also, i did not edit the hash function, i only changed Test.equals(Object o). It’s a simple project and since the java documentation states that it uses o.equals(this), i thought i would not have to.

微信扫一扫,分享到朋友圈

Why does my class not work correctly in a Java HashSet&quest;

创建自己的新冠病毒疫情跟踪器(Node.js+React+TS)

上一篇

手把手教你入门V8漏洞利用

下一篇

你也可能喜欢

Why does my class not work correctly in a Java HashSet&quest;

长按储存图像,分享给朋友