JAVA - Collections & Stream

choko's avatar
Jun 29, 2024
JAVA - Collections & Stream
notion image
 

collection vs Collection vs Collections

  • collection : 데이터의 집합이나 그룹(List + Set)
  • Collection(java.util.Collection)
    • Collection 프레임워크 내에 있는 Collection 인터페이스
    • Set, List, Queue 인터페이스가 하위 인터페이스로써 있다.
      • 위 그림에서 ArrayList는 Collection → List → ArrayList 클래스로 구현한 것이다.
  • Collections(java.util.Collections)
    • Collection 인터페이스를 구현한 클래스에 대한 객체 생성, 정렬, 병합, 검색 등의 기능을 안정적으로 수행하도록 도와주는 Utility 클래스
    • Generic으로 작성되었기 때문에, Collection 하위 인터페이스면 사용 가능
    • 주요 Method
      • max
      • min
      • sort
      • copy
      • reverse
      • binarySearch
      • unmodifiableSet
      • List<Integer> numbers = List.of(4, 0, 5, 2, 7, 1, 8, 6, 9, 3); int max = numbers.isEmpty() ? -1 : Collections.max(numbers); System.out.println("Max: " + max); // Max: 9 // Collections.max()는 리스트가ㅕㄴㄷ 비어 있을 시 NoSuchElementException 발생
         
       

stream

  • 스트림은 데이터 처리 연산을 지원하도록 소스에서 추출된 연속된 요소이다.
  • 스트림 데이터는 filter, map, reduce, find, match, sort 등으로 데이터를 조작할 수 있다.
 
Share article

Tom의 TIL 정리방