「Java8」Stream.map()で配列(Integer、String、Boolean)の要素の値を取得するサンプル

説明
1.<R> Stream<R> map(Function<? super T,? extends R> mapper)
このストリームの要素に指定された関数を適用した結果から構成されるストリームを返します。
2.void forEach(Consumer<? super T> action)
このストリームの各要素に対してアクションを実行します。
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.it;
import java.util.Arrays;
import java.util.List;
public class SttreamDemo {
public static Integer[] cftA = { 11, 22, 33, 44, 55, 66 };
public static String[] cftB = { "A001", "B002", "C003", "D004" };
public static Boolean[] cftC = { true, false, false };
public static void main(String[] args) {
List<Integer> intLst = Arrays.asList(cftA);
System.out.println("結果(Integer型配列の要素):");
intLst.stream().map(n -> n + 2).forEach(System.out::println);
List<String> strLst = Arrays.asList(cftB);
System.out.println("結果(String型配列の要素):");
strLst.stream().map(str -> str.charAt(0)).forEach(System.out::println);
List<Boolean> boolLst = Arrays.asList(cftC);
System.out.println("結果(Boolean型配列の要素):");
boolLst.stream().map(b -> b.booleanValue()).forEach(System.out::println);
}
}
package com.arkgame.study.it; import java.util.Arrays; import java.util.List; public class SttreamDemo { public static Integer[] cftA = { 11, 22, 33, 44, 55, 66 }; public static String[] cftB = { "A001", "B002", "C003", "D004" }; public static Boolean[] cftC = { true, false, false }; public static void main(String[] args) { List<Integer> intLst = Arrays.asList(cftA); System.out.println("結果(Integer型配列の要素):"); intLst.stream().map(n -> n + 2).forEach(System.out::println); List<String> strLst = Arrays.asList(cftB); System.out.println("結果(String型配列の要素):"); strLst.stream().map(str -> str.charAt(0)).forEach(System.out::println); List<Boolean> boolLst = Arrays.asList(cftC); System.out.println("結果(Boolean型配列の要素):"); boolLst.stream().map(b -> b.booleanValue()).forEach(System.out::println); } }
package com.arkgame.study.it;

import java.util.Arrays;
import java.util.List;

public class SttreamDemo {

      public static Integer[] cftA = { 11, 22, 33, 44, 55, 66 };
      public static String[] cftB = { "A001", "B002", "C003", "D004" };
      public static Boolean[] cftC = { true, false, false };

      public static void main(String[] args) {
            List<Integer> intLst = Arrays.asList(cftA);
            System.out.println("結果(Integer型配列の要素):");
            intLst.stream().map(n -> n + 2).forEach(System.out::println);

            List<String> strLst = Arrays.asList(cftB);
            System.out.println("結果(String型配列の要素):");
            strLst.stream().map(str -> str.charAt(0)).forEach(System.out::println);

            List<Boolean> boolLst = Arrays.asList(cftC);
            System.out.println("結果(Boolean型配列の要素):");
            boolLst.stream().map(b -> b.booleanValue()).forEach(System.out::println);
      }

}

実行結果
結果(Integer型配列の要素):
13
24
35
46
57
68
結果(String型配列の要素):
A
B
C
D
結果(Boolean型配列の要素):
true
false
false

Java

Posted by arkgame