Compare two lists to determine if both lists contain same items using lambda predicates

java 8 compare two lists of objects
java compare two lists of objects and get differences
java 8 compare two lists for equality
how to compare two arraylist of different objects in java
how to compare elements of an arraylist in java 8
compare two lists in java using comparator
java compare two lists of strings
get uncommon elements from two lists java 8

I am trying to return a boolean value if my object list contains all the types in a list of types in either or C#. I am struggling in writing a lambda expression to accomplish this. Can this be done using lambda predicates? I know it can be done easily with a for each loop.

Public Class Widget
    Public wobbly As String
    Public sprocket As String
    Public bearing As String
End Class

Public Sub test()
    Dim wList As New List(Of Widget)
    wList.Add(New Widget() With {.bearing = "xType", .sprocket = "spring", .wobbly = "99"})
    wList.Add(New Widget() With {.bearing = "yType", .sprocket = "sprung", .wobbly = "45"})
    wList.Add(New Widget() With {.bearing = "zType", .sprocket = "straight", .wobbly = "17"})

    Dim typeList As New List(Of String) From {"xType", "yType", "zType"}

    Dim containsAllTypes As Boolean = wList.TrueForAll(Function(a) a.bearing.Equals(typeList.Where(Function(b) b = a.bearing)))
End Sub


public class Widget
    public string wobbly;
    public string sprocket;
    public string bearing;

public void test()
    List<Widget> wList = new List<Widget>();
    wList.Add(new Widget {
        bearing = "xType",
        sprocket = "spring",
        wobbly = "99"
    wList.Add(new Widget {
        bearing = "yType",
        sprocket = "sprung",
        wobbly = "45"
    wList.Add(new Widget {
        bearing = "zType",
        sprocket = "straight",
        wobbly = "17"

    List<string> typeList = new List<string> {

    bool containsAllTypes = wList.TrueForAll(a => a.bearing.Equals(typeList.Where(b => b == a.bearing)));

EDIT, thanks for all the quick answers, I see there are a few ways to do this, and now have a better understanding of what is happening in the expression.

var containsAll = typeList.All(type => 
    wList.Any(widget => widget.bearing.Equals(type)));

Translated, it is true for all types in typeList that any (at least one) widget in the list has that type.

Check if two lists are equal, Use SequenceEqual to check for sequence equality because Equals method checks for reference equality. var a = ints1.SequenceEqual(ints2);. Or if you don't � When using SequenceEqual to compare two lists or arrays ,the items in the two collections should be the same in the same ordinal.For example if the first list contains the value “A” in the first position then the second list should also contain the value “A” in the first position.

Try var containsAllTypes = typeList.All(x => wList.Select(x => x.bearing).Contains(x))

How to Compare List Objects in Java 7 vs. Java 8, One or more elements from one List match(es) the elements of another List. comparing two Lists in Java 7 and checking if any element from List 1 exists in List 2. That Predicate is applied to each element of the stream, and if each We will have a simple model class, Employee , for all our examples:. Compare the two linked lists and return 1 if the lists are equal. Otherwise, return 0. Do NOT print anything to stdout/console. The output is handled by the code in the editor and it is as follows: For each test case, in a new line, print if the two lists are equal, else print .

I believe what you want is the following:

bool containsAllTypes1 = wList.TrueForAll(a => null != typeList.Find(b => b == a.bearing));

You can also use System.Linq as follows:

bool containsAllTypes2 = wList.All(a => typeList.Any(b => b == a.bearing));

Determine If All Elements Are the Same in a Java List, Learn different ways to determine if all elements in a list are equal. First, it's true that for all elements to be equal, they all have to equal the first element. whether all elements of this stream match the provided predicate: Here, we have two solutions that are very much alike, iterating through a list of� i have two lists which contain same property.I want to compare those two lists and return only difference value of property in c# What I have tried: i have gone through google but i didnt find solution

 Dim containsAllTypes As Boolean = wList.All(Function(a) typeList.Any(Function(b) b = a.bearing))

For each value in wList, it checks to see if any values in typeList match the wList bearing value.

Java Stream Compare Two Lists, This post contains multiple examples for collecting stream elements to list As we can see from their Javadoc (here and here) both methods return an arbitrary on stream object that matches the given Predicate and then returns boolean value. In java 8 Comparator can be instantiated using lambda expression. shuffle� Find the intersection of two lists. Find the difference between two lists. Compare two lists. Find data overlap. Compare more than 4 lists. Online Venn diagram for up to four lists. Online Jaccard index calculation. Compare data sets to find their intersection. Find which items are shared by two or more lists.

The shorter is

containsAllTypes = wList.Where(x => typeList.Contains(x.bearing)).Count() == typeList.Count;


containsAllTypes =  wList.Select(x => x.bearing).Except(typeList).Count() == 0;


containsAllTypes =  wList.Select(x => x.bearing).Intersect(typeList).Count() == typeList.Count;

Java Stream Compare Two Lists, After populating both the lists, we simply pass a Stream of Employee objects to the We will iterate through the list of String objects using stream lambda in java 8. We need to define a Predicate instance with the comparison logic and In other words, two lists are defined to be equal if they contain the same elements in � Using traversal in two lists, we can check if there exists one common element at least in them. While traversing two lists if we find one element to be common in them, then we return true. After complete traversal and checking, if no elements are same, then we return false.

Java Stream Compare Two Lists, In mathematics, a predicate is commonly understood to be a boolean-valued Check if a string contains an element from a list of strings [Last Updated: Mar 6, We can filter the string values and as well as objects using the Java 8 Stream filter. was to compare two lists and print out the number of elments that are same. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Java 8 compare two lists and find differences, Using Java 8 lambda's (added April 10, 2019) Java 8 solves this nicely by In this post, we will see how to sort a List of objects using Comparator in Java. The two String arrays are considered equal if both arrays have same length, and which can be applied to a stream object that matches the given Predicate and then� Select from two lists at the same positon. True if XList is unifiable with YList apart a single element at the same position that is unified with X in XList and with Y in YList. A typical use for this predicate is to replace an element, as shown in the example below. All possible substitutions are performed on backtracking.

You are not logged in. Reading 24: Map, Filter, Reduce, In java 8 Comparator can be instantiated using lambda expression. Write a Java program to join two array lists. for streams that don't use the disk or network, such as the Check if a string contains an element from a list of strings [Last Updated: Mar 6, 2016] Java Java Just compare both arrays has the same elements. Compare. Criteria, two lists. Sums, return unique. Extract missing values. Common values 3 lists. Two price lists. Records in two tables. Common records. Update recent values(2) Missing values two cols. Update recent values. Values shared by 2 rngs. Vals not shared 2 rngs. Values shared by 3 rngs. Extract not shared vals. Shared values 2 cols

  • What if wList contains two items with xType? And what problems with your current code? Can you show (in c# pseudo code) what you trying to achive?
  • I am trying to validate that all types exist in the list, not just one.
  • Yes, that was my understanding. For all types in typeList there must be at least one matching widget in wList.
  • Maybe I am not stating it as clearly as I could, but I am wanting to make sure that all items in typeList correspond to an item in wList so that I compare each item in wList to make sure it is also in typeList
  • In this case I think the number of answers suggests a lack of clarity. "if my object list contains all the types in a list of types." If wList contains all of the types in typeList? That was my understanding.
  • Okay, after going over your logic I see what it is doing now, I thought at first it was just checking if any one of the types existed, but I see how it is using the all and then doing the conditional
  • This solution works as well, I think the logic is basically the same using Contains or as in Scott Hannen's answer using Any as long as it it prefixed with the All predicate so checks each one.