[Java] 컬렉션(Collection)
Machine translation — switch to KO for the original.
1) 컬렉션(Collection)은 무엇이고 왜 사용되는가?
자료를 담는 자료구조를 말한다.
구성은 java.util 패키지의 collection 인터페이스에 있으며, Iterable 인터페이스로부터 상속받고 있다.
여러 데이터를 담기 위해 컬렉션 이전에 배열 타입으로 자료를 저장할 수 있었다. 하지만, 컬렉션을 이용하면 여러 타입의 자료를 저장할 수 있다. 그리고 동적 메모리 할당으로 배열과 달리 크기 선언을 미리 할 필요가 없다. 추가적으로 필요한 만큼 데이터 공간을 추가할 수 있기 때문이다.
2) 컬렉션(Collection)의 자료구조 유형
그럼 자바에서 컬렉션 인터페이스를 이용하는(상속받는 자손 인터페이스) 자료구조 유형은 어떤 것들이 있을까?
대표적으로 List
1. List
인덱스를 사용해 간결하게 자료를 관리할 수 있다. 중복된 자료의 저장이 가능하며, null 값의 요소도 저장이 가능하다.
구현 클래스 종류 :
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList,
RoleUnresolvedList, Stack, Vector
2. Queue
자료를 순서대로 저장가능하며, 역시 중복 저장이 가능하다.
구현 클래스 종류 :
AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedDeque, ConcurrentLinkedQueue, DelayQueue,
LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue,
SynchronousQueue
3. Set
객체 순서가 없고, 중복저장이 불가하다.
구현 클래스 종류 :
AbstractSet, ConcurrentHashMap.KeySetView, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet,
JobStateReasons, LinkedHashSet, TreeSet
+ 컬렉션 인터페이스 외에 Map
3) 주요 메서드
1. 컬렉션(Collection) 주요 메서드
boolean add(Element e) → 자료 추가
boolean remove(Object o) → 하나의 객체를 제거
void clear() → 모든 객체 삭제
+ 1.의 메서드는 '상속(extends interface)'에 따라 하위 인터페이스에 상속되고, 아래 인터페이스에서 사용가능하다.
2. List
get(int index) → 인덱스 값의 데이터 값을 반환
set(int index, E element) → 특정 인덱스에 해당하는 요소(데이터 값)을 저장
subList(int fromIndex, int toIndex)
→ from인덱스에서 to인덱스 값 전까지의 값을 반환, String 클래스에서 substring() 메서드 개념과 유사
3. Queue
poll() → 가장 먼저 보관한 값을 꺼내고 반환
peek() → 가장 먼저 보관한 값을 단순참조, 꺼내지 않음
offer(E e) → 데이터 값을 순차보관
4. Set
List
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?