From this post https://codereview.stackexchange.com/questions/7422/compare-items-in-two-lists
i found many people give many solution to compare two list<T>. i have few question about their code.
Which approach perform best when two list will have large data ?
1st approach
internal bool DoIdsMatchThoseFromXml(List<string> ids, List<string> xmlIds)
{
return !ids.Except(xmlIds).Any(); // A - B = {}
}
i understand this approach how it is working and comparing two list.
2nd approach
internal bool DoIdsMatchThoseFromXml(List<string> ids, List<string> xmlIds)
{
return ids.Count == xmlIds.Count // assumes unique values in each list
&& new HashSet<string>(ids).SetEquals(xmlIds);
}
just do not understand this line new HashSet<string>(ids).SetEquals(xmlIds);
what kind of action SetEquals() is doing here?
3rd Approach
internal bool DoIdsMatchThoseFromXml(List<string> Ids, List<string> XmlIds)
{
return
Ids.Count == XmlIds.Count &&
Ids.All(XmlIds.Contains) &&
XmlIds.All(Ids.Contains);
}
this checking Ids.All(XmlIds.Contains) would not be sufficient ? why they are checking
Ids.All(XmlIds.Contains) and XmlIds.All(Ids.Contains); both any idea ?
4th Approach
internal static bool DoIdsMatchThoseFromXml(List<string> Ids, List<string> XmlIds)
{
return Ids.TrueForAll(XmlIds.Contains) && XmlIds.TrueForAll(Ids.Contains);
}
what TrueForAll() function does ?
5th Approach
internal bool DoIdsMatchThoseFromXml(List<string> Ids, List<string> XmlIds)
{
Ids.Sort();
XmlIds.Sort();
return Ids.SequenceEqual(XmlIds);
}
what SequenceEqual() does here ?
6th Approach
internal bool DoIdsMatchThoseFromXml(List<string> Ids, List<string> XmlIds) {
if (Ids.Count != XmlIds.Count) return false;
HashSet<string> xmlIds = new HashSet<string>(XmlIds);
return Ids.All(id => xmlIds.Contains(id));
}
please tell me how below two line is working
HashSet<string> xmlIds = new HashSet<string>(XmlIds);
return Ids.All(id => xmlIds.Contains(id));
Which approach perform best when two list will have large data ?
please explain which help me to understand how various approaches is working. Thanks