Generic Filter for enum

Generic Filter for enum

Following is my class

Following is my class

public final class Test {
enum Animal {DOG,CAT};
enum COLOR {RED,YELLOW};

class Meaningless { String animal,color; } public void filter(List<Meaningless> meaninglesses){ meaninglesses.stream() .filter(meaningless -> { try { Animal.valueOf(meaningless.animal); return true; }catch(Exception e){ return false; } }) .filter(meaningless -> { try { COLOR.valueOf(meaningless.color); return true; }catch(Exception e){ return false; } }) .collect(Collectors.toList()); }

}

The 2 iterations of filter methods essentially filters out the invalid enum types. How can I remove the code duplication from this ? The check should be generic enough so that I dont have to change the isValidEnum when there is a new enum introduced.

Ideally I would like to do something like

 meaninglesses.stream()
            .filter(meaningless -> isValidEnum(meaningless.animal,Animal.class))
            .filter(meaningless -> isValidEnum(meaningless.color,COLOR.class))


java

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

How Java Became The Best Career Option for IT Jobs in 2020?

Join CETPA, India's best institute for Java online training and Certification Course. Enroll now to learn Core to Advanced Java & avail 50% discount on Java Online Course with Certificate.

Java Online Certifications Courses To Level Up Your Career In 2020

Join CETPA, India's best institute for Java online training and Certification Course. Enroll now to learn Core to Advanced Java & avail 50% discount on Java Online Course with Certificate.