Check If List<T> Contains Any of Another List

How to check list A contains any value from list B?

If you didn't care about performance, you could try:

a.Any(item => b.Contains(item))
// or, as in the column using a method group
a.Any(b.Contains)

But I would try this first:

a.Intersect(b).Any()

Check if listt contains any of another list

You could use a nested Any() for this check which is available on any Enumerable:

bool hasMatch = myStrings.Any(x => parameters.Any(y => y.source == x));

Faster performing on larger collections would be to project parameters to source and then use Intersect which internally uses a HashSet<T> so instead of O(n^2) for the first approach (the equivalent of two nested loops) you can do the check in O(n) :

bool hasMatch = parameters.Select(x => x.source)
.Intersect(myStrings)
.Any();

Also as a side comment you should capitalize your class names and property names to conform with the C# style guidelines.

C# Check if listt contains any of another list and return the matching item

I guess this will answer your question

strList1.Where(c => strList2.Any(a => a.Contains(c))).ToList();

how to check that a list contains another list C#

You can do this

ItemList.Where(item => OfferList.Any(offer => offer.ItemID == item.ItemID)).ToList();

You can also do this (may perform faster)

ItemList.Join(OfferList, item => item.ItemID, offer => offer.ItemID, (item, offer) => item).ToList();

How to check if an object of one list contains any object of another list

I think a counter attribute in the Tag class to count how often a Tag occurs in a list of questions is a design error. You don't need such a counter there.

A possibly plausible example: Imagine you have a class Student and a class Course. To keep track of how many students are in a course, there is no need for a counter in the Student class. In the same way a class Tag should only contain the attributes of a tag. That said, you can achieve your goal without the counter with either one of the two following approachs (provided you have java 8 or higher and your Tag class overrides the equals and hashcode methods):

Approach 1 using Streams & Collectors.groupingBy

Map<Tag,Long> map = allQuestions.stream()
.flatMap(q -> q.getTags().stream())
.filter(allTags::contains)
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));

Approach 2 using List.forEach & Map.compute

Map<Tag, Integer> map2 = new HashMap<>();
allQuestions.forEach(q -> {
q.getTags()
.stream()
.filter(allTags::contains)
.forEach(t -> map2.compute(t, (k, v) -> v == null ? 1 : v + 1));
});

Or using a traditional for-loop and if-else block

Map<Tag, Integer> map3 = new HashMap<>();

for (Question q : allQuestions) {
for (Tag t : q.getTags()) {
if (allTags.contains(t)) {
if (map3.containsKey(t)) {
map3.put(t, map3.get(t) + 1);
} else {
map3.put(t, 1);
}
}
}
}

Does .NET have a way to check if List a contains all items in List b?

If you're using .NET 3.5, it's easy:

public class ListHelper<T>
{
public static bool ContainsAllItems(List<T> a, List<T> b)
{
return !b.Except(a).Any();
}
}

This checks whether there are any elements in b which aren't in a - and then inverts the result.

Note that it would be slightly more conventional to make the method generic rather than the class, and there's no reason to require List<T> instead of IEnumerable<T> - so this would probably be preferred:

public static class LinqExtras // Or whatever
{
public static bool ContainsAllItems<T>(this IEnumerable<T> a, IEnumerable<T> b)
{
return !b.Except(a).Any();
}
}

Check if one list contains element from the other

If you just need to test basic equality, this can be done with the basic JDK without modifying the input lists in the one line

!Collections.disjoint(list1, list2);

If you need to test a specific property, that's harder. I would recommend, by default,

list1.stream()
.map(Object1::getProperty)
.anyMatch(
list2.stream()
.map(Object2::getProperty)
.collect(toSet())
::contains)

...which collects the distinct values in list2 and tests each value in list1 for presence.



Related Topics



Leave a reply



Submit