본문 바로가기

Language93

비 구조화 할당 ■ 배열 비 구조화 할당 let arr = ["one", "two", "three"]; const [a, b, c, d = "four"] = arr; // d 는 four 로 기본값 지정 console.log(b); // two console.log(d); // four 배열 비 구조화 할당에서는 인덱스가 기준이 된다. 인덱스 기준이므로 a = one, b = two, c = three 의 값이 할당 된다. ■ 배열 비 구조화 할당 응용 let a = 1; let b = 2; [a, b] = [b, a]; console.log(a, b); // 2 1 ■ 객체 비 구조화 할당 객체 비 구조화 할당에서는 키값이 기준이 된다. 키값이 없는 경우에는 기본값을 설정할 수 있다. let object = { ONE.. 2023. 3. 9.
스트림 ■ 스트림 예제 import java.util.Arrays; import java.util.stream.IntStream; public class MyFirstStream { public static void main(String[] args) { int[] ar = { 1, 2, 3, 4, 5 }; // 스트림 생성 IntStream stm1 = Arrays.stream(ar); // 중간 파이프 구성 IntStream stm2 = stm1.filter((n) -> n % 2 == 1); // 최종 파이프 구성 int sum = stm2.sum(); System.out.println(sum); } } ar 에 저장된 데이터를 대상으로 스트림 생성하였고 그 스트림을 stm1 이 참조 stm1 이 참조하는.. 2023. 3. 8.
조건문 업그레이드 ■ 배열 내장 함수를 활용한 조건문 업그레이드 function isCountry(country) { if (country === "USA" || country === "UK") { return true; } else { return false; } } const a = isCountry("USA"); const b = isCountry("Korea"); console.log(a); // true console.log(b); // false 위 조건문을 다음과 같이 변환할 수 있다. function isCountry(country) { if (["USA", "UK"].includes(country)) { return true; } else { return false; } } ■ 객체 괄호 표기법을 활용한 .. 2023. 3. 7.
Optional 클래스 ■ Optional 클래스 인스턴스 생성 import java.util.Optional; public class StringOptional1 { public static void main(String[] args) { Optional os1 = Optional.of(new String("Toy1")); Optional os2 = Optional.ofNullable(new String("Toy2")); if (os1.isPresent()) { System.out.println(os1.get()); } if (os2.isPresent()) { System.out.println(os2.get()); } } } 위 예제를 통해 Optional 인스턴스의 생성 방법 두가지를 볼 수 있다. Optional os1 .. 2023. 3. 7.