一些代码:
public class Player {
Team team;
String name;
}
public class Team {
List<Player> players;
}
public class Demo {
@Inject
TeamDAO teamDAO;
@Inject
PlayerDAO playerDAO;
List<String> findTeamMatesNames(String playerName) {
Optional<Player> player = Optional.ofNullable(playerDAO.get(playerName));
return player.flatMap(p -> teamDAO.findPlayers(p.team))
.map(p -> p.name)
.orElse(Collections.emptyList());
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样做?在flatMap方法中我收到错误"类型不匹配:无法从列表转换为可选"
我的目标是:
如果存在可选项,我想根据此可选对象属性获取项目列表
如果不存在可选项,我想返回空列表
这是这个问题的后续行动:
在那里我使用以下代码将Records 数组转换为s数组Person:
let records = // load file from bundle
let persons = records.flatMap(Person.init)
Run Code Online (Sandbox Code Playgroud)
由于此转换可能需要一些时间来处理大文件,因此我希望监控索引以提供进度指示器.
这种flatMap结构有可能吗?我想到的一种可能性是在init函数中发送通知,但我在想从内部计算记录也是可能的flatMap?
我正在使用自己的API,我希望使用RxJava链接一些分页结果.我使用基于游标的分页.(想象有50个用户):
{
"data":{
"status":"ok",
"total":988, //users total
"has_next_page":true,
"end_cursor":"AQAxd8QPGHum7LSDz8DnwIh7yHJDM22nEjd",
"users":[{"id":"91273813",
"username":"codergirl",
"full_name":"Code Girl",
"picture_url":"https://cdn.com/21603182_7904715668509949952_n.jpg",
},
...
]
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在使用改造这样的前50个结果:
public class DataResponse {
@SerializedName("end_cursor")
private String end_cursor;
@SerializedName("users")
private JsonArray users;
@SerializedName("has_next_page")
private Boolean has_next_page;
public boolean hasNextCursor(){
return has_next_page;
}
public String endCursor(){
if (hasNextCursor()){
return end_cursor;
}
return "";
}
public JsonArray getUsers(){
return users;
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
public interface MyService {
@GET( "/users")
Observable<DataResponse> getUsers(
@Query("cursor") String cursor,
);
}
Run Code Online (Sandbox Code Playgroud)
和
MyService service = RetrofitClient.getInstance();
service.getUsers() …Run Code Online (Sandbox Code Playgroud) 我试图将我的String []流展平为String Stream
例如
{ "A", "B", "C" }, {"D, "E" } to "A", "B", "C", "D", "E"
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的代码:
Files.lines(Paths.get(file)).map(a -> a.split(" "));
Run Code Online (Sandbox Code Playgroud)
Files.lines(path)返回Stream[String]并用""分隔每个字符串以得到所有单词的数组(现在这样Stream<String[]>.
我想将每个单词数组拼成单个元素,Stream[String]而不是Stream<String[]>
当我使用flatMap而不是map时,我收到一个错误: Type mismatch: cannot convert from String[] to Stream<? extends Object>
我以为flatMap是用于此目的?什么是完成我想要做的最好的方法
教授提问:
使用流:编写一个方法,根据长度对文件中的单词进行分类:
public static Map<Integer,List<String>> wordsByLength(String file)
throws IOException {
// COMPLETE THIS METHOD
}
Run Code Online (Sandbox Code Playgroud) 当我flatMap与String类型数组一起使用时,它没有给出任何警告,而在Int类型数组的情况下它给出了警告。为什么?例:
let strings = [
"I'm excited about #SwiftUI",
"#Combine looks cool too",
"This year's #WWDC was amazing"
]
strings.flatMap{$0 + "."} //No warning
let ints = [
2,3,4
]
ints.flatMap{$0 + 1} //'flatMap' is deprecated: Please use compactMap(_:) for the case where closure returns an optional value
Run Code Online (Sandbox Code Playgroud) 我有一个嵌套对象,我想将其展平/映射为单层的、类似表格的对象。
[{
a: 1,
b: 2,
c: [{
x: 10,
y: 20
}, {
x: 30,
y: 40
}]
}, {
a: 3,
b: 4,
c: [{
x: 50,
y: 60
}, {
x: 70,
y: 80
}]
}]
Run Code Online (Sandbox Code Playgroud)
由此,我想得到这样的东西:
[{
a: 1,
b: 2,
x: 10,
y: 20
}, {
a: 1,
b: 2,
x: 30,
y: 40
}, {
a: 3,
b: 4,
x: 50,
y: 60
}, {
a: 3,
b: 4,
x: 70,
y: 80
}] …Run Code Online (Sandbox Code Playgroud) 我们考虑一下我有以下课程:
class A {
int i, j, k;
public A(int i, int j, int k) {
this.i = i; this.j = j; this.k = k;
}
}
Run Code Online (Sandbox Code Playgroud)
其中i,j,k具有已知的范围:r_i,r_j,r_k.现在我想生成A此范围内的所有可能实例.我可以想出类似的东西:
Stream.iterate(0, n -> ++n).limit(r_i)
.flatMap(i -> Stream.iterate(0, n -> ++n).limit(r_j)
.flatMap(j -> Stream.iterate(0, n -> ++n).limit(r_k)
.map(k -> new A(i, j, k)))).collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
首先,它太冗长了.有没有办法缩短它?特别是我无法找到一个range
上Stream.其次,编译器无法确定返回类型的类型.它认为它
List<Object>不是预期的List<A>.我该如何解决这个问题?
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (flatmap proc seq)
(accumulate append nil (map proc seq)))
Run Code Online (Sandbox Code Playgroud)
以上是来自 SICP 的代码片段,在 Scheme 中。为什么flatmap需要这个程序?flatmap和 和有map什么区别?
我不明白这一行:
Right(1).flatMap(_ => Left(2)).flatMap(_ => Left(3))
Right(1)被传递给.flatMap(_ => Left(2)。它返回Left(2)并传递给.flatMap(_ => Left(3)。它应该已经退货了Left(3)。但是它又回来了Left(2)。
为什么?
另一个例子是 Right(1).flatMap(_ => Right(2)).flatMap(_ => Right(3))
它返回Right(3)(应有的)。
据我了解,它的工作原理如下:
Right(1)传递给.flatMap(_ => Right(2))。它返回Right(2)并传递给.flatMap(_ => Right(3)。最后返回Right(3)
我遇到以下构建错误RxSwift,
func testFlatMap() {
let bag = DisposeBag()
let subject = PublishSubject<String>.init()
subject.flatMap({ (value) -> String in
PublishSubject.just(value)
}).subscribe(
onNext: { value in
print(value)
}
).disposed(by: bag)
subject.on(.next("Test"))
}
Run Code Online (Sandbox Code Playgroud)
实例方法“flatMap”要求“String”符合“ObservableConvertibleType”
我缺少什么?