Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a Class inside a List of Classes with java stream

I'm going to find a class in a list of Classes (List<Class<?>>) with java streams:

Class cls = classList.stream().filter(clazz -> clazz.getName().startsWith("MyName")).findAny().orElse(null);

A class with the name MyNameClass exists in the list, but the value of cls is null at the end. Is there anything wrong in the filter()?

like image 337
DummyBeginner Avatar asked Oct 19 '25 03:10

DummyBeginner


1 Answers

You should use .getSimpleName, .getName returns fully qualified name of the class with package in it. For example, String.class.getName() returns "java.lang.String".

like image 165
DDovzhenko Avatar answered Oct 21 '25 17:10

DDovzhenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!