[6/8 TIL] JAVA(Collection, Iterator, Stream, Optional)

yumyeonghanยท2023๋…„ 6์›” 10์ผ
0
post-custom-banner

๐Ÿƒํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฐฑ์—”๋“œ ๋ฐ๋ธŒ์ฝ”์Šค 4๊ธฐ ๊ต์œก๊ณผ์ •์„ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.๐Ÿƒ

Collection

List

  • List ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๊ตฌํ˜„ ํด๋ž˜์Šค๋กœ๋Š” ArrayList, LinkedList ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Set

  • Set ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ˆœ์„œ๊ฐ€ ์—†๋Š” ๊ณ ์œ ํ•œ ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๊ตฌํ˜„ ํด๋ž˜์Šค๋กœ๋Š” HashSet, TreeSet ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ•ด์‹œ ๊ธฐ๋ฐ˜ ๋˜๋Š” ์ •๋ ฌ๋œ ์ˆœ์„œ๋กœ ์š”์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

Map

  • Map ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ‚ค์™€ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ํ‚ค๋Š” ๊ณ ์œ ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฐ’์€ ์ค‘๋ณต์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ๊ตฌํ˜„ ํด๋ž˜์Šค๋กœ๋Š” HashMap, TreeMap, LinkedHashMap ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Queue

  • Queue ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์„ ์ž…์„ ์ถœ(FIFO) ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž…์€ ํ•œ์ชฝ ๋์—์„œ, ์‚ญ์ œ๋Š” ๋‹ค๋ฅธ ํ•œ์ชฝ ๋์—์„œ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  • ๊ตฌํ˜„ ํด๋ž˜์Šค๋กœ๋Š” LinkedList, PriorityQueue ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Stack

  • Stack ํด๋ž˜์Šค๋Š” ํ›„์ž…์„ ์ถœ(LIFO) ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๊ฐ€ ๊ฐ™์€ ์ชฝ์—์„œ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  • ์ฃผ์š” ๋ฉ”์„œ๋“œ๋กœ๋Š” push(), pop(), peek() ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Iterator

public class Main {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Iterator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปฌ๋ ‰์…˜์˜ ์š”์†Œ์— ์ ‘๊ทผ
        Iterator<String> iterator = fruits.iterator();
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            System.out.println(fruit);
        }

        // Iterator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์˜ ์š”์†Œ ์‚ญ์ œ
        iterator = fruits.iterator();
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            if (fruit.equals("Banana")) {
                iterator.remove(); // ํ˜„์žฌ ์š”์†Œ ์‚ญ์ œ
            }
        }

        System.out.println(fruits); // ์ถœ๋ ฅ: [Apple, Orange]
    }
}
  • ์ปฌ๋ ‰์…˜์˜ ์š”์†Œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • hasNext() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋‹ค์Œ ์š”์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ , next() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋‹ค์Œ ์š”์†Œ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.
  • ์š”์†Œ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Iterator๋ฅผ ํ†ตํ•ด remove() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ˜„์žฌ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
  • Iterator๋Š” ์ž๋ฐ”์˜ ๋ชจ๋“  ์ปฌ๋ ‰์…˜ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Stream

  • ์ž๋ฐ” 8๋ถ€ํ„ฐ ๋„์ž…๋œ Stream์€ ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ปฌ๋ ‰์…˜์— ์ €์žฅ๋œ ์š”์†Œ๋“ค์„ ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ๋ณ‘๋ ฌ ์ŠคํŠธ๋ฆผ์„ ์‚ฌ์šฉํ•˜๋ฉด, ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Stream์€ ํ•œ ๋ฒˆ ์‚ฌ์šฉํ•œ ํ›„์—๋Š” ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ์ค‘๊ฐ„ ์—ฐ์‚ฐ๊ณผ ์ตœ์ข… ์—ฐ์‚ฐ์œผ๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.
    • ์ค‘๊ฐ„ ์—ฐ์‚ฐ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ํ•„ํ„ฐ๋งํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค.
    • ์ตœ์ข… ์—ฐ์‚ฐ์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ค‘๊ฐ„ ์—ฐ์‚ฐ

filter(Predicate<T> predicate)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

List<Integer> evenNumbers = numbers.stream()
                                   .filter(n -> n % 2 == 0)
                                   .collect(Collectors.toList());

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(evenNumbers); // [2, 4]
  • ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋“ค๋กœ ๊ตฌ์„ฑ๋œ ์ƒˆ๋กœ์šด Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

map(Function<T, R> mapper)

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

List<Integer> nameLengths = names.stream()
                                 .map(String::length)
                                 .collect(Collectors.toList());

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(nameLengths); // [5, 3, 7]
  • ์š”์†Œ๋ฅผ ๋‹ค๋ฅธ ์š”์†Œ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ƒˆ๋กœ์šด Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

distinct()

List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 5, 5);

List<Integer> distinctNumbers = numbers.stream()
                                       .distinct()
                                       .collect(Collectors.toList());

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(distinctNumbers); // [1, 2, 3, 4, 5]
  • ์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

sorted()

List<Integer> numbers = Arrays.asList(5, 3, 1, 4, 2);

List<Integer> sortedNumbers = numbers.stream()
                                     .sorted()
                                     .collect(Collectors.toList());

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(sortedNumbers); // [1, 2, 3, 4, 5]
  • ์š”์†Œ๋ฅผ ์ •๋ ฌํ•œ ์ƒˆ๋กœ์šด Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

limit(long maxSize)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

List<Integer> limitedNumbers = numbers.stream()
                                      .limit(3)
                                      .collect(Collectors.toList());

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(limitedNumbers); // [1, 2, 3]
  • ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ maxSize๋งŒํผ ๊ฐ–๋Š” ์ƒˆ๋กœ์šด Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ตœ์ข… ์—ฐ์‚ฐ

forEach(Consumer<T> action)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

numbers.stream()
       .forEach(System.out::println); // ๊ฒฐ๊ณผ ์ถœ๋ ฅ: ๊ฐ ์ˆซ์ž๊ฐ€ ํ•œ ์ค„์”ฉ ์ถœ๋ ฅ๋จ
  • Stream์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

collect(Collector<T, A, R> collector)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

String concatenatedNumbers = numbers.stream()
                                    .map(Object::toString)
                                    .collect(Collectors.joining(", "));

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(concatenatedNumbers); // "1, 2, 3, 4, 5"
  • Stream์˜ ์š”์†Œ๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ ์ปฌ๋ ‰์…˜ ๋˜๋Š” ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

reduce(BinaryOperator<T> accumulator)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

// ๋ชจ๋“  ์ˆซ์ž์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ
Optional<Integer> sum = numbers.stream()
                               .reduce((a, b) -> a + b);

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(sum.orElse(0)); // 15
  • Stream์˜ ์š”์†Œ๋“ค์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์กฐํ•ฉํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ Optional๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

count()

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

// ์š”์†Œ์˜ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ
long count = numbers.stream()
                   .count();

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(count); // 5
  • Stream์˜ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

anyMatch(Predicate<T> predicate)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

boolean anyEvenNumber = numbers.stream()
                               .anyMatch(n -> n % 2 == 0);

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(anyEvenNumber); // true
  • Stream์˜ ์š”์†Œ ์ค‘ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์กด์žฌํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

allMatch(Predicate<T> predicate)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

boolean allGreaterThanZero = numbers.stream()
                                   .allMatch(n -> n > 0);

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(allGreaterThanZero); // true
  • Stream์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

noneMatch(Predicate<T> predicate)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

boolean noneGreaterThanFive = numbers.stream()
                                     .noneMatch(n -> n > 5);

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(noneGreaterThanFive); // true
  • Stream์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

findFirst()

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

Optional<Integer> firstEvenNumber = numbers.stream()
                                           .filter(n -> n % 2 == 0)
                                           .findFirst();

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(firstEvenNumber.orElse(-1)); // 2
  • Stream์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ Optional๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ŠคํŠธ๋ฆผ์˜ ์š”์†Œ๊ฐ€ ์ •๋ ฌ๋˜์–ด ์žˆ์ง€ ์•Š๊ฑฐ๋‚˜, ํŠน์ •ํ•œ ์š”์†Œ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ findAny() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

findAny()

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

Optional<Integer> anyNumber = numbers.stream()
                                     .findAny();

// ๊ฒฐ๊ณผ ์ถœ๋ ฅ
System.out.println(anyNumber.orElse(-1)); // 1 or 2 or 3 or 4 or 5 (๋žœ๋ค)
  • Stream์—์„œ ์ž„์˜์˜ ์š”์†Œ๋ฅผ Optional๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ŠคํŠธ๋ฆผ์˜ ์š”์†Œ๊ฐ€ ์ •๋ ฌ๋˜์–ด ์žˆ๊ณ  ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ฐพ์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” findFirst() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

Optional

public class Main {
    public static void main(String[] args) {
        String name = "John";
        Optional<String> optionalName = Optional.of(name);  // ๊ฐ’์ด ์žˆ๋Š” Optional ์ƒ์„ฑ

        // ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
        boolean isEmpty = optionalName.isEmpty();
        boolean isPresent = optionalName.isPresent();

        // ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
        String retrievedName = optionalName.get();

        // ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •
        String defaultValue = optionalName.orElse("Default");
        String defaultValueFromSupplier = optionalName.orElseGet(() -> "Default");

        // ํ•จ์ˆ˜ํ˜• ์Šคํƒ€์ผ์˜ ์—ฐ์‚ฐ
        optionalName.map(String::toUpperCase)
                    .ifPresent(System.out::println);  // ๊ฒฐ๊ณผ ์ถœ๋ ฅ: JOHN
    }
}
  • Optional์€ ์ž๋ฐ” 8๋ถ€ํ„ฐ ๋„์ž…๋œ ๊ฐ’์„ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋ž˜ํผ ํด๋ž˜์Šค๋กœ, null์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • null ๋Œ€์‹ ์— Optional.empty()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, NullPointException(NPE)๋ฅผ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.
  • isEmpty() ๋˜๋Š” IsPresent() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Optional์ด ๊ฐ’์„ ๊ฐ€์กŒ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • get()๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Optional์ด ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ NoSuchElementException์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋จผ์ € ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • Optional์— ๊ฐ’์ด ์—†์„ ๋•Œ OrElse()๋‚˜ orElseGet()๋กœ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ orElseThrow()๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋งต(Map)์ด๋‚˜ ํ•„ํ„ฐ(Filter) ๋“ฑ์˜ ํ•จ์ˆ˜ํ˜• ์Šคํƒ€์ผ ์—ฐ์‚ฐ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
profile
์›น ๊ฐœ๋ฐœ์— ๊ด€์‹ฌ ์žˆ์Šต๋‹ˆ๋‹ค.
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€