我想比较两个集合(在C#中),但我不确定有效实现它的最佳方法.
我已经阅读了关于Enumerable.SequenceEqual的其他帖子,但这并不是我正在寻找的.
在我的情况下,如果它们都包含相同的项目(无论顺序),则两个集合将是相等的.
例:
collection1 = {1, 2, 3, 4};
collection2 = {2, 4, 1, 3};
collection1 == collection2; // true
Run Code Online (Sandbox Code Playgroud)
我通常做的是遍历一个集合中的每个项目,看看它是否存在于另一个集合中,然后循环遍历另一个集合的每个项目,看它是否存在于第一个集合中.(我首先比较长度).
if (collection1.Count != collection2.Count)
return false; // the collections are not equal
foreach (Item item in collection1)
{
if (!collection2.Contains(item))
return false; // the collections are not equal
}
foreach (Item item in collection2)
{
if (!collection1.Contains(item))
return false; // the collections are not equal
}
return true; // the collections are equal
Run Code Online (Sandbox Code Playgroud)
但是,这并不完全正确,并且它可能不是比较两个集合的最有效方法.
我能想到的一个例子是错误的:
collection1 …Run Code Online (Sandbox Code Playgroud) 假设字典键和值的equals和hash方法正确实现,那么测试两个字典相等的最简洁有效的方法是什么?
在这种情况下,如果它们包含相同的一组键(顺序并不重要),则说两个字典是相等的.对于每个这样的键,它们都同意该值.
这里有一些我提出的方法(可能还有更多):
public bool Compare1<TKey, TValue>(
Dictionary<TKey, TValue> dic1,
Dictionary<TKey,TValue> dic2)
{
return dic1.OrderBy(x => x.Key).
SequenceEqual(dic2.OrderBy(x => x.Key));
}
public bool Compare2<TKey, TValue>(
Dictionary<TKey, TValue> dic1,
Dictionary<TKey, TValue> dic2)
{
return (dic1.Count == dic2.Count &&
dic1.Intersect(dic2).Count().
Equals(dic1.Count));
}
public bool Compare3<TKey, TValue>(
Dictionary<TKey, TValue> dic1,
Dictionary<TKey, TValue> dic2)
{
return (dic1.Intersect(dic2).Count().
Equals(dic1.Union(dic2).Count()));
}
Run Code Online (Sandbox Code Playgroud) 非常简短的问题.我有一个随机排序的大字符串数组(100K +条目),我想找到所需字符串的第一次出现.我有两个解决方案.
从我读到我可以猜到的是'for循环'目前会提供略微更好的性能(但这个余量总是会改变),但我也发现linq版本更具可读性.总的来说哪种方法通常被认为是目前最好的编码实践,为什么?
string matchString = "dsf897sdf78";
int matchIndex = -1;
for(int i=0; i<array.length; i++)
{
if(array[i]==matchString)
{
matchIndex = i;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
要么
int matchIndex = array.Select((r, i) => new { value = r, index = i })
.Where(t => t.value == matchString)
.Select(s => s.index).First();
Run Code Online (Sandbox Code Playgroud) 我希望能够从Byte []转换为Image,反之亦然.
我从这里开始这两种方法:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
Run Code Online (Sandbox Code Playgroud)
他们似乎工作,但如果我这样做:
byte[] pic = GetImageFromDb();
bool result = pic == imageToByteArray(byteArrayToImage(pic));
Run Code Online (Sandbox Code Playgroud)
我明白了result = false!
有没有办法纠正这种方法或一些不同的功能来实现我的目标?
谢谢!
在C#,为什么Equals()方法总是arrays通过比较引用而不是通过比较内容来检查两者之间的相等?
因此,调用Equals()其实现的所有方法(很多)都不能像数组那样工作(它不会比较内容):
示例:
int[] array1 = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] array2 = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
var u = array1.Equals(array1); //true
var v = array1.Equals(array2); //false
var w = Array.Equals(array1, array2); //false
var x = (new List<int[]>(new int[][] { array1 })).Contains(array2); //false
var y = (new int[][] { array1 }).Any(x => x == array2); //false
var z = (new …Run Code Online (Sandbox Code Playgroud) 有没有人知道LINQ使用Join运算符执行的Join算法.
是NestedLoop,Merge还是HashSet?如果支持,有没有办法指定另一个?
关心艾伯特
有必须是一个更好的办法来做到这一点,我敢肯定...
// Simplified code
var a = new List<int>() { 1, 2, 3, 4, 5, 6 };
var b = new List<int>() { 2, 3, 5, 7, 11 };
var z = new List<int>();
for (int i = 0; i < a.Count; i++)
if (b.Contains(a[i]))
z.Add(a[i]);
// (z) contains all of the numbers that are in BOTH (a) and (b), i.e. { 2, 3, 5 }
Run Code Online (Sandbox Code Playgroud)
我不介意使用上述技术,但我想要快速有效的东西(我需要多次比较非常大的列表<>),这似乎都不是!有什么想法吗?
编辑:因为它有所不同 - 我使用的是.NET 4.0,初始数组已经排序并且不包含重复项.
我试图返回一个代表两个数组之间相似性的数字.
即:
Array1: {Katy, Jenny, Sarah, Ben, Jill, Tina}
Array2: {Katy, John, Sam, Ben, Jill, Linda}
Run Code Online (Sandbox Code Playgroud)
我想返回数字3,因为三个比较是正确的.这可能吗?我想不出有任何能为我做这件事的功能.
c# ×6
arrays ×3
equality ×3
.net ×2
linq ×2
bytearray ×1
collections ×1
compare ×1
comparison ×1
dictionary ×1
image ×1
join ×1
loops ×1
nested ×1
performance ×1
reference ×1