Java method: Finding object in array list given a known attribute value

后端 未结 8 1389
名媛妹妹
名媛妹妹 2020-12-16 11:43

I have a couple of questions actually.

I have a class Dog with the following instance fields:

private int id;
private int id_mother;         


        
8条回答
  •  温柔的废话
    2020-12-16 12:14

    I solved this using java 8 lambdas

    int dogId = 2;
    
    return dogList.stream().filter(dog-> dogId == dog.getId()).collect(Collectors.toList()).get(0);
    

提交回复
热议问题