【发布时间】:2010-12-04 10:21:20
【问题描述】:
如果我有两个字符串类型(或任何其他类型)的列表,那么连接这两个列表的快速方法是什么?
顺序应该保持不变。应删除重复项(尽管两个链接中的每个项目都是唯一的)。我在谷歌搜索时没有找到太多关于此的内容,并且不想实现任何 .NET 接口以加快交付速度。
【问题讨论】:
-
顺序重要吗?是否要保留重复项?
标签: c#
如果我有两个字符串类型(或任何其他类型)的列表,那么连接这两个列表的快速方法是什么?
顺序应该保持不变。应删除重复项(尽管两个链接中的每个项目都是唯一的)。我在谷歌搜索时没有找到太多关于此的内容,并且不想实现任何 .NET 接口以加快交付速度。
【问题讨论】:
标签: c#
【讨论】:
空间开销最小的方式是使用 Concat 扩展方法。
var combined = list1.Concat(list2);
它创建一个IEnumerable<T> 的实例,它将按该顺序枚举list1 和list2 的元素。
【讨论】:
using System.Linq;才能使用Concat
Union 方法可能会满足您的需求。您没有指定顺序或重复是否重要。
采用两个 IEnumerable 并执行联合,如下所示:
int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };
IEnumerable<int> union = ints1.Union(ints2);
// yields { 5, 3, 9, 7, 8, 6, 4, 1, 0 }
【讨论】:
类似这样的:
firstList.AddRange (secondList);
或者,您可以使用 System.Linq 中定义的“Union”扩展方法。 使用'Union',你还可以指定一个比较器,它可以用来指定一个项目是否应该联合。
像这样:
List<int> one = new List<int> { 1, 2, 3, 4, 5 };
List<int> second=new List<int> { 1, 2, 5, 6 };
var result = one.Union (second, new EqComparer ());
foreach( int x in result )
{
Console.WriteLine (x);
}
Console.ReadLine ();
#region IEqualityComparer<int> Members
public class EqComparer : IEqualityComparer<int>
{
public bool Equals( int x, int y )
{
return x == y;
}
public int GetHashCode( int obj )
{
return obj.GetHashCode ();
}
}
#endregion
【讨论】:
targetList = list1.Concat(list2).ToList();
我认为它工作正常。如前所述,Concat 返回一个新序列,在将结果转换为 List 时,它完美地完成了这项工作。使用 AddRange 方法时,隐式转换有时可能会失败。
【讨论】:
如果两个列表中都存在某些项目,您可以使用
var all = list1.Concat(list2).Concat(list3) ... Concat(listN).Distinct().ToList();
【讨论】:
只要是同一类型,用AddRange就很简单了:
list2.AddRange(list1);
【讨论】:
aList.AddRange( anotherList );
【讨论】:
var bigList = new List<int> { 1, 2, 3 }
.Concat(new List<int> { 4, 5, 6 })
.ToList(); /// yields { 1, 2, 3, 4, 5, 6 }
【讨论】:
Concat 更多/更少“联合所有”
List<string> list1 = new List<string>();
list1.Add("dot");
list1.Add("net");
List<string> list2 = new List<string>();
list2.Add("pearls");
list2.Add("!");
var result = list1.Concat(list2);
【讨论】:
一种方式:List.AddRange() 取决于类型?
【讨论】:
一种方式,我没有看到提到它可以更健壮,特别是如果你想以某种方式改变每个元素(例如你想.Trim()所有元素。
List<string> a = new List<string>();
List<string> b = new List<string>();
// ...
b.ForEach(x=>a.Add(x.Trim()));
【讨论】:
看到这个link
public class ProductA
{
public string Name { get; set; }
public int Code { get; set; }
}
public class ProductComparer : IEqualityComparer<ProductA>
{
public bool Equals(ProductA x, ProductA y)
{
//Check whether the objects are the same object.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether the products' properties are equal.
return x != null && y != null && x.Code.Equals(y.Code) && x.Name.Equals(y.Name);
}
public int GetHashCode(ProductA obj)
{
//Get hash code for the Name field if it is not null.
int hashProductName = obj.Name == null ? 0 : obj.Name.GetHashCode();
//Get hash code for the Code field.
int hashProductCode = obj.Code.GetHashCode();
//Calculate the hash code for the product.
return hashProductName ^ hashProductCode;
}
}
ProductA[] store1 = { new ProductA { Name = "apple", Code = 9 },
new ProductA { Name = "orange", Code = 4 } };
ProductA[] store2 = { new ProductA { Name = "apple", Code = 9 },
new ProductA { Name = "lemon", Code = 12 } };
//从两个数组中获取产品 //排除重复。
IEnumerable<ProductA> union =
store1.Union(store2);
foreach (var product in union)
Console.WriteLine(product.Name + " " + product.Code);
/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/
【讨论】:
我使用的两个选项是:
list1.AddRange(list2);
或
list1.Concat(list2);
但是我注意到,当我使用带有递归函数的 AddRange 方法时,它经常调用自身,因为达到了最大维数,所以我得到了 SystemOutOfMemoryException。
(消息 Google 翻译)
数组尺寸超出了支持的范围。
使用Concat 解决了这个问题。
【讨论】:
我只是想测试Union 在引用类型对象的重叠集合上如何与默认比较器一起工作。
我的对象是:
class MyInt
{
public int val;
public override string ToString()
{
return val.ToString();
}
}
我的测试代码是:
MyInt[] myInts1 = new MyInt[10];
MyInt[] myInts2 = new MyInt[10];
int overlapFrom = 4;
Console.WriteLine("overlapFrom: {0}", overlapFrom);
Action<IEnumerable<MyInt>, string> printMyInts = (myInts, myIntsName) => Console.WriteLine("{2} ({0}): {1}", myInts.Count(), string.Join(" ", myInts), myIntsName);
for (int i = 0; i < myInts1.Length; i++)
myInts1[i] = new MyInt { val = i };
printMyInts(myInts1, nameof(myInts1));
int j = 0;
for (; j + overlapFrom < myInts1.Length; j++)
myInts2[j] = myInts1[j + overlapFrom];
for (; j < myInts2.Length; j++)
myInts2[j] = new MyInt { val = j + overlapFrom };
printMyInts(myInts2, nameof(myInts2));
IEnumerable<MyInt> myUnion = myInts1.Union(myInts2);
printMyInts(myUnion, nameof(myUnion));
for (int i = 0; i < myInts2.Length; i++)
myInts2[i].val += 10;
printMyInts(myInts2, nameof(myInts2));
printMyInts(myUnion, nameof(myUnion));
for (int i = 0; i < myInts1.Length; i++)
myInts1[i].val = i;
printMyInts(myInts1, nameof(myInts1));
printMyInts(myUnion, nameof(myUnion));
输出是:
overlapFrom: 4
myInts1 (10): 0 1 2 3 4 5 6 7 8 9
myInts2 (10): 4 5 6 7 8 9 10 11 12 13
myUnion (14): 0 1 2 3 4 5 6 7 8 9 10 11 12 13
myInts2 (10): 14 15 16 17 18 19 20 21 22 23
myUnion (14): 0 1 2 3 14 15 16 17 18 19 20 21 22 23
myInts1 (10): 0 1 2 3 4 5 6 7 8 9
myUnion (14): 0 1 2 3 4 5 6 7 8 9 20 21 22 23
所以,一切正常。
【讨论】: