简体   繁体   中英

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

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

The error is clear, you are using .count() which return Single<Long> and not Observable<String> instead you have to use :

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

Or if you want to get the count as Long you can use .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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