「Java8」Stream.distinct()メソッドで配列(String、Integer型)の重複要素を排除するサンプル
説明
Stream<T> distinct()
このストリームの重複を除いた要素(Object.equals(Object)による)から構成されるストリームを返します。
Javaコード
package com.arkgame.study.it;
import java.util.Arrays;
import java.util.List;
public class SttreamDemo {
// Integer
public static Integer[] cftA = { 11, 22, 33, 33, 11, 66 };
// String
public static String[] cftB = { "A1", "B02", "A1", "D0004" };
// Boolean
public static Boolean[] cftC = { true, false, false };
// Double
public static Double[] cftD = { 12.12, (double) 15, (double) 99, 12.12 };
public static void main(String[] args) {
List<Integer> intLst = Arrays.asList(cftA);
System.out.println("distinctメソッド(Integer型配列の要素):");
intLst.stream().distinct().forEach(System.out::println);
List<String> strLst = Arrays.asList(cftB);
System.out.println("distinctメソッド(String型配列の要素):");
strLst.stream().distinct().forEach(System.out::println);
List<Boolean> boolLst = Arrays.asList(cftC);
System.out.println("distinctメソッド(Boolean型配列の要素):");
boolLst.stream().distinct().forEach(System.out::println);
List<Double> dbLst = Arrays.asList(cftD);
System.out.println("distinctメソッド(Doublen型配列の要素):");
dbLst.stream().distinct().forEach(System.out::println);
}
}
package com.arkgame.study.it;
import java.util.Arrays;
import java.util.List;
public class SttreamDemo {
// Integer
public static Integer[] cftA = { 11, 22, 33, 33, 11, 66 };
// String
public static String[] cftB = { "A1", "B02", "A1", "D0004" };
// Boolean
public static Boolean[] cftC = { true, false, false };
// Double
public static Double[] cftD = { 12.12, (double) 15, (double) 99, 12.12 };
public static void main(String[] args) {
List<Integer> intLst = Arrays.asList(cftA);
System.out.println("distinctメソッド(Integer型配列の要素):");
intLst.stream().distinct().forEach(System.out::println);
List<String> strLst = Arrays.asList(cftB);
System.out.println("distinctメソッド(String型配列の要素):");
strLst.stream().distinct().forEach(System.out::println);
List<Boolean> boolLst = Arrays.asList(cftC);
System.out.println("distinctメソッド(Boolean型配列の要素):");
boolLst.stream().distinct().forEach(System.out::println);
List<Double> dbLst = Arrays.asList(cftD);
System.out.println("distinctメソッド(Doublen型配列の要素):");
dbLst.stream().distinct().forEach(System.out::println);
}
}
package com.arkgame.study.it; import java.util.Arrays; import java.util.List; public class SttreamDemo { // Integer public static Integer[] cftA = { 11, 22, 33, 33, 11, 66 }; // String public static String[] cftB = { "A1", "B02", "A1", "D0004" }; // Boolean public static Boolean[] cftC = { true, false, false }; // Double public static Double[] cftD = { 12.12, (double) 15, (double) 99, 12.12 }; public static void main(String[] args) { List<Integer> intLst = Arrays.asList(cftA); System.out.println("distinctメソッド(Integer型配列の要素):"); intLst.stream().distinct().forEach(System.out::println); List<String> strLst = Arrays.asList(cftB); System.out.println("distinctメソッド(String型配列の要素):"); strLst.stream().distinct().forEach(System.out::println); List<Boolean> boolLst = Arrays.asList(cftC); System.out.println("distinctメソッド(Boolean型配列の要素):"); boolLst.stream().distinct().forEach(System.out::println); List<Double> dbLst = Arrays.asList(cftD); System.out.println("distinctメソッド(Doublen型配列の要素):"); dbLst.stream().distinct().forEach(System.out::println); } }
実行結果
distinctメソッド(Integer型配列の要素):
11
22
33
66
distinctメソッド(String型配列の要素):
A1
B02
D0004
distinctメソッド(Boolean型配列の要素):
true
false
distinctメソッド(Doublen型配列の要素):
12.12
15.0
99.0