我正在尝试了解 reduce 方法。如果我使用 reduce 和 stream() 我得到_ab
,如果我使用 reduceparallelStream()
我得到_a_b
. 无论我们使用parallelStream还是stream,reduce的输出不应该是一样的吗?
import java.util.*;
import java.util.stream.*;
class TestParallelStream{
public static void main(String args[]){
List<String> l = Arrays.asList("a","b","c","d");
String join=l.stream()
.peek(TestParallelStream::sleepFor)
.reduce("_",(a,b) -> a.concat(b));
System.out.println(join);
}
public static void sleepFor(String w){
System.out.println("inside thread:"+w);
try{
Thread.currentThread().sleep(5000);
}catch(InterruptedException e){ }
}
}
Run Code Online (Sandbox Code Playgroud) 试图了解super()方法何时被调用.在下面的代码中,Child类有一个带有this()的无参数构造函数,因此编译器无法插入super().那么如何调用父构造函数.
public class Parent
{
public Parent()
{
System.out.println("In parent constructor");
}
}
public class Child extends Parent
{
private int age;
public Child()
{
this(10);
System.out.println("In child constructor with no argument");
}
public Child(int age)
{
this.age = age;
System.out.println("In child constructor with argument");
}
public static void main(String[] args)
{
System.out.println("In main method");
Child child = new Child();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
In main method
In parent constructor
In child constructor with argument
In child constructor with no argument
Run Code Online (Sandbox Code Playgroud) 我试图理解flatMap
:flatMap(x->stream.of(x) )
不平坦流和flatMap(x->x.stream())
工作,并给出所需的结果.有人可以解释两者之间的区别吗?
import java.util.*;
import java.util.stream.*;
class TestFlatMap{
public static void main(String args[]){
List<String> l1 = Arrays.asList("a","b");
List<String> l2 = Arrays.asList("c","d");
Stream.of(l1, l2).flatMap((x)->Stream.of(x)).forEach((x)->System.out.println(x));
Stream.of(l1, l2).flatMap((x)->x.stream()).forEach((x)->System.out.println(x));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
[a, b]
[c, d]
a
b
c
d
Run Code Online (Sandbox Code Playgroud) 我正在学习Wicket,并想知道PropertyModel如何动态检索其数据.
我理解为了使Model动态,我们必须覆盖getObject方法.有人可以解释PropertyModel的内部工作原理吗?
想知道为什么wicket在Web.xml中使用过滤器而不是Servlet.每个请求都必须通过过滤器,是否会影响性能?
java ×3
java-stream ×2
wicket ×2
constructor ×1
flatmap ×1
java-8 ×1
model ×1
reduce ×1
web.xml ×1