目前,每当我需要从数组创建流时,我都会这样做
String[] array = {"x1", "x2"};
Arrays.asList(array).stream();
Run Code Online (Sandbox Code Playgroud)
是否有一些从数组创建流的直接方法?
是否有可能在发生整数溢出时抛出某种运行时异常,而不是静默失败.例如
int x = 100000000 * 1000000000;
Run Code Online (Sandbox Code Playgroud)
1569325056由于溢出而打印,我想要的是获得某种运行时异常
我max()用来查找列表中的最大值,但下面的代码返回4虽然最大值是90.
List<Integer> list = new ArrayList<>(Arrays.asList(4,12,19,10,90,30,60,17,90));
System.out.println(list.stream().max(Integer::max).get());
Run Code Online (Sandbox Code Playgroud) 我正在将java7代码迁移到Java 8.对于下面在Java 8中发布的代码,可以是等效的(最好是一个衬垫)
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> output = new ArrayList<>(list.size());
for(Integer n : list) {
int x = n * n * n;
output.add(x);
}
Run Code Online (Sandbox Code Playgroud) 尝试编写一个简单的程序,它将在Java8中打印输入数组中的唯一单词.例如,如果输入是
String[] input = {"This", "is", "This", "not"};
Run Code Online (Sandbox Code Playgroud)
程序应输出[T, h, i, s, n, o, t],元素的顺序应遵循它们在输入中出现的相同模式.我的方法是split输入,然后map,distinct最后collect是toList.但是下面的代码是打印流而不是单词列表,我缺少什么?.Eg
String[] input = {"This", "is", "This", "not"};
System.out.println(Arrays.stream(input)
.map(word -> word.split(""))
.map(Arrays::stream)
.distinct()
.collect(toList()));
Run Code Online (Sandbox Code Playgroud)
电流输出
[java.util.stream.ReferencePipeline$Head@548c4f57, java.util.stream.ReferencePipeline$Head@1218025c, java.util.stream.ReferencePipeline$Head@816f27d, java.util.stream.ReferencePipeline$Head@87aac27]
Run Code Online (Sandbox Code Playgroud)
我很想知道Java8中是否还有其他方法可以实现相同的目标.
使用String.join 我得到意想不到的输出.下面的代码应该打印ABC,而是打印BAC
System.out.println(String.join("A", "B", "C"))
Run Code Online (Sandbox Code Playgroud)