[英]Converting List to new object Map using Lambda Expression Issue
Java 8 之前:
final List<SimpleTimerTask> simpleTimerTaskList; // suppose a set of SimpleTimerTask in list
final Map<String, SimpleTTExecutorEntry> result = new HashMap<String, SimpleTTExecutorEntry>();
for (SimpleTimerTask stt: simpleTimerTaskList) {
SimpleTTExecutorEntry sttEntry = new SimpleTTExecutorEntry(stt, SimpleTimerTaskStatus.Ready, time);
result.put(stt.getTaskId(), sttEntry);
}
拉姆达:
final Map<String, SimpleTTExecutorEntry> result = simpleTimerTaskList.stream().collect(
Collectors.toMap(
SimpleTimerTask::getTaskId, SimpleTTExecutorEntry ->
new SimpleTTExecutorEntry(SimpleTimerTask, <- error here ->
SimpleTimerTaskStatus.Ready, time)
));
想将 for 循环转换为 Lamdba,但 SimpleTimer 在 SimpleTTExecutorEntry 承包商中发现对象未找到错误,您能帮忙提出建议吗,如何纠正它,谢谢
我只是编了个例子,只管 map() 方法。 你需要使用你的吸气剂,我刚刚公开了“taskId”。 希望这可以帮助:
private void map(){
List<SimpleTimerTask> simpleTimerTaskList = new ArrayList<>();
Map<String, SimpleTTExecutorEntry> result =
simpleTimerTaskList.stream().collect(
Collectors.toMap(task -> task.taskId, task -> new SimpleTTExecutorEntry(task, "Status", "time")));
}
// objects similar to yours with the same kind of wrapping
static class SimpleTTExecutorEntry {
private SimpleTimerTask SimpleTimerTask;
private Object status;
private Object time;
public SimpleTTExecutorEntry(SimpleTimerTask simpleTimerTask, Object status, Object time) {
SimpleTimerTask = simpleTimerTask;
this.status = status;
this.time = time;
}
}
static class SimpleTimerTask {
public String taskId;
}
祝你好运
那个有效。 非常感谢。
但是,我还是不明白,为什么他们必须使用相同的SimpleTimerTask ->
for getTaskId()
和new SimpleTTExecutorEntry(...)
,您能帮忙new SimpleTTExecutorEntry(...)
吗,谢谢
Collectors.toMap(
SimpleTimerTask -> SimpleTimerTask.getTaskId(),
SimpleTimerTask -> new SimpleTTExecutorEntry(SimpleTimerTask, SimpleTimerTaskStatus.Ready, time))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.