2017-07-23 32 views
0

我有一個交易列表。每筆交易都有貨幣和金額信息等等。我想創建一個持股清單,所以貨幣持有當前的金額。我從groupBy()開始並繼續減少。看來我要訂閱之前,我可以做的結果什麼,因爲這給了我一個錯誤:如何在RxJava 2中的groupBy之後獲取列表?

Observable.fromIterable(transactions) 
      .groupBy(Transaction::getCurrency) 
      .flatMap(t -> t.reduce(new Holding(t.getKey()), (holding, transaction) -> holding.addTransaction(transaction.getAmount())) 

它說:「沒有任何類型的實例變量R存在使單符合ObservableSource <延伸R> 」。

在另一方面,如果我試試這個:

Observable.fromIterable(transactions) 
      .groupBy(Transaction::getCurrency) 
      .subscribe((GroupedObservable<String, Transaction> r) -> r.reduce(new Holding(r.getKey()), (holding, transaction) -> holding.addTransaction(transaction.getAmount())) 
        .toObservable() 
        .subscribe(t -> { 
           //t is a single Holding. 
          } 
        )); 

我不能讓一個列表,因爲我已經訂閱了分組流。我可以把它加起來,但我確信有一個更優雅的解決方案,但我無法弄清楚。基於akarnokd的回答

解決方案

Observable.fromIterable(transactions) 
      .groupBy(Transaction::getCurrency) 
      .flatMapSingle(Observable::toList) 
      .map(Holding::new) 
      .toList() 
      .subscribe(holdings -> { 
       whatever(holdings); 
      }); 
+0

嘗試'flatMapSingle'工作。另外,從onNext處理程序中訂閱是一種糟糕的做法,因爲您失去了RxJava的組合屬性。 – akarnokd

+0

@akarnokd做到了。我甚至不需要減少。你會發佈一個答案,或者我應該嗎? – Herrbert74

回答

2

(從我到後評論):

嘗試flatMapSingle在大寫。另外,從onNext處理程序中訂閱是一種糟糕的做法,因爲您失去了RxJava的組合屬性。

0

由於文件說,該reduce功能

applies a function to each item emitted by an Observable, sequentially, and emit the final value.

這是,你得到一個值(實際上爲組中的每個Observable你會得到一個單一的項目)。

獲得清單後,您可以推遲您的reduce操作。你也許可以取代你的第一長subscribe本:

.subscribe(group -> group.toList() 

然後你根據你有組數一些Observable s,各發射預定義的類型的單個List

注:不知道,但也許你可以用flatMap它可以將你GroupedObservableObservable發射項目列表取代第一subscribe

+0

我認爲這種方式我只得到一個交易分組列表,更難以減少,然後我有同樣的問題。第二部分正是我在第一部分所做的,但有這個錯誤信息... – Herrbert74

0

這將在大寫肯定

public Single<Map<Integer, List<Category>>> getSubCategoryListById(List<Category> categoryList) { 

    return Flowable.just(categoryList) 
     .flatMapIterable(new Function<List<Category>, Iterable<Category>>() { 
      @Override public Iterable<Category> apply(List<Category> categories) throws Exception { 
      return categories; 
      } 
     }) 
     .filter(new Predicate<Category>() { 
      @Override public boolean test(Category category) throws Exception { 
      return category.parent_id != 0; 
      } 
     }) 
     .groupBy(new Function<Category, Integer>() { 
      @Override public Integer apply(Category category) throws Exception { 
      return category.category_id; 
      } 
     }) 
     .flatMapSingle(new Function<GroupedFlowable<Integer, Category>, Single<List<Category>>>() { 
      @Override public Single<List<Category>> apply(
       GroupedFlowable<Integer, Category> integerCategoryGroupedFlowable) throws Exception { 
      return integerCategoryGroupedFlowable.toList(); 
      } 
     }) 
     .toMap(new Function<List<Category>, Integer>() { 
      @Override public Integer apply(List<Category> categories) throws Exception { 
      return categories.get(0).category_id; 
      } 
     }); 
    } 
相關問題