我感觉不到Java 8 Streams map()和mapToObj()方法之间的区别.在两者中我们都可以创建对象并将对象返回到流,所以为什么这些方法存在为两个,而不仅仅是一个.
你能用例子给我解释一下吗?
我找到了以下代码片段:
Function<Integer, Predicate<Integer>> smallerThan = x -> y -> y < x;
List<Integer> l = Arrays.asList(5, 6, 7, 23, 4, 5645,
6, 1223, 44453, 60182, 2836, 23993, 1);
List<Integer> list2 = l.stream()
.filter(smallerThan.apply(l.get(0)))
.collect(Collectors.toList());
System.out.println(list2);
Run Code Online (Sandbox Code Playgroud)
作为输出我收到:
[4, 1]
Run Code Online (Sandbox Code Playgroud)
smallerThan考虑到我们只传递一个参数,本例中的函数如何工作smallerThan.apply(l.get(0))?
New -> Servlet我无法在 IntelliJ IDEA 中使用创建 servlet 。当然是终极版;我已标记src/main/java为源根目录,但仍然无法自动创建它。剩下的唯一选择就是像我一样手动创建它。
我有以下案例。服务通常是从服务器获取数据,并且该数据需要在其他组件中更新。
该组件仅获取一次订阅值,但服务每 2 秒获取一次数据。我测试了一下,服务做得很好。
在我的情况下,如果订阅是在 ngOnInit 或构造函数中,则不会
成分:
import {Component, OnInit} from '@angular/core';
import {TaskService} from "../../services/task.service";
import {Task} from "../../models/task";
@Component({
selector: 'app-tasks',
templateUrl: './tasks.component.html',
styleUrls: ['./tasks.component.css']
})
export class TasksComponent implements OnInit {
tasks: Task[];
constructor(private taskService: TaskService) {
this.taskService.getTasks().subscribe(tasks => { // this is triggered only once, why ?
this.tasks = tasks;
console.log(tasks);
});
}
ngOnInit() {
}
updateTask(task: Task) {
try {
task.completed = !task.completed;
this.taskService.updateTask(task).subscribe();
}
catch (e) {
task.completed = !task.completed;
}
}
} …Run Code Online (Sandbox Code Playgroud) 我即将从字符串创建二维数组:
char[][] chars;
String alphabet = "abcdefghijklmnopqrstuvwxyz";
Run Code Online (Sandbox Code Playgroud)
但是我怎么能用Java 8 Streams做到这一点,因为我不想通过休闲循环来做到这一点?
我正在学习CompletableFuture,在下面的代码片段中,该thenAccept()方法不打印应该的值10,但程序编译无一例外.任何人都可以解释我的问题是什么?
import java.util.concurrent.*;
import java.util.stream.Stream;
public class CompletableFutureTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture.supplyAsync(CompletableFutureTest::counting).thenAccept(System.out::println);
System.out.println("xD");
}
public static int counting() {
Stream.iterate(1, integer -> integer +1).limit(5).forEach(System.out::println);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 10;
}
}
Run Code Online (Sandbox Code Playgroud) 我有检查CompletableFuture执行时间的方法.如果此类CompletableFuture执行的时间超过2秒,我想要终止此任务.但是,如果我没有执行CompletableFuture方法的控制线程,我该怎么办呢?
final CompletableFuture<List<List<Student>>> responseFuture = new CompletableFuture<>();
responseFuture.supplyAsync(this::createAllRandomGroups)
.thenAccept(this::printGroups)
.exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});
Run Code Online (Sandbox Code Playgroud)
createAllRandomGroups()
private List<List<Student>> createAllRandomGroups() {
System.out.println("XD");
List<Student> allStudents = ClassGroupUtils.getActiveUsers();
Controller controller = Controller.getInstance();
List<List<Student>> groups = new ArrayList<>();
int groupSize = Integer.valueOf(controller.getGroupSizeComboBox().getSelectionModel().getSelectedItem());
int numberOfGroupsToGenerate = allStudents.size() / groupSize;
int studentWithoutGroup = allStudents.size() % groupSize;
if (studentWithoutGroup != 0) groups.add(this.getListOfStudentsWithoutGroup(allStudents, groupSize));
for(int i = 0; i < numberOfGroupsToGenerate; i++) {
boolean isGroupCreated = false;
while (!isGroupCreated){
Collections.shuffle(allStudents);
List<Student> newGroup = this.createNewRandomGroupOfStudents(allStudents, groupSize);
groups.add(newGroup);
if …Run Code Online (Sandbox Code Playgroud) java ×6
java-8 ×4
java-stream ×3
angular ×1
angular6 ×1
arrays ×1
chars ×1
concurrency ×1
future ×1
lambda ×1
servlets ×1
typescript ×1