他们之间有什么区别?我知道
LinkedHashSet是HashSet的有序版本,它维护所有元素的双向链接列表.在关心迭代顺序时,请使用此类而不是HashSet.当您遍历HashSet时,顺序是不可预测的,而LinkedHashSet允许您按照插入顺序迭代元素.
但是在LinkedHashSet的源代码中,只有HashSet的调用构造函数.那么双链接列表和插入顺序在哪里?
我正在探索这种HashSet<T>类型,但我不明白它在收藏中的位置.
可以用它来代替List<T>吗?我认为a的表现HashSet<T>会更好,但我看不到个人对其元素的访问.
它只用于枚举吗?
请考虑以下代码:
HashSet hs = new HashSet();
hs.add("hi"); -- (1)
hs.add("hi"); -- (2)
Run Code Online (Sandbox Code Playgroud)
hs.size()将给出1,因为HashSet不允许重复,因此只存储一个元素.
我想知道我们是否添加了重复元素,然后它是否替换了前一个元素,或者它只是不添加它?
此外,HashMap在同一案件中使用会发生什么?
因此,如果我在迭代时尝试从Java HashSet中删除元素,我会得到一个ConcurrentModificationException.从HashSet中删除元素子集的最佳方法是什么,如下例所示?
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < 10; i++)
set.add(i);
// Throws ConcurrentModificationException
for(Integer element : set)
if(element % 2 == 0)
set.remove(element);
Run Code Online (Sandbox Code Playgroud)
这是一个解决方案,但我认为它不是很优雅:
Set<Integer> set = new HashSet<Integer>();
Collection<Integer> removeCandidates = new LinkedList<Integer>();
for(int i = 0; i < 10; i++)
set.add(i);
for(Integer element : set)
if(element % 2 == 0)
removeCandidates.add(element);
set.removeAll(removeCandidates);
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一个班级IComparable:
public class a : IComparable
{
public int Id { get; set; }
public string Name { get; set; }
public a(int id)
{
this.Id = id;
}
public int CompareTo(object obj)
{
return this.Id.CompareTo(((a)obj).Id);
}
}
Run Code Online (Sandbox Code Playgroud)
当我将这个类的对象列表添加到哈希集时:
a a1 = new a(1);
a a2 = new a(2);
HashSet<a> ha = new HashSet<a>();
ha.add(a1);
ha.add(a2);
ha.add(a1);
Run Code Online (Sandbox Code Playgroud)
一切都很好,ha.count是2的,但:
a a1 = new a(1);
a a2 = new a(2);
HashSet<a> ha = new HashSet<a>();
ha.add(a1); …Run Code Online (Sandbox Code Playgroud) HashSet<T> t = new HashSet<T>();
// add 10 million items
Dictionary<K, V> t = new Dictionary<K, V>();
// add 10 million items.
Run Code Online (Sandbox Code Playgroud)
谁的.Contains方法会更快返回?
只是为了澄清,我的要求是我有1000万个对象(嗯,真的是字符串),我需要检查它们是否存在于数据结构中.我永远不会迭代.
C#中是否有一个集合不允许您向其添加重复项?例如,与愚蠢的类
public class Customer {
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public override int GetHashCode() {
return (FirstName + LastName + Address).GetHashCode();
}
public override bool Equals(object obj) {
Customer C = obj as Customer;
return C != null && String.Equals(this.FirstName, C.FirstName) && String.Equals(this.LastName, C.LastName) && String.Equals(this.Address, C.Address);
}
}
Run Code Online (Sandbox Code Playgroud)
以下代码(显然)将抛出异常:
Customer Adam = new Customer { Address = "A", FirstName = "Adam", LastName = …Run Code Online (Sandbox Code Playgroud) 对于列表,我们使用该Collections.sort(List)方法.如果我们想要排序HashSet怎么办?
我已经读过这个问题,为什么它不可能,但没有找到问题的解决方案.
我想从.NET中检索一个项目HashSet<T>.我正在寻找一种具有此签名的方法:
/// <summary>
/// Determines if this set contains an item equal to <paramref name="item"/>,
/// according to the comparison mechanism that was used when the set was created.
/// The set is not changed. If the set does contain an item equal to
/// <paramref name="item"/>, then the item from the set is returned.
/// </summary>
bool TryGetItem<T>(T item, out T foundItem);
Run Code Online (Sandbox Code Playgroud)
使用这种方法搜索集合的项目将是O(1).从a中检索项目的唯一方法HashSet<T>是枚举所有O(n)项.
除了自己制作HashSet<T>或使用之外,我还没有找到解决这个问题的方法Dictionary<K, V>.还有其他想法吗?
注意:
我不想检查是否HashSet<T> …
我知道从Map的keySet()方法返回的Set不保证任何特定的顺序.
我的问题是,它是否保证多次迭代的相同顺序.例如
Map<K,V> map = getMap();
for( K k : map.keySet() )
{
}
...
for( K k : map.keySet() )
{
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,假设映射不修改,将迭代在按键组处于相同的顺序.使用Sun的jdk15它会以相同的顺序迭代,但在我依赖于这种行为之前,我想知道所有JDK是否都会这样做.
编辑
我从答案中看到我不能依赖它.太糟糕了.我希望不必为了保证我的订购而建立一些新的收藏品.我的代码需要迭代,执行一些逻辑,然后使用相同的顺序再次迭代.我将从keySet创建一个新的ArrayList,这将保证顺序.
hashset ×10
java ×5
c# ×4
.net ×3
hashmap ×3
iteration ×2
set ×2
collections ×1
dictionary ×1
duplicates ×1
performance ×1
sorting ×1