我想将流转换为不可变列表.以下方法之间的区别是什么?从性能角度来看哪个更好?
collect( Collectors.collectingAndThen(Collectors.toList(), ImmutableList::copyOf));
ImmutableList.copyOf( stream.iterator() );
collect( Collector.of(
ImmutableList.Builder<Path>::new,
ImmutableList.Builder<Path>::add,
(l, r) -> l.addAll(r.build()),
ImmutableList.Builder<Path>::build) );
一些性能或效率的参数,
列表/集合中可能有许多条目.
如果我希望使用".sorted()"自定义比较器的中间操作对集进行排序,该怎么办?
.parallel()到流中该怎么办?我正在运行RxJava并创建一个使用onNext()方法来生成数据的主题.我正在使用Spring.
这是我的设置:
@Component
public class SubjectObserver {
private SerializedSubject<SomeObj, SomeObj> safeSource;
public SubjectObserver() {
safeSource = PublishSubject.<SomeObj>create().toSerialized();
**safeSource.subscribeOn(<my taskthreadExecutor>);**
**safeSource.observeOn(<my taskthreadExecutor>);**
safeSource.subscribe(new Subscriber<AsyncRemoteRequest>() {
@Override
public void onNext(AsyncRemoteRequest asyncRemoteRequest) {
LOGGER.debug("{} invoked.", Thread.currentThread().getName());
doSomething();
}
}
}
public void publish(SomeObj myObj) {
safeSource.onNext(myObj);
}
}
Run Code Online (Sandbox Code Playgroud)
在RxJava流上生成新数据的方式是通过@Autowire private SubjectObserver subjectObserver
然后调用subjectObserver.publish(newDataObjGenerated)
无论我为subscribeOn()&指定什么observeOn():
其中onNext()的实际工作是在实际调用onNext()主题以生成/生成数据的同一线程上完成的.
它是否正确?如果是这样,我错过了什么?我期待在doSomething()不同的线程上完成.
更新
在我的调用类中,如果我改变了调用publish方法的方式,那么当然会为订阅者分配一个新线程来运行.
taskExecutor.execute(() -> subjectObserver.publish(newlyGeneratedObj)); …Run Code Online (Sandbox Code Playgroud)