Find a Class inside a List of Classes with java stream

DummyBeginner :

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()?

DDovzhenko :

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".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=84641&siteId=1