Java,设置特定的HashSet TreeSet

use*_*667 0 java tree hash set

试图从集合中的.txt文件中保存单词.从未使用套装.

Set<Word> set = new HashSet<Word>();
Run Code Online (Sandbox Code Playgroud)

保存在哈希集中时,我将大小设置为241.如果我将上面的内容更改为

Set<Word> set = new TreeSet<Word>();
Run Code Online (Sandbox Code Playgroud)

在代码中没有其他内容,我的大小为231.在不同的集合中是否存在某种自然原因?

Tom*_*ine 6

你可能没有Comparable.compareTo与之保持一致Object.equals/hashCode.那或hashCode与之不一致equals.可能实际上没有被覆盖hashCode- @Override用来确保.