Optional
Optional<String> empty = Optional.empty();
System.out.println(empty.isPresent()); // false
Optional<String> empty2 = Optional.of("assertNull");
System.out.println(empty2.isPresent()); // true
Optional.of(null); // 에려남
Optional<String> empty3 = Optional.ofNullable(null);
System.out.println(empty3.isPresent()); // false
- ifPresent() : Optional 에서 꺼낸 객체가 존재한다면, 구문 수행
String name = null;
Optional<String> opt = Optional.ofNullable(name);
opt.ifPresent(n -> System.out.println(n.length)); // 에러남
- orElse() : Optional에서 꺼낸 객체가 존재한다면 꺼내고, 그렇지 않다면? orElse의 인자값을 반환
System.out.println(Optional.ofNullable(null).orElse("냐옹")); // 냐옹
System.out.println(Optional.ofNullable("Hey!").orElse("냐옹")); // Hey!
- orElseGet() : orElse()와 비슷하지만, 인자값으로 람다 표현식의 결과값을 출력
System.out.println(Optional.ofNullable(null).orElseGet(String::new)); // ""
System.out.println(Optional.ofNullable(null).orElseGet(() -> "냐옹")); // 냐옹
System.out.println(Optional.ofNullable("Hey!").orElseGet(() -> "냐옹")); // Hey!
- orElseThrow() : Optional에서 꺼낸 객체가 존재한다면 꺼내고, 그렇지 않다면? Exception 던지기
String nullName = null;
String name = Optional.ofNullable(nullName).orElseThrow(
IllegalArgumentException::new);
Stream
- filter + anyElement / firstElement
List<String> elements = Stream.of("a", "b", "c")
.filter(element -> element.contains("b"))
.collect(Collectors.toList()); // [b]
Optional<String> anyElement = elements.stream().findAny();
System.out.println(anyElement.orElse("암것두 없어")); // b
Optional<String> firstElement = elements.stream().findFirst();
System.out.println(firstElement.orElse("한개두 없어")); //b
- N개의 중개 연산자
Stream<String> onceModifiedStream = Stream.of("abcd", "bbcd", "cbcd");
Stream<String> twiceModifiedStream = onceModifiedStream
.skip(1)
.map(element -> element.substring(0, 3));
System.out.println(twiceModifiedStream); // java.util.stream.ReferencePipeline$3@7cef4e59
System.out.println(twiceModifiedStream.collect(Collectors.toList())); // [bbc, cbc]
- map 활용
class Product {
private int age;
private String name;
public Product(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
}
public class JavaPracApplication {
public static void main(String[] args) {
List<Product> productList = Arrays.asList(
new Product(23, "potatoes"),
new Product(14, "orange"), new Product(13, "lemon"),
new Product(23, "bread"), new Product(13, "sugar")
);
List<String> collectorCollection = productList.stream()
.map(Product::getName)
.collect(Collectors.toList());
System.out.println(collectorCollection); // [potatoes, orange, lemon, bread, sugar]
double averageAge = productList.stream()
.collect(Collectors.averagingInt(Product::getAge));
System.out.println("나이 평균 : " + averageAge); // 나이 평균 : 17.2
int summingAge = productList.stream().mapToInt(Product::getAge).sum();
System.out.println("나이 총합 : " + summingAge); // 나이 총합 : 86
}
}
'내일배움캠프 > TIL' 카테고리의 다른 글
2022.12.7 (0) | 2022.12.07 |
---|---|
2022.12.3 (0) | 2022.12.03 |
2022.11.30 (0) | 2022.11.30 |
2022.11.29 / Optional (1) | 2022.11.29 |
2022.11.28 (2) | 2022.11.28 |