我遇到了一些需要大量工作才能解决的番石榴依赖问题。由于第三方库也依赖于 Guava(不幸的是我不得不使用它),我必须将我对 Guava 的依赖从27.0.1-jre一路降级到20.0. 除了下面的代码被破坏之外,似乎没有主要的副作用,因为ImmutableSet.toImmutableSet()它只在 Guava 中引入21.0:
ImmutableSet.toImmutableSet();
Run Code Online (Sandbox Code Playgroud)
完整的代码块是:
cronJobDefinitions = cronJobsRegistry.get()
.stream()
.map(ThrowingFunction.unchecked(clazz -> clazz.newInstance()
.getCronJobDefinition()))
.collect(ImmutableSet.toImmutableSet());
Run Code Online (Sandbox Code Playgroud)
也许有一种简单的方法可以用ImmutableSet.toImmutableSet()JDK 中的替代方法替换?我目前正在使用 JDK8,(但如果在较新的 JDK-s 中有更好的解决方案,我会很想知道)。
You can collect it as a Set using Collectors.toSet. But there are no guarantees on the type of the returned Set. So, you can use Collectors.collectingAndThen to convert the above set into a Google Guava ImmutableSet.
cronJobDefinitions = cronJobsRegistry.get()
.stream()
.map(..)
.collect(Collectors.collectingAndThen(Collectors.toSet(), ImmutableSet::copyOf));
Run Code Online (Sandbox Code Playgroud)