Day73 :) Stream, Optional
10086 ワード
Stream API
特長
Collectionとの相違
Stream:内部重複
Collection Collection:外部繰返し[Collection:がいぶはんぷく]
ストリームストリーム
作成
アレイフロー
Arrays.stream(배열);
String[] example = new String[]{1,2,3,4,5};
Stream<String> stream = Arrays.stream(example);
しゅうごうりゅう
List<String> 스트림명 = List.of("값","값","값"..);
List<String> names = List.of("김유신","강감찬","이순신","유관순","홍길동");
// 스트림 생성
Stream<String> stream = names.stream();
くうりゅう
Stream<Object> stream = Stream.empty();
の代わりに
Arrays.stream(배열);
String[] example = new String[]{1,2,3,4,5};
Stream<String> stream = Arrays.stream(example);
List<String> 스트림명 = List.of("값","값","값"..);
List<String> names = List.of("김유신","강감찬","이순신","유관순","홍길동");
// 스트림 생성
Stream<String> stream = names.stream();
Stream<Object> stream = Stream.empty();
ちゅうかんえんざん
フィルタリング
filter(Predicate)
IntStream example = IntStream.of(7, 5, 5, 2, 1, 2, 3, 5, 4, 6);
examle.filter(num->num <= 5) // 5,5,2,1,2,3,5,4
distinct()
String[] sample = {"서울","경기","인천","서울","부산","인천","울산"};
sample.distict(); // 서울, 경기, 인천, 부산, 울산
へんかん
map(Function)
IntStream numbers = IntStream.of(1,3,5,7,9);
numbers.map(num -> num*2); // 2,6,10,14,18
flatMap(Function)
// 배열생성
String names = {"김좌진", "이순신", "안중근", "김유신", "유관순"};
// 스트림생성
Arrays.stream(names)
// 스트림 변환
.flatMap(text->Stream.of(text.split(" "))) // 김좌진, 이순신, 안중근, 김유신, 유관순
制限
limit()
IntStream num1 = IntStream.range(0, 10); // 0~9
num.limit(5); // 0,1,2,3,4
skip()
IntStream num2 = IntStream.range(0, 10); // 0~9
num.skip(4); // 4,5,6,7,8,9
ストリームはツールバーの
sorted()
Stream<String> hangeul = Stream.of("마","다","라","가","나");
// 오름차순
hangeul.sorted(); // 가,나,다,라,마
// 내림차순
hangeul.sorted(Comparator.reverseOrder()); // 마,라,다,나,가
さいしゅうえんざん
しゅつりょく
forEach()
Stream<String> textArray = Stream.of("Hello","Hi","java","stream");
textArray.forEach(System.out::prinln);
消耗する
reduce()
Stream<String> countNumber = Stream.of("하나,"둘","셋,"넷");
String countNumber = countNumber.reduce("숫자세보기! ",(s1,s2)->s1+" 다음 "+s2);
// 숫자세보기! 하나 다음 둘 다음 셋 다음 넷
検索
findFirst()
IntStream example = IntStream.of(4, 2, 7, 3, 5, 1, 6);
OptionalInt result1 = stream1.findFirst(); // 4
findAny()
IntStream example = IntStream.of(4, 2, 7, 3, 5, 1, 6);
OptionalInt result1 = stream1.findFirst(); // 4
けんさ
anyMatch()
IntStream numbers = IntStream.of(30, 90, 70, 10);
numbers.anyMatch(n-> n >= 70); //true
allMatch()
IntStream numbers = IntStream.of(30, 90, 70, 10);
numbers.allMatch(n-> n >= 10); // true
numbers.allMatch(n-> n >= 70); // false
noneMatch()
IntStream numbers = IntStream.of(30, 90, 70, 10);
numbers.noneMatch(n-> n >= 70); // false
numbers.noneMatch(n-> n >= 95); // true
統計
count()
IntStream.of(4, 2, 7, 6, 8, 1, 5).count()); // 7
ロングタイプは、min()
IntStream.of(4, 2, 7, 6, 8, 1, 5).min().getAsInt()) // 1
は、max()
IntStream.of(4, 2, 7, 6, 8, 1, 5).max().getAsInt()) // 8
は、えんざん
sum()
IntStream.of(4, 2, 7, 6, 8, 1, 5).sum() // 33
average()
IntStream.of(2,4,6,8,10).average().getAsDouble() // 6.0
収集
collect()
List<Integer> numberList = Stream.of(4, 2, 7, 6, 8, 1, 5).collect(Collectors.toList());
// [4,2,7,6,8,1,5]
Set<Integer> numberSet = Stream.of(4, 2, 7, 6, 8, 1, 5).collect(Collectors.toSet());
// [1,2,4,5,6,7,8]
Optional
パッケージ
作成
empty()
of()
保存
Optional<String> opt1 = Optional.of("가나다");
// 가나다
ofNullable()
Optional<String> opt = Optional.ofNullable("Hello,world");
// Hello,world
けんさ
isPresent()
Optional<String> opt = Optional.ofNullable("테스트");
opt.isPresent() // true
朝礼
get()
Optional<String> opt1 = Optional.of("가나다");
opt1.get() // 가나다
orElse()
Optional<String> opt = Optional.empty();
opt.orElse("비어있습니다");
// 비어있습니다
orElseThrow()
String name = null;
Optional.ofNullable(name).orElseThrow(IllegalArgumentException::new);
Reference
この問題について(Day73 :) Stream, Optional), 我々は、より多くの情報をここで見つけました https://velog.io/@yoonee1126/Day73-Streamテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol