❓
물음표살인마 블로그
  • README
  • ALGORITHM
    • Sieve of Eratosthenes
    • Round Up
    • Binary Search
    • Union Find
    • Sorting Array
    • Lcm, Gcd
  • TechTalk Review
    • Template
  • Books
    • CS Note for Interview
      • Ch1. Design Pattern & Programming paradigm
        • 1.1.1 Singleton Pattern
        • 1.1.2 Factory Pattern
        • 1.1.4 Observer Pattern
        • 1.1.5 Proxty Pattern & Proxy Server
        • 1.1.8 Model-View-Controller Pattern
        • 1.2.1 Declarative and Functional Programming
        • 1.2.2 Object Oriented Programming
      • Ch2. Network
        • 2.2.1 TCP/IP Four-Layer Model
        • 2.2.1-1 TCP 3, 4 way handshake
        • 2.3 Network Devices L4, L7
        • 2.4.1 ARP, RARP
        • 2.4.2 Hop By Hop Communication
        • 2.4.3 IP Addressing Scheme
      • Ch3. Operating System
        • 3.1.1 Roles and Structure of Operating Systems
        • 3.2.1 Memory Hierarchy
        • 3.2.2 Memory Management
        • 3.3.1 Processes and Compilation Process
        • 3.3.3 Memory Structure of a Process
        • 3.3.4 Process Control Block (PCB)
        • 3.3.5 Multiprocessing
        • 3.3.6 Threads and Multithreading
        • 3.3.7 Shared Resources and Critical Sections
        • 3.3.8 Deadlock
        • 3.4 CPU Scheduling Algorithm
      • Ch4. Database
        • 4.1 Database Basic
        • 4.2 Normalization
        • 4.3 Transaction and Integrity
        • 4.4 Types of Databases
        • 4.5 Indexes
        • 4.6 Types of Joins
        • 4.7 Principles of Joins
      • Ch5. Data Structure
    • Learning the Basics of Large-Scale System Design through Virtual Interview Cases
      • 1. Scalability based on user counts(1/2)
      • 1. Scalability based on user counts(2/2)
      • 2.Back-of-the-envelope estimation
      • 3. Strategies for System Design Interviews
      • 4. Rate Limiter
      • 5. Consistent Hashing
      • 6. Key-Value System Design
      • 7. Designing a Unique ID Generator for Distributed Systems
      • 8. Designing a URL Shortener
      • 9. Designing a Web Crawler
      • 10. Notification System Design
      • 11. Designing a News Feed System
      • 12. Chat System Design
      • 13. AutoComplete
      • 14. Design YouTube
      • 15. Design Google Drive
      • Loadbalancer Algorithms
      • Cache tier
      • CDN, Content Delivery Network
      • Stateless Web tier
    • Computer System A programmer's perspective
    • Effective Java
      • Item 1. Consider Static Factory Methods Instead of Constructors
      • Item 2. Consider a Builder When Faced with Many Constructor Parameters
      • Item 3. Ensure Singleton with Private Constructor or Enum Type
      • Item 4. Enforce Noninstantiability with a Private Constructor
      • Item 5. Prefer Dependency Injection to Hardwiring Resources
      • Item 6. Avoid Creating Unnecessary Objects
      • Item 7. Eliminate Obsolete Object References
      • Item 8. Avoid Finalizers and Cleaners
      • Item 9.Prefer try-with-resources to try-finally
      • Item10. Adhering to General Rules When Overriding equals
        • Handling Transitivity Issues
        • Ensuring Consistency
      • Item11. Override hashCode When You Override equals
      • Item12. Always Override toString
        • Always Override toString
      • Item13. Override Clone Judiciously
      • Item14. Consider Implementing Comparable
      • Item15. Minimize the Accessibility of Classes and Members
      • Item16. Accessor Methods Over Public Fields
      • Item17. Minimize Mutability
      • Item18. Composition over inherentance
      • Item19. Design and Document for Inheritance, or Else Prohibit It
      • Item20. Prefer Interfaces to Abstract Classes
      • Item21. Design Interfaces with Implementations in Mind
      • Item22. Use Interfaces Only to define Types
      • Item23. Prefer Class Hierarchies to Tagged Classes
      • Item24. Favor Static Member Classes Over Non-Static
      • Item28. Use Lists Instead of Arrays
      • Item29. Prefer Generic Types
      • Item30. Favor Generic Methods
    • Head First Design Patterns
      • Ch1. Strategy Pattern
      • Ch2. Observer Pattern
        • Ver1. Ch2. Observer Pattern
      • Ch3. Decorator Pattern
        • Ch3. Decorator Pattern
      • Ch4. Factory Pattern
      • Ch5. Singleton Pattern
      • Ch6. Command Pattern
      • Ch7. Adapter and Facade Pattern
      • Ch8. Template Method Pattern
    • Digging Deep into JVM
      • Chapter 2. Java Memory Area & Memory Overflow
      • Chapter 3. Garbage Collector & Memory Allocation Strategy (1/2)
      • Chapter 3. Garbage Collector & Memory Allocation Strategy (2/2)
      • Chapter 5. Optimization Practice
      • Chapter 6. Class file structure
      • Chapter 8. Bytecode Executor Engine (1/2)
  • Interview Practices
    • Restful API Practices
      • Url Shortener API
      • Event Ticket Reservation API
      • Course Management API
      • Search posts by tags API
      • Online Code platform API
      • Simple Task Management API
      • Event Participation API
      • Review System API
      • Car management API
      • Online Library
    • Tech Review
      • if(kakao)
        • Kakao Account Cache Migration / if(kakao)2022
        • Improving the Anomaly Detection System for KakaoTalk Messaging Metrics / if(kakao) 2022
        • Standardizing API Case Handling Without Redeployment / if(kakaoAI)2024
        • JVM warm up / if(kakao)2022
    • Naver Computer Science
      • Process & Thread
      • TCP & UDP
      • Spring & Servlet
      • Filter & Interceptor & AOP
      • Equals() & ==
      • Dependency Injection
      • Object Oriented Programming
  • F-Lab
    • Week1
      • Client & Server
      • HTTP
      • TCP/UDP
      • REST API
      • Questions
        • Object Oriented Programming
        • HTTP
        • Process & Thread
        • Data Structure
    • Week2
      • OSI 7 layer
      • Web vs WAS
    • Week3
      • RDB vs NoSQL
      • RDB Index
      • Cache
      • Redis
      • Messaging Queue
    • Week4
      • Project - Ecommerce
    • Week5
      • ERD - 1
    • Week6
      • Ecommerce - 2
      • Role
      • pw hashing && Salt
      • CreatedAt, ModifiedAt
      • JWT
      • Copy of ERD - 1
    • Week7
      • Vault (HashiCorp Vault)
    • Week 8
      • Api Endpoints
    • Week10
      • Product Create Workflow
  • TOY Project
    • CodeMentor
      • Implementation of Kafka
      • Project Improvement (Architectural Enhancements)
      • Communication between servers in msa
  • JAVA
    • MESI protocol in CAS
    • CAS (Compare and Set)
    • BlockingQueue
    • Producer & Consumer
    • Synchronized && ReentrantLock
    • Memory Visibility
    • Checked vs Unchecked Exception
    • Thread
    • Batch delete instead of Cascade
    • Java Questions
      • Week 1(1/2) - Basic Java
      • Week 1(2/2) - OOP
      • Week 2(1/2) - String, Exception, Generic
      • Week2(2/2) Lambda, Stream, Annotation, Reflection
      • Week3(1/2) Collections
      • Week3(2/2) Threads
      • Week4 Java Concurrency Programming
      • Week5 JVM & GC
    • Java 101
      • JVM Structure
      • Java Compiles and Execution Method
      • Override, Overload
      • Interface vs Abstract Class
      • Primitive vs Object Type
      • Identity and equality
      • String, StringBuilder, StringBuffer
      • Checked Exceptions and Unchecked Exceptions
      • Java 8 methods
      • Try-with-reources
      • Strong Coupling and Loose Coupling
      • Serialization and Deserialization
      • Concurrency Programming in Java
      • Mutable vs Immutable
      • JDK vs JRE
  • SPRING
    • DIP. Dependency Inversion Principal
    • Ioc container, di practice
    • @Transactional
    • Proxy Pattern
    • Strategy Pattern
    • Template Method Pattern
    • using profile name as variable
    • Spring Questions
      • Spring Framework
      • Spring MVC & Web Request
      • AOP (Aspect-Oriented Programming)
      • Spring Boot
      • ORM & Data Access
      • Security
      • ETC
  • DATABASE
    • Enhancing Query Performance & Stability - User list
    • Ensuring Data Consistency, Atomicity and UX Optimization (feat.Firebase)
    • Redis: Remote Dictionary Server
    • Database Questions
      • Week1 DBMS, RDBMS basics
      • Week2 SQL
      • Week3 Index
      • Week4 Anomaly, Functional Dependency, Normalization
      • Week5 DB Transaction, Recovery
    • Normalization
      • 1st Normal Form
      • 2nd Normal Form
      • 3rd Normal Form
  • NETWORK
    • HTTP & TCP head of line blocking
    • HTTP 0.9-3.0
    • Blocking, NonBlocking and Sync, Async
    • Network Questions
      • Week1 Computer Network Basic
      • Week2(1/3) Application Layer Protocol - HTTP
      • Week2(2/3) Application Layer Protocol - HTTPS
      • Week2(3/3) Application Layer Protocol - DNS
      • Week3 Application Layer
      • Week4 Transport Layer - UDP, TCP
      • Week5 Network Layer - IP Protocol
    • Network 101
      • https://www.google.com
      • TCP vs UDP
      • Http vs Https
      • TLS Handshake 1.2
      • HTTP Method
      • CORS & SOP
      • Web Server Software
  • OS
    • Operating System Questions
      • Week1 OS & How Computer Systems Work
      • Week2(1/2) Process
      • Week2(2/2) Thread
      • Week3 CPU Scheduling
      • Week4 Process Synchronize
      • Week5 Virtual Memory
    • Operating System 101
      • Operating system
        • The role of the operating system
        • The composition of the operating system.
      • Process
        • In Linux, are all processes except the initial process child processes?
        • Zombie process, orphan process
        • (Linux) Daemon process
        • Process address space
        • Where are uninitialized variables stored?
        • Determination of the size of the Stack and Heap
        • Access speed of Stack vs Heap
        • Reason for memory space partitioning
        • Process of compiling a process
        • sudo kill -9 $CURRENT_PID
      • Thread
        • Composition of a thread's address space
      • Process vs Thread
        • Creation of processes and threads in Linux
      • Multiprocessing
        • Web Browser
        • Implementation of multiprocessing
        • Application areas of multiprocessing
      • Multithreading
        • Application areas of multithreading
      • Interrupt
        • HW / SW Interrupt
        • Method of handling interrupts
        • Occurrence of two or more interrupts simultaneously
      • Polling
      • Dual Mode
        • Reason for distinguishing between user mode and kernel mode
      • System call
        • Differentiation between system calls
        • Types of system calls
        • Execution process of a system call
      • Process Control Block (PCB)
        • PCB의 구조
        • 쓰레드는 PCB를 갖고 있을까?
        • 프로세스 메모리 구조
      • Context switching
        • Timing of context switching
        • Registers saved during context switching
        • Context switching in processes
        • Context switching in threads
        • Difference between context switching in processes and threads
        • Information of the current process during context switching
      • Interprocess Communication (IPC)
        • Cases where IPC is used
        • Process address space in IPC Shared Memory technique
        • Types of IPC
  • COMPUTER SCIENCE
    • Computer Architecture 101
      • 3 components of a computer
      • RAM vs ROM
      • CPU vs GPU
      • SIMD
      • Two's complement
      • Harvard Architecture vs. von Neumann Architecture
      • The structure of a CPU.
      • Instruction cycle (CPU operation method)
      • Instruction pipelining
      • Bus
      • Memory area
      • Memory hierarchy structure
        • Reason for using memory hierarchy structure
      • Cache memory
      • L1, L2, L3 Cache
      • Locality of reference (cache)
      • Fixed-point vs Floating-point
        • epresentation of infinity and NaN (Not a Number) in floating-point
      • RISC vs CISC
      • Hamming code
      • Compiler
      • Linking
      • Compiler vs Interpreter
      • Mutex vs Semaphore
      • 32bit CPU and 64bit CPU
      • Local vs Static Variable
      • Page
  • Programming Paradigm
    • Declarative vs Imperative
  • JPA, QueryDsl
    • why fetchResults() is deprecated
  • PYTHON
    • Icecream
  • FASTAPI
    • Template Page
  • LINUX
    • Template Page
  • DATA STRUCTURE
    • Counting Sort
    • Array vs Linked List
  • GIT, Github
    • git clone, invalid path error
  • INFRA
    • Template Page
  • AWS
    • Server Log Archive Pipeline
    • Image Processing using Lambda
  • DOCKER
    • Docker and VM
    • Python Executable Environment
    • Docker commands
  • docker-compose
    • Kafka, Multi Broker
  • KUBERNATES
    • !Encountered Errors
      • my-sql restarts
      • kafka producer: disconnected
    • Kubernetes Components
    • Helm
      • Helm commands
    • Pod network
    • Service network
      • deployment.yaml
      • services.yaml
    • Service type
      • Cluster IP
      • NodePort
    • service-name-headless?
    • kube-proxy
  • GraphQL
    • Template Page
  • WEB
    • Template Page
  • Reviews
    • Graphic Intern Review
    • Kakao Brain Pathfinder Review
    • JSCODE 자바 1기 Review
  • 😁Dev Jokes
    • Image
      • Plot twist
      • Priorities
      • SQL join guide
      • Google is generous
      • Genie dislikes cloud
      • buggy bugs
      • last day of unpaid internship
      • what if clients know how to inspect
      • its just game
      • how i wrote my achievement on resume
      • self explanatory
      • chr(sum(range(ord(min(str(not))))))
Powered by GitBook
On this page
  • Stream
  • 특징
  • 종류
  • Lambda
  • 함수형 인터페이스
  • 기본 제공 함수형 인터페이스
  • Default
  • 디폴트의 단점
  • 다이아몬드 문제
  • Completable Future
  • Future의단점
  • CompletableFuture의 장점
  • Optional
  • 특징
  • Optional 생성
  • Optional 값 다루기
  • Optional + Stream
  1. JAVA
  2. Java 101

Java 8 methods

Java 8에서 추가된 기능

Stream

스트림은 데이터를 저장하지 않고, 일련의 연산을 통해 데이터를 처리하는 추상화된 개념입니다. 이를 통해 대량의 데이터를 간결하고 효율적으로 처리할 수 있습니다.

특징

  • 선언적 방식: 스트림 API는 선언적 방식으로 데이터를 처리합니다. 즉, 무엇을 할 것인지에 집중할 수 있게 해줍니다.

  • 데이터 변경 없음: 스트림을 사용한 데이터 처리 과정에서 원본 데이터를 변경하지 않습니다.

  • 지연 연산: 스트림 연산은 기본적으로 지연(Lazy) 연산으로, 실제로 필요한 시점에 연산이 수행됩니다.

  • 병렬 처리: 쉽게 병렬 스트림을 생성하여 병렬 처리를 구현할 수 있습니다.

종류

스트림은 크게 **시퀀셜 스트림(Sequential Stream)**과 **병렬 스트림(Parallel Stream)**으로 나뉩니다.

  • 시퀀셜 스트림: 스트림의 연산이 순차적으로 처리됩니다.

  • 병렬 스트림: 스트림의 연산이 여러 스레드에서 병렬로 처리됩니다. 이는 대용량 데이터 처리에 유용하지만, 항상 병렬 스트림이 성능을 개선하는 것은 아닙니다. 데이터의 크기, 연산의 복잡성 등에 따라 오히려 성능이 저하될 수 있습니다.

Lambda

람다 표현식은 Java 8에서 도입된 함수형 프로그래밍의 핵심 기능 중 하나입니다. 람다 표현식을 사용하면 익명 함수를 간단하게 표현할 수 있으며, 코드의 간결성과 가독성을 높여줍니다.

함수형 인터페이스 사용 예시
Predicate<Integer> isPositive = (n) -> n > 0;

System.out.println(isPositive.test(5));  // true
System.out.println(isPositive.test(-3)); // false

이 경우, isPositive는 Predicate<Integer> 타입의 변수입니다. 이는 하나의 Integer를 받아서 boolean 값을 반환하는 람다 표현식을 참조합니다.

함수형 인터페이스

람다 표현식은 함수형 인터페이스를 구현하는 데 주로 사용됩니다. 함수형 인터페이스는 하나의 추상 메서드만을 가지는 인터페이스로, Java 8에서는 이 인터페이스들에 @FunctionalInterface 애너테이션을 붙여 명시적으로 표현할 수 있습니다.

예시
@FunctionalInterface
interface MyFunction {
    void apply();
}

기본 제공 함수형 인터페이스

  • Predicate<T>: T 타입의 값을 입력받아 boolean을 반환합니다.

  • Function<T, R>: T 타입의 값을 받아 R 타입의 값을 반환합니다.

  • Supplier<T>: 인자를 받지 않고 T 타입의 값을 반환합니다.

  • Consumer<T>: T 타입의 값을 받아 어떤 동작을 수행하고 반환 값은 없습니다.

커스텀 함수형 인터페이스 활용 예시
@FunctionalInterface
interface MyPredicate<T> {
    boolean test(T t);
}

public class FunctionalInterfaceExample {
    public static void main(String[] args) {
        MyPredicate<Integer> isEven = n -> n % 2 == 0;

        System.out.println(isEven.test(4)); // 출력: true
        System.out.println(isEven.test(5)); // 출력: false
    }
}

이 예제에서는 MyPredicate 함수형 인터페이스를 사용하여 숫자가 짝수인지 여부를 검사하는 람다 표현식을 작성하였습니다.

커스텀 함수형 인터페이스 활용 예시2
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "User{name='" + name + "', age=" + age + '}';
    }
}

@FunctionalInterface
interface UserPredicate {
    boolean test(User user);
}

public class CustomFunctionalInterfaceExample {
    public static void main(String[] args) {
        List<User> users = Arrays.asList(
            new User("Alice", 23),
            new User("Bob", 17),
            new User("Anna", 25),
            new User("Charlie", 19)
        );

        // 나이가 18세 이상인지 검사
        UserPredicate isAdult = user -> user.getAge() >= 18;

        // 이름이 'A'로 시작하는지 검사
        UserPredicate startsWithA = user -> user.getName().startsWith("A");

        // 두 조건을 결합하여 검사
        List<User> filteredUsers = users.stream()
            .filter(user -> isAdult.test(user) && startsWithA.test(user))
            .collect(Collectors.toList());

        System.out.println(filteredUsers);
        // 출력: [User{name='Alice', age=23}, User{name='Anna', age=25}]
    }
}
  • User 클래스: 사용자의 name과 age를 가지고 있는 간단한 데이터 클래스입니다.

  • UserPredicate 함수형 인터페이스: User 객체를 받아 특정 조건을 테스트하는 함수형 인터페이스입니다.

  • isAdult 및 startsWithA: 각각 나이가 18세 이상인지, 이름이 'A'로 시작하는지를 검사하는 두 개의 람다 표현식입니다.

  • 필터링 및 수집: 스트림 API를 사용해 사용자 리스트에서 두 가지 조건을 모두 만족하는 사용자만 필터링한 후 리스트로 수집합니다.

Default

이전 버전의 Java에서는 인터페이스에 메서드를 추가할 경우, 그 인터페이스를 구현하는 모든 클래스에서 해당 메서드를 구현해야 했습니다. 하지만 디폴트 메서드를 사용하면 인터페이스에 메서드를 추가하더라도 구현 클래스에서 해당 메서드를 구현할 필요가 없습니다.

디폴트 메서드의 사용 예시
interface Vehicle {
    default void start() {
        System.out.println("Vehicle is starting");
    }
}

class Car implements Vehicle {
    // Car 클래스는 start 메서드를 구현하지 않아도, Vehicle 인터페이스의 기본 구현을 사용합니다.
}

public class DefaultMethodExample {
    public static void main(String[] args) {
        Car car = new Car();
        car.start(); // 출력: Vehicle is starting
    }
}

이 예제에서 Vehicle 인터페이스는 start라는 디폴트 메서드를 가지고 있습니다. Car 클래스는 이 인터페이스를 구현하면서 start 메서드를 별도로 구현하지 않았지만, Vehicle 인터페이스의 디폴트 메서드를 사용할 수 있습니다.

디폴트의 단점

  1. 다이아몬드 문제(Diamond Problem): 디폴트 메서드를 사용할 때, 다중 상속 구조에서 다이아몬드 문제가 발생할 수 있다. 이는 인터페이스들이 동일한 디폴트 메서드를 제공할 때 발생하며, 이 경우에는 구현 클래스에서 메서드를 오버라이드하여 해결해야 한다.

  2. 혼란을 유발할 수 있음: 디폴트 메서드는 인터페이스의 모든 구현체에서 동일하게 동작해야 한다. 만약 기본 구현이 모든 상황에 적합하지 않다면, 이를 오버라이드해야 할 수도 있다. 이 과정에서 오버라이드할 필요가 있는지 혼란스러울 수 있다.

  3. 인터페이스의 일관성: 인터페이스는 기본적으로 동작을 정의하는 것이 아니라, 동작을 명시하는 역할을 해야 한다. 디폴트 메서드는 이러한 인터페이스의 일관성을 무너뜨릴 수 있다. 따라서 디폴트 메서드를 추가할 때는 이 메서드가 정말로 모든 구현체에 적합한지 신중하게 고려해야 한다.

다이아몬드 문제

여러 인터페이스를 구현하는 클래스에서 같은 이름의 디폴트 메서드를 상속받을 때 문제가 발생할 수 있습니다. 이를 다이아몬드 문제(Diamond Problem)라고 부릅니다.

다이아몬드 문제 예시
interface InterfaceA {
    default void hello() {
        System.out.println("Hello from InterfaceA");
    }
}

interface InterfaceB {
    default void hello() {
        System.out.println("Hello from InterfaceB");
    }
}

public class DiamondProblemExample implements InterfaceA, InterfaceB {
    public void hello() {
        InterfaceA.super.hello(); // InterfaceA의 hello 메서드 호출
    }

    public static void main(String[] args) {
        DiamondProblemExample example = new DiamondProblemExample();
        example.hello(); // 출력: Hello from InterfaceA
    }
}

이 예제에서 DiamondProblemExample 클래스는 InterfaceA와 InterfaceB 모두의 hello 메서드를 상속받습니다. 하지만, 두 인터페이스에서 동일한 메서드 이름을 사용하기 때문에 충돌이 발생합니다. 이를 해결하기 위해 특정 인터페이스의 메서드를 호출하도록 명시해야 합니다.

Completable Future

비동기 프로그래밍은 처음 자바5에 Future가 추가되면서 가능하게 되었고, 이것을 통해 비동기 작업에 대한 결과값을 반환받을 수 있게 됐습니다. Future에는 단점들이 존재했는데, 단점들을 해결하고자 나온게 자바8에서 CompletableFuture입니다.

Future의단점

  • 외부에서 작업을 중단시키는 기능을 지원하지 않음 (제어 불가) 비동기 작업이 무한루프나 오래걸릴경우, 강제로 완료 시킬 수가 없습니다.

  • 완전한 비동기 작업이 아닙니다. Future는 비동기 작업이지만, 결과를 처리하기 위해서는 동기화된 블로킹 코드(’get’)로 돌아가야 합니다.

  • 여러 Future를 조합할 수 없습니다. (예: 회원 정보를 가져오는 작업과 알림을 발송하는 작업을 조합하려면 각 Future의 결과를 순차적으로 처리해야 하며, 이를 효율적으로 조합할 수 있는 메커니즘이 없습니다.)

  • 예외처리가 어렵습니다. 예: 첫번째 작업의 성공 유무에 따른 두번쨰 작업 실행 방향 처리 구현이 어렵습니다.

CompletableFuture의 장점

  • 유연성: 여러 비동기 작업을 유연하게 조합할 수 있습니다.

  • 비동기 예외 처리: 예외 처리를 더욱 간단하게 할 수 있습니다.

  • 명시적인 비동기 API: 비동기 작업의 흐름을 명확하게 표현할 수 있습니다.

  • 병렬 실행 지원: 여러 비동기 작업을 병렬로 실행하여 성능을 향상시킬 수 있습니다.

CompletableFuture의 작업 종류

비동기 작업 실행, 작업 콜백, 작업 조합, 예외처리가 있습니다.

  • 비동기 작업 실행

    • runAsync(Runnable) - 반환 값이 없는 비동기 작업을 실행합니다.

    • supplyAsync(Supplier<U>) - 반환 값이 있는 비동기 작업을 실행합니다.

  • 작업 콜백

    • thenApply(Function<T, U>) - 결과를 받아서 다른 결과로 변환합니다.

    • thenAccept(Consumer<T>) - 결과를 받아서 소비합니다. 반환 값은 없습니다.

    • thenRun(Runnable) - 결과를 받지 않고 단순히 실행합니다.

  • 작업 조합

    • thenCompose(Function<T, CompletionStage<U>>) 이전 작업의 결과를 받아서 새로운 비동기 작업을 실행합니다.

    • thenCombine(CompletionStage<U>, BiFunction<T, U, V>) - 두 개의 비동기 작업 결과를 조합합니다.

    • allOf(CompletableFuture<?>...) - 여러 비동기 작업을 모두 완료할 때까지 기다립니다.

    • anyOf(CompletableFuture<?>...) - 여러 비동기 작업 중 하나라도 완료되면 결과를 반환합니다.

  • 예외 처리

    • exceptionally(Function<Throwable, T>) - 예외가 발생했을 때 기본값을 반환하거나, 예외를 처리합니다.

    • handle(BiFunction<T, Throwable, U>) - 정상 결과와 예외를 모두 처리할 수 있습니다.

    • handleAsync(BiFunction<T, Throwable, U>) - 비동기적으로 정상 결과와 예외를 모두 처리할 수 있습니다.

Optional

Optional은 값이 있을 수도 있고 없을 수도 있는 객체를 감싸는 컨테이너 클래스입니다.

특징

  • 명시적 Null 처리: Optional을 사용하면 null 반환 가능성을 명시적으로 나타낼 수 있습니다.

  • 널 참조 방지: Optional 메서드를 통해 안전한 null 참조를 보장합니다.

  • 간결한 코드: 반복적인 null 체크를 줄이고, 더 간결하고 가독성 높은 코드를 작성할 수 있습니다.

Optional 생성

Optional.of()

Optional.of()는 null이 아닌 값을 감쌉니다. 만약 null 값을 전달하면 NullPointerException이 발생합니다.

String name = "John";
Optional<String> optionalName = Optional.of(name);
Optional.ofNullable()

Optional.ofNullable()는 값이 null일 수도 있고, 아닐 수도 있는 경우에 사용됩니다. null 값이 전달되면 빈 Optional 객체(Optional.empty())를 반환합니다.

String name = null;
Optional<String> optionalName = Optional.ofNullable(name);
Optional.Empty()

Optional.empty()는 값이 없는 빈 Optional 객체를 반환합니다.

Optional<String> emptyOptional = Optional.empty();

Optional 값 다루기

isPresent()

isPresent() 메서드는 Optional 객체에 값이 존재하는지 확인합니다. 값이 존재하는 경우 true를 반환하며, get() 메서드를 통해 값을 가져올 수 있습니다. 그러나 get() 메서드는 값이 없을 경우 NoSuchElementException을 발생시키므로, 반드시 isPresent()로 값이 있는지 먼저 확인해야 합니다.

Optional<String> optionalName = Optional.of("John");

if (optionalName.isPresent()) {
    System.out.println(optionalName.get()); // 출력: John
}
ifPresent()

ifPresent() 메서드는 값이 존재하는 경우에만 특정 동작을 수행할 수 있습니다. 값이 없을 때는 아무 작업도 하지 않습니다.

optionalName.ifPresent(name -> System.out.println("Name: " + name));
 // 출력: Name: John
orElse()

orElse() 메서드는 Optional 객체에 값이 없을 경우 제공된 기본 값을 반환합니다.

String name = optionalName.orElse("Default Name");
System.out.println(name); // 출력: John

Optional<String> emptyOptional = Optional.empty();
String defaultName = emptyOptional.orElse("Default Name");
System.out.println(defaultName); // 출력: Default Name
orElseGet()

orElseGet() 메서드는 Optional에 값이 없을 경우 기본 값을 제공하는 Supplier를 실행합니다. orElse()와의 차이점은 기본 값을 계산하는 비용이 높은 경우, orElseGet()을 사용하여 지연 평가(lazy evaluation)를 할 수 있다는 점입니다.

String name = optionalName.orElseGet(() -> "Default Name");
orElseThrow()

orElseThrow() 메서드는 값이 없을 경우 예외를 발생시킵니다.

String name = optionalName.orElseThrow(() -> new IllegalArgumentException("Name not found"));

Optional + Stream

예시: 사용자 목록에서 특정 사용자 찾기
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

public class OptionalStreamExample {
    public static void main(String[] args) {
        List<User> users = Arrays.asList(
            new User("John", 25),
            new User("Jane", 22),
            new User("Jack", 28)
        );

        Optional<User> user = users.stream()
            .filter(u -> "Jane".equals(u.getName()))
            .findFirst();

        user.ifPresent(u -> System.out.println("User found: " + u.getName())); // 출력: User found: Jane
    }
}
PreviousChecked Exceptions and Unchecked ExceptionsNextTry-with-reources

Last updated 9 months ago