我有以下型号:
{
"account" : {
"accountName": "Complete Access",
"accountNumber": "062005 1709 5888",
"available": 226.76,
"balance": 246.76
},
"transactions": [{
"id": "44e5b2bc484331ea24afd85ecfb212c8",
"effectiveDate": "20/07/2017",
"description": "Kaching TFR from JOHN CITIZEN<br/>xmas donation",
"amount": 12.00
}, {
"id": "1506aeeb8c3a699b1e3c87db03156428",
"effectiveDate": "20/07/2017",
"description": "Wdl ATM CBA ATM CIRCULAR QUAY STATION NSW 221092 AUS",
"amount": -200.00,
"atmId": "129382"
}]
}
我想根据Rxjava上的effectiveDate对它们进行分组,但是很新鲜......你可以帮忙做一下os。非常感谢
我失败的努力:
Observable.fromIterable(transactionsViewModel.getTransactions())
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.groupBy(transaction -> transaction.getEffectiveDate())
.flatMap(grouped -> {
groupedTransactionMap.put(grouped.getKey(),grouped.toList().blockingGet());
return grouped;
})
.subscribe(v -> System.out.println(v));
答案 0 :(得分:0)
你很亲密!这可能对你有所帮助:
Observable.fromIterable(transactionsViewModel.getTransactions())
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.groupBy(transaction -> transaction.getEffectiveDate())
.flatMapSingle(grouped -> grouped.collect(ArrayList::new, List::add))
...
这应该将所有相同的日期分组并将它们放入列表中,然后它将发出每个列表。您应该获得 n 列表,其中 n 是您拥有的不同日期的数量。因此,对于您的示例,您应该只使用两个条目获取一个列表。
编辑:如果你想要一个映射,你可以按如下方式继续转换流:
Observable.fromIterable(transactionsViewModel.getTransactions())
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.groupBy(transaction -> transaction.getEffectiveDate())
.flatMapSingle(grouped -> grouped.collect((Callable<ArrayList<Transaction>>) ArrayList::new, ArrayList::add))
.toMap(transactions -> transactions.get(0).getEffectiveDate())
...
但是,这将成为Single<Map<Date,Transaction>>
而不是Observable
。