C# 我的哈希表值应该包含键吗
我在反复辩论这个问题。我想要一个Hashtable(或dictionary…这两个功能似乎都很好) 我有一个类项,它有ItemNumber、Description和Quantity。我有一个类ItemCollection(它当前使用一个项数组),我想使用哈希表或字典。在我的Item类中,我使用ItemNumber来比较ItemA==ItemB 因此,我应该将ItemNumber作为哈希表的键向上移动到ItemCollection,还是干脆不使用它。我的一部分人想离开它,因为我基于项生成了一个表。。但我的另一部分认为这是多余和愚蠢的 我的最终目标是通过这样做,看看一个ItemCollection(发货的项目)是否等于另一个ItemCollection(订单)C# 我的哈希表值应该包含键吗,c#,hashtable,C#,Hashtable,我在反复辩论这个问题。我想要一个Hashtable(或dictionary…这两个功能似乎都很好) 我有一个类项,它有ItemNumber、Description和Quantity。我有一个类ItemCollection(它当前使用一个项数组),我想使用哈希表或字典。在我的Item类中,我使用ItemNumber来比较ItemA==ItemB 因此,我应该将ItemNumber作为哈希表的键向上移动到ItemCollection,还是干脆不使用它。我的一部分人想离开它,因为我基于项生成了一个表。
foreach(var package in shipments)
foreach(var item in package)
shipOrder += item;
return mainOrder==shipOrder;
我希望这是有道理的。由于生病,我的头仍然有点模糊,所以如果我需要进一步澄清,请让我知道
编辑
好吧,我想了想大家都说了些什么,试了试,除了一次,大多数测试都通过了,我不知道为什么。所以我会把我的东西贴出来,看看你们能不能找出我愚蠢的错误
public class Item
{
public Item(object[] array)
{
ItemNumber = array[0].ToString();
Description = array[1].ToString();
Quantity = (int)array[2];
}
public Item(string item, string desc, int qty)
{
ItemNumber = item;
Description = desc;
Quantity = qty;
}
/// <summary>
/// </summary>
public string ItemNumber;
public string Description;
public int Quantity;
public override bool Equals(object obj)
{
//return compareTwoItems(this, (Item)obj);
return this.GetHashCode() == obj.GetHashCode();
}
public static bool operator ==(Item ic1, Item ic2)
{
return compareTwoItems(ic1, ic2);
}
public static bool operator !=(Item ic1, Item ic2)
{
return !compareTwoItems(ic1, ic2);
}
private static bool compareTwoItems(Item ic1, Item ic2)
{
return (ic1.ItemNumber == ic2.ItemNumber)
&& (ic1.Quantity == ic2.Quantity);
}
public override int GetHashCode()
{
return ItemNumber.GetHashCode() ^ Quantity;
}
}
public class ItemCollection : System.Collections.Generic.SortedDictionary<string, Item>
{
public ItemCollection()
{
}
public void AddItem(Item i)
{
this.Add(i.ItemNumber, i);
}
public string TrackNumber = "";
/// <summary>
/// Check to see if the Two ItemCollections have the same Quantity of items. If not it may be that the order was not completed
/// </summary>
/// <param name="obj">the sales order items</param>
/// <returns>True if the quantity of the two collections are the same.</returns>
/// <exception cref="ArgumentException">If the collections have different counts, or if ItemNumbers differ in one of the elements</exception>
public override bool Equals(object obj)
{
return this.GetHashCode() == ((ItemCollection)obj).GetHashCode();
}
public override int GetHashCode()
{
int hash = 0;
foreach (var item in this.Values)
hash ^= item.GetHashCode();
return hash;
}
/// <summary>
/// Check to see if the Two ItemCollections have the same Quantity of items. If not it may be that the order was not completed
/// </summary>
/// <param name="ic1">the sales order items</param>
/// <param name="ic2">the shipping ticket items</param>
/// <returns>True if the quantity of the two collections are the same.</returns>
/// <exception cref="ArgumentException">If the collections have different counts, or if ItemNumbers differ in one of the elements</exception>
public static bool operator ==(ItemCollection ic1, ItemCollection ic2)
{
return ic1.Equals(ic2);
}
public static bool operator !=(ItemCollection ic1, ItemCollection ic2)
{
return !ic1.Equals(ic2);
}
}
。。。。现在由于某种原因它起作用了。。。我在Item中的GetHash方法的代码中更改了一件事(忘记用ItemNumber中的hash对数量进行异或),现在它们传递了。。。奇怪的但是我还是会发布我的代码,因为它可能会帮助别人。您可以使用
哈希表作为集合,并在Item类中覆盖基本对象Equals()
和GetHashCode()
方法
我同意scarle88,我认为您的项目集合
应该扩展字典
或哈希表
。字典需要为每个项目提供一个唯一的键,您可以使用项目编号(如果您的要求是每个项目都是唯一的)
像这样:
public class Item {
public string ItemNumber { get; set; }
public string Description { get; set; }
public string Quantity { get; set; }
public override bool Equals(Object o) {
// Implement your Equals here
}
public override int GetHashCode() {
// Implement your hash code method here
}
}
public class ItemCollection : Dictionary<string, Item> {
public override bool Equals(Object o) {
// Implement your collection's Equals method here
}
}
我已经这么做了,但有一点需要注意,我的商品编号不是一个数字,而是一个字符串。(我不喜欢它,但它超出了我的控制)我应该将字符串放入字节数组并对字节数组进行异或吗?在Item类GetHashCode override中,您可以简单地返回ItemNumber的hashcode,例如:return _ItemNumber.GetHashCode();我不想成为一个混蛋,但你的答案并没有显示我是否应该在我的Item类中保留ItemNumber。其他部分是有意义的,并且碰巧回答了我的另一个挥之不去的问题。我会将ItemNumber保留在Item类中,因为它是Item的一部分。另一个想法是,您的项集合可以扩展HashTable(比方说),使其行为类似于HashTable,但您可以重写==运算符来判断两个项集合是否相同。(考虑到你问题中的信息,我想这是我能提供的最多的了)非常感谢你举这个例子。让我告诉你我有什么,因为一个奇怪的原因,我的单元测试失败了,我很高兴它开始工作了。我不能告诉你它以前失败的原因,看起来这取决于你从数据库中获得的数据。是的,它说的是Expected是ItemCollection和Found ItemCollection。。我开始发疯了,因为它不起作用。现在是。
public class Item {
public string ItemNumber { get; set; }
public string Description { get; set; }
public string Quantity { get; set; }
public override bool Equals(Object o) {
// Implement your Equals here
}
public override int GetHashCode() {
// Implement your hash code method here
}
}
public class ItemCollection : Dictionary<string, Item> {
public override bool Equals(Object o) {
// Implement your collection's Equals method here
}
}
public override bool Equals(Object o) {
if (o == null)
return false;
if (!(o is ItemCollection))
return false;
var other = o as ItemCollection;
// check the 'this' collection
foreach (var item in this.Keys) {
if (item != null && !other.ContainsKey(item.ItemNumber))
return false;
// check the 'other' collection
foreach (var item in other.Keys) {
if (item != null && !this.ContainsKey(item.ItemNumber))
return false;
return true;
}