Java 8:展平流<stream>

时间:2016-10-31 09:40:23

标签: java java-8 java-stream

我希望在指定年份的一年内按季度购买笛卡尔积?

年度(2105,2016年)应返回季度(2015年,第1季度),季度(2015年,第2季度)......季度(2016年,第4季度)

表示Quarter的枚举将是

10-30 11:39:56.270: D/dalvikvm(14412): VFY: replacing opcode 0x71 at 0x0116
10-30 11:39:56.270: I/dalvikvm(14412): Could not find method org.apache.commons.lang3.StringUtils.replace, referenced from method com.jdev.tutor.common.DbHelper.synchronizeRatingsWithDb
10-30 11:39:56.270: W/dalvikvm(14412): VFY: unable to resolve static method 63447: Lorg/apache/commons/lang3/StringUtils;.replace (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
10-30 11:39:56.270: D/dalvikvm(14412): VFY: replacing opcode 0x71 at 0x000c
10-30 11:39:56.287: I/PhenotypeFlagCommitter(8050): Experiment Configs successfully retrieved for com.google.android.gms.ocr
10-30 11:39:56.307: D/AndroidRuntime(14412): Shutting down VM
10-30 11:39:56.308: W/dalvikvm(14412): threadid=1: thread exiting with uncaught exception (group=0x41733d40)
10-30 11:39:56.313: E/AndroidRuntime(14412): FATAL EXCEPTION: main
10-30 11:39:56.313: E/AndroidRuntime(14412): Process: com.jdev.tutor, PID: 14412
10-30 11:39:56.313: E/AndroidRuntime(14412): java.lang.NoClassDefFoundError: org.apache.commons.lang3.StringUtils
10-30 11:39:56.313: E/AndroidRuntime(14412):    at com.jdev.tutor.common.Commons.checkStorages(Commons.java:87)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at com.jdev.tutor.SplashScreen.init(SplashScreen.java:117)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at com.jdev.tutor.SplashScreen.onCreate(SplashScreen.java:66)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.app.Activity.performCreate(Activity.java:5248)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.app.ActivityThread.access0(ActivityThread.java:139)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.os.Looper.loop(Looper.java:136)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at android.app.ActivityThread.main(ActivityThread.java:5102)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at java.lang.reflect.Method.invokeNative(Native Method)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at java.lang.reflect.Method.invoke(Method.java:515)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-30 11:39:56.313: E/AndroidRuntime(14412):    at dalvik.system.NativeStart.main(Native Method)

并且我试图想出的代码卡在下面

public enum Quarters {
    Q1, Q2, Q3, Q4
}

以上代码返回IntStream.rangeClosed(this.getYear(), to.getYear()) .boxed() .map(i -> Arrays .stream(Quarters.values()) .map(q -> new Quarter(i, q)) ); ,我需要将其展平为Stream<Stream<Quarter>>任何帮助,我们将不胜感激。

2 个答案:

答案 0 :(得分:4)

您可以使用flatMapStream<Stream<Quarter>>展平为Stream<Quarter>

IntStream.rangeClosed(this.getYear(), to.getYear())
    .boxed()
    .map(i -> Arrays
        .stream(Quarters.values())
        .map(q -> new Quarter(i, q))
        .flatMap(Function.identity())
    );

flatMap的主体只是一个身份功能。

或者,正如Alexis建议的那样,您可以直接使用flatMap代替map

IntStream.rangeClosed(this.getYear(), to.getYear())
    .boxed()
    .flatMap(i -> Arrays
        .stream(Quarters.values())
        .map(q -> new Quarter(i, q))
    );

答案 1 :(得分:1)

flatMap( i -> i)基本上会连接流。