Tag: hashset

在HashMap上使用HashSet的优点

根据JavaDoc API,HashSet只是HashMap的包装器。 因此,在HashMap上使用HashSet会有任何性能优势。 自从? 自从? 或者只是有一个不同的API适合其他情况?

如何在不改变equals和hashcode的情况下插入到set中

我正在寻找一个建议。 我有一个带有firstName和String lastName的Person类当我想要插入具有相同String的列表值时: set.add(new Person(“firstName”,”lastName”)) set.add(new Person(“firstName”,”lastName”)) 该集合不会过滤对象,他们仍然进入集合。 有没有建议创建集合列表而不覆盖equales和hashcode函数? 也许用番石榴或一些groovy列表? 谢谢,或者。

HashSet存储相等的对象

下面是从对象列表中查找重复对象的代码。 但由于某种原因,hashset甚至存储了相等的对象。 我当然错过了一些东西,但当我检查hashset的大小时,它出来了5。 import java.util.ArrayList; import java.util.HashSet; public class DuplicateTest { public static void main(String args[]){ ArrayList dogList = new ArrayList(); ArrayList duplicatesList = new ArrayList(); HashSet uniqueSet = new HashSet(); Dog a = new Dog(); Dog b = new Dog(); Dog c = new Dog(); Dog d = new Dog(); Dog e = new […]

当值的hashset为Empty时,删除hashmap中的键

我有一个将字符串键映射到hashsets值的hashmap,我想在hashmaps的hashset值为空时从hashmap中删除一个键。 我无法接近这个。 这是我尝试过的但是我很困惑: for(Map.Entry<String, HashSet> entr : stringIDMap.entrySet()) { String key = entr.getKey(); if (stringIDMap.get(key).isEmpty()) { stringIDMap.remove(key); continue; } //few print statements… }

HashSet包含()方法

我执行下面的代码,发现输出是false 。 import java.util.Set; import java.util.HashSet; public class Name { private String first, last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Object o) { if (!(o instanceof Name)) return false; Name n = (Name) o; return n.first.equals(first) && n.last.equals(last); } public static void main(String[] args) { Set […]

当您知道HashSet中最大可能的元素数时,应使用什么负载因子

当我真正知道HashSet中最大可能的元素数时,我应该使用什么负载因子? 我听说建议使用0.75的默认负载系数,因为它在速度和空间之间提供了良好的性能折衷。 它是否正确 ? 但是,更大的HashSet也会在创建和更多空间上花费更多时间。 我正在使用HashSet,以便从整数列表中删除重复的整数。

在Java中将数据从HashSet移动到ArrayList

我在Java中有以下Set : Set< Set > SetTemp = new HashSet< Set >(); 我想将其数据移动到ArrayList : ArrayList< ArrayList > List = new ArrayList< ArrayList >); 有可能吗?

HashSet与ArrayList速度? 插入vs查找(Java)

看看这个问题 ,我很好奇使用哪个,Hashset vs ArrayList。 Hashset似乎有更好的查找,ArrayList有更好的插入(对于许多对象)。 所以我的问题是,因为我无法使用ArrayList插入,然后使用HashSet搜索它,我将不得不选择其中一个。 使用ArrayList插入,转换为HashSet进行查找,总体上是SLOWER还是只是插入HashSet然后查找? 或者只是坚持使用ArrayList,虽然查找更糟糕,插入可以弥补吗?

如何在多个列表中查找公共元素?

我有一个列表列表(嵌套列表)。 我需要找到它们之间的共同元素。 Example would be [1,3,5], [1,6,7,9,3], [1,3,10,11] 应该导致[1,3] 如果没有使用HashSet的retainAll方法,如何迭代所有要查找的元素? 谢谢,

HashSet 的初始容量

我应该为HashSet使用什么初始容量,我知道我将插入1000个整数以防止需要进行任何内部重建? 起初我虽然我应该使用1000但是阅读Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75). initialCapacity参数的构造函数的描述,它表示Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75). Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default […]