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

前端 未结 6 2102
遥遥无期
遥遥无期 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:24

       public boolean containsId(List list, long id) {
        boolean flag = false;
        for (HasId object : list) {
            if (object.getId() == id) {
               flag = true;
            }
        }
        return flag;
    }
    

提交回复
热议问题