Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I type validate values in a map?

I have a Map<String, Object> which I call myMap, and I want to iterate through each key and ensure that its value is either of type String, int & Boolean.

I’ve tried something along these lines:

myMap.entrySet().forEach(key -> myMap.get(key));

I don’t know how to do the actual check…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can check all of myMap.values() with Stream#allMatch.

boolean pass = myMap.values().stream().allMatch(o -> o instanceof String || 
                    o instanceof Integer || o instanceof Boolean);

Note that it is impossible for the value to be of type int; it can only be the wrapper class Integer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading