Finding out if a list of Objects contains something with a specified field value?

前端 未结 6 2105
遥遥无期
遥遥无期 2020-12-03 10:20

I have a list of DTO received from a DB, and they have an ID. I want to ensure that my list contains an object with a specified ID. Apparently creating an object with expect

6条回答
  •  盖世英雄少女心
    2020-12-03 10:30

    This is what I used in my DFS GetUnvisitedNeighbour function.

        public static int GetUnvisitedNeighbour(int v)
    {
        Vertex vertex = VertexList.stream().filter(c -> c.Data == v).findFirst().get();
        int position = VertexList.indexOf(vertex);
        ...
    }
    

    I used to work in C#. Lambda expressions in C# are much easier to work with than they are in Java.

    You can use filter function to add condition for property of element.

    Then use findFirst().get() or findAny.get() according to your logic.

提交回复
热议问题