# 请教各位高手一个算法，两个集合对比内容

candypill 2021-08-15 22:35:43

A~SIZE2~15.22~15.22~15.22
B~SIZE1~15.22~15.22~15.22
C~SIZE1~15.22~15.22~15.22

A~SIZE1~15.22~15~15.22
B~SIZE2~15.22~15.22~15.22
C~SIZE2~15.22~15~15.22

A~SIZE1~15.22~15.22~15.22
B~SIZE1~15.22~15.22~15.22
C~SIZE2~15.22~15.22~15.22

...全文
124 点赞 收藏 8

8 条回复

``````class Program
{

static void Main(string[] args)
{

ConcurrentDictionary<MyClass, MyClass> dic = new ConcurrentDictionary<MyClass, MyClass>(new MyClass());
//第一部分数据
List<MyClass> lst1 = new List<MyClass>();

//第2部分数据
List<MyClass> lst2 = new List<MyClass>();

foreach (var myClass in lst1)
{
}

foreach (var myClass in lst2)
{
dic.AddOrUpdate(myClass, p => p, (p1, p2) => myClass);
}

foreach (var keyValuePair in dic)
{
Console.WriteLine(keyValuePair.Value.data);
}

}

class MyClass:IEqualityComparer<MyClass>
{
private string _data;
private string node { get; set; }
private string node3 { get; set; }

private string key => node + node3;

public MyClass()
{

}

public MyClass(string str)
{
this.data = str;
}

public string data
{
get => _data;
set
{
_data = value;
//demo 我就不做啥校验了，你自己保证数据准确
string[] b = _data.Split('~');
node = b[0];
node3 = b[2];
}
}

public bool Equals(MyClass x, MyClass y)
{
return x.node.Equals(y.node) && x.node3.Equals(y.node3);
}

public int GetHashCode(MyClass obj)
{
return obj.key.GetHashCode();
}
}

}

``````

candypill 08-17
@wanghui0380 多谢

xuzuning 08-16

candypill 08-16
@xuzuning 建议和我一块搬个板凳向高手们学习

key：前2个就好。

candypill 08-16
@wanghui0380 两集合没有重复数据，比对条件是说，根据集合1记录对比集合2，比方集合1里是A，15.22，15.22，在2里A，15.22，15，这时就把1里A那行的SIZE2用集合2里A那行的替换成SIZE1生成新的行，A~SIZE1~15.22~15.22

assky124 08-16

candypill 08-16
@assky124 因为实际情况两个集合比较大，每个集合几W条记录，~有100个左右，有没有更高效些的办法 分割再拼接的办法是需要两层循环吧

C#

10.6w+

.NET技术 C#

2021-08-15 22:44

2021-08-15 22:43

2021-08-15 22:35