小编puv*_*uvi的帖子

java 8 使用并行流和流减少

我正在尝试了解 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)

java parallel-processing reduce java-stream

5
推荐指数
1
解决办法
2317
查看次数

何时在下面的代码中调用super()

试图了解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)

java constructor

4
推荐指数
1
解决办法
77
查看次数

Java flatMap - 有什么区别stream.of()和collection.stream()

我试图理解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)

java java-8 java-stream flatmap

4
推荐指数
2
解决办法
334
查看次数

wicket Property Model如何动态地将数据传递给其组件

我正在学习Wicket,并想知道PropertyModel如何动态检索其数据.

我理解为了使Model动态,我们必须覆盖getObject方法.有人可以解释PropertyModel的内部工作原理吗?

wicket model

2
推荐指数
1
解决办法
161
查看次数

为什么wicket在Web.xml中使用Filter而不是Servlet?

想知道为什么wicket在Web.xml中使用过滤器而不是Servlet.每个请求都必须通过过滤器,是否会影响性能?

wicket web.xml

1
推荐指数
1
解决办法
275
查看次数