Below piece of code is using asIterator() method from Java 9 can someone please help me to convert below code compatible with Java 8?
private static Function<ClassLoader, Stream<URL>> asURLs(String packageDir) {
return cl -> {
try {
Iterator<URL> iterator = cl.getResources(packageDir).asIterator();
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
} catch (IOException e) {
throw new UnhandledIOException(e);
}
};
To begin with, look up "jdk source code" in your favourite search engine to find the source of
asIterator, so that you can copy it. You'll find it here:https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/Enumeration.java
There you'll find
Enumeration.asIterator(), which is an interface default method. You can define it locally as a method that takes an Enumeration argument:and then use it like this:
However, @tevemadar commented with a link to an answer to a related question that might suit you better