简体   繁体   中英

Check if a list<StringCollection> contains list<string>

Given a list<StringCollection> how do most efficiently check whether all string are contained in any of the StringCollection s?

Example:

using System;
using System.Collections.Specialized;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        // Create and initializes a new StringCollection.
        StringCollection myCol0 = new StringCollection();
        StringCollection myCol1 = new StringCollection();
        StringCollection myCol2 = new StringCollection();

        StringCollection SearchCol = new StringCollection();

        // Add a range of elements from an array to the end of the StringCollection.
        String[] myArr0 = new String[] { "RED", "car", "boat" };
        myCol0.AddRange( myArr0 );
        
        // Add a range of elements from an array to the end of the StringCollection.
        String[] myArr1 = new String[] { "Blue", "Goku", "Nappa" };
        myCol1.AddRange( myArr1 );
        
        // Add a range of elements from an array to the end of the StringCollection.
        String[] myArr2 = new String[] { "Yellow", "Winter", "Summer" };
        myCol2.AddRange( myArr2 );
        
        // Add a range of elements from an array to the end of the StringCollection.
        String[] myArr3 = new String[] { "Yellow", "Blue", "RED" };
        SearchCol.AddRange( myArr3 );
        
        List<StringCollection> a = new List<StringCollection>();
        a.Add(myCol0);
        a.Add(myCol1);
        a.Add(myCol2);
    }
}

In this case I want to know whether the strings in SearchCol is contained within the stringcollections stored in List<StringCollection> a

In this case I just like to know which of the searchCol strings is not included in the List<StringCollection> a

The only way I see this it possible to do so is via a double for loop? Is there any datastructure that would be more efficient rather than an stringcollection?

Is there any datastructure that would be more efficient rather than an stringcollection

Efficient in what way? Of course you should normally use a IEnumerable<string> (like a string[] vor List<string> ) since StringCollection is not generic.

But you can also use StringCollection , you have to cast each item from object to string:

var allStrings = a.SelectMany(c => c.Cast<string>());
var searchStrings = SearchCol.Cast<string>();
bool allSearchStringsAreContained = searchStrings.All(allStrings.Contains);

as for the "how do most efficiently", this simple approach is efficient, but if you have a large list of strings that you search or huge string lists, you could use a set based approach:

HashSet<string> set = new HashSet<string>(searchStrings);
bool allSearchStringsAreContained = set.IsSubsetOf(allStrings);

Finally, if you want to ignore the case, so treat "RED" and "Red" same:

Approach 1:

bool allSearchStringsAreContained = searchStrings.All(s => allStrings.Contains(s, StringComparer.OrdinalIgnoreCase));

Approach 2:

HashSet<string> set = new HashSet<string>(searchStrings, StringComparer.OrdinalIgnoreCase);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM