Convert List to Set | One-liner Java Code

Поделиться
HTML-код
  • Опубликовано: 5 ноя 2024

Комментарии • 4

  • @Sollace
    @Sollace 5 месяцев назад +1

    1. Use Set.of and/or Set.copyOf
    2. This will throw if the list has duplicate elements.

    • @The1080Pi
      @The1080Pi 5 месяцев назад +1

      I would prefer getting the size of set and compare with size of list to know if there are any duplicates.
      List.of()
      1. List numbers = List.of(1,2,3); this code is okay, however List numbers = List.of(1,2,3, null); results in nullpointerexception
      2. List numbers = List.of(1,2,3); numbers.add(4); this results in unsupportedoperationexception
      3. List numbers = List.of() generates an empty list
      List.of()
      1. List numbers = List.copyOf(List.of(1,2,3)); this code is okay, however List numbers = List.of(1,2,3, null); results in nullpointerexception
      2. List numbers = List.copyOf(List.of(1,2,3)); numbers.add(4); this results in unsupportedoperationexception
      3. List numbers = List.copyOf(List.of()) generates an empty list

    • @Sollace
      @Sollace 5 месяцев назад +1

      @@The1080Pi How is any of this relevant to what I said?

    • @InterviewMindsAtWork
      @InterviewMindsAtWork  Месяц назад

      Correct