java8
-
인터페이스 default 메소드와 static 메소드프로그래밍/Java 2022. 6. 9. 14:53
기본 메소드 (Default Methods) 인터페이스에 메소드 선언이 아니라 구현체를 제공하는 방법 해당 인터페이스를 구현한 클래스를 깨트리지 않고 새 기능을 추가할 수 있다. 기본 메소드는 구현체가 모르게 추가된 기능으로 그만큼 리스크가 있다. 컴파일 에러는 아니지만 구현체에 따라 런타임 에러가 발생할 수 있다. 반드시 문서화 할 것. (@implSpec 자바독 태그 사용) public interface Foo { void printName(); /* * @implSpec * 이 구현체는 getName()으로 가져온 문자열을 대문자로 바꿔 출력한다. */ // getName() 에 null 값이 들어올 수 있음. 문제가 된다면 구현 클래스에서 재정의하자. default void printNameUpp..
-
메소드 레퍼런스프로그래밍/Java 2022. 6. 8. 22:44
람다가 하는 일이 기존 메소드 또는 생성자를 호출하는 거라면, 메소드 레퍼런스를 사용해서 간결하게 표현할 수 있다. 즉, 람다식을 더 간결하게 표현하는 방법이 메소드 레퍼런스 이다. 메소드를 참조하는 방법 메소드 또는 생성자의 매개변수로 람다의 입력값을 받는다. 리턴값 또는 생성한 객체는 람다의 리턴값이다. 아래 예제코드를 살펴보자. import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.function.Supplier; import java.util.function.UnaryOperator; public class App { public static void main(String..
-
자바에서 제공하는 함수형 인터페이스프로그래밍/Java 2022. 6. 7. 23:12
함수형 인터페이스를 따로 정의하지 않고도 자바에서 기본으로 제공하는 함수형 인터페이스가 있다. 이를통해 람다 표현식, 메서드 레퍼런스, 생성자 레퍼런스를 사용해서 구현할 수 있는 함수형 인터페이스를 살펴보자. 자바에서 미리 정의해둔 자주 사용할만한 함수 인터페이스 Function T타입을받아서R타입을리턴하는함수인터페이스 import java.util.function.Function; class Plus10 implements Function { @Override public Integer apply(Integer integer) { return integer + 10; } } public class Foo { public static void main(String[] args) { // 방법 1 Plus..
-
함수형 인터페이스와 람다 표현식 소개프로그래밍/Java 2022. 6. 7. 22:24
함수형 인터페이스 추상 메서드를 하나만 가지고 있는 인터페이스 Single Abstract Method 인터페이스 @FunctionalInterface 애노테이션을 가지고 있는 인터페이스 다른 형태 (예를들어, static 혹은 default)의 메서드가 있더라도 함수형 인터페이스 이며, 추상 메서드가 하나여야 한다. @FunctionalInterface 는 Java 가 제공해주는 애노테이션이며, 함수형 인터페이스 조건위반시 에러를 띄운다. @FunctionalInterface public interface RunSomething { int doIt(int number); //void doIt(); static void printName() { System.out.println("lhk"); } defa..
-
Date와 Time API프로그래밍/Java 2022. 5. 5. 17:15
기존에 사용하던 날짜와 시간 Api 는 아래와같다. Date date = new Date(); Calendar calendar = new Gregoriancalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat(); Java8 에 새로운 날짜와 시간 API 가 생긴 이유 그 전까지 사용하던 java.util.Date 클래스는 mutable 하기 때문에 thread safe 하지 않다. .setTime( ) 시 변경이 가능하며, 멀티 쓰레드 환경에서 안전하게 쓰기 어렵다. 클래스 이름이 명확하지 않다. Date 인데 시간까지 다룬다. 버그 발생할 여지가 많다. 타입 안정성이 없고, 월이 0부터 시작한다거나 등 날짜 시간 처리가 복잡한 애플리케이션에서는..
-
Optional프로그래밍/Java 2022. 5. 5. 09:22
Optional Java8 에 추가된 새로운 인터페이스. 비어있을수도 있고, 값 하나를 담고 있을수도 있는 컨테이너 인스턴스의 타입이다. 즉, 오직 값 한 개가 들어있을 수도 없을 수도 있는 컨테이너. 자바 프로그래밍에서 NullPointerException 을 보는 이유. null 를 리턴하니까! ( null 이 리턴되는 자체가 문제이다. ) null 체크를 깜빡했으니까! 메소드에서 작업 중 특별한 상황에서 값을 제대로 리턴할 수 없는 경우 선택할 수 있는 방법 예외를 던진다. 에러가 발생하게되면 자바는 스택 트레이스를 찍는데, ( 이 에러가 발생하기 전까지의 어떠한 콜 스택을 거쳐서 에러가 발생하게 되었는지에 대한 정보) 이 자체로 리소스를 사용하는거여서 부담이 가기 때문에 필요할 때에만 사용해야지 ..
-
Stream프로그래밍/Java 2022. 5. 4. 22:31
Stream 이란? 연속된 데이터를 처리하는 Operation(조작)의 모임. 컬렉션 이나 배열 등에 저장되어 있는 요소들을 하나씩 참조하며 반복적인 처리를 가능케 하는 기능이다. Stream 을 이용한다면 불필요한 반복문이나 분기처리를 쓰지 않고도 직관적인 코드를 작성할 수 있다. Stream 특징 데이터를 담고 있는 저장소(컬렉션)이 아니다. Functional in nature, 즉 Stream 이 처리하는 데이터 소스를 변경하지 않는다. 무제한일 수도 있다. 실시간으로 계속해서 Stream 으로 들어온 데이터를 Stream 으로 받아 처리할 수 있다. Short Circuit 메소드를 사용해서 제한하는 것도 가능하다. 중개 오퍼레이션들은 근복적으로 Lazy 하다. Stream API 는 크게 중계..