简体   繁体   English

Rxjava 可观察的不兼容类型

[英]Rxjava observable incompatible types

Im trying to return the number of times a word occurs in a text document using rxjava, i keep getting an error saying我正在尝试使用 rxjava 返回一个单词在文本文档中出现的次数,我不断收到错误消息

incompatible types: Single Long cannot be converted to Observable String 

the function that I'm using is :我正在使用的功能是:

static public Observable<String> WordCount(Observable<String> lineEmitter, String startTerm){
    return lineEmitter.flatMap(str -> Observable.fromArray(str.split("\\s")))
            .filter(str -> str.replaceAll("[^a-zA-Z0-9]", "").contains(startTerm))
            .count();
}

I think it is to do with the count at the end making it a single long but I dont know how to convert it to a string我认为这与最后的计数有关,使其成为一个 long 但我不知道如何将其转换为字符串

The error is clear, you are using .count() which return Single<Long> and not Observable<String> instead you have to use :错误很明显,您正在使用.count()返回Single<Long>而不是Observable<String>而您必须使用:

static public Single<Long> wordCount(Observable<String> lineEmitter, String startTerm){
              ^^^^^^^^^^^

Or if you want to get the count as Long you can use .blockingGet()或者,如果您想将计数设为 Long,您可以使用.blockingGet()

static public Long WordCount(Observable<String> lineEmitter, String startTerm){
    return lineEmitter.flatMap(str -> Observable.fromArray(str.split("\\s")))
            .filter(str -> str.replaceAll("[^a-zA-Z0-9]", "").contains(startTerm))
            .count()
            .blockingGet();
}

Advice: Please don't use UpperCase in first letter of method建议:请不要在方法的第一个字母中使用大写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM