简体   繁体   中英

How to check if enum value exists in a list of objects

I have enum and I have no trouble iterating over its values like this:

for(catType ct : catType.values()) {
        if(ct.toString().equals()))
}

I have List of objects where every object has(or does not have) a property matching the values from the enum.

List<Zoo> zoos = zooRepository.findAll();

To get a String I need from first index of zoos I must

zoos.get(0).getAnimal();

How can I iterate over all catTypes so I can check if they exist in any object from the List of objects, so if they don't exist there I can further add them to the database?

If using java8, it can be done via lambda expression

for(catType ct : catType.values()) {
     boolean exist = zoos.stream()
        .anyMatch(zoo -> zoo.getAnimal().equals(ct.toString()));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM