** 자기 공부용 및 암기용으로 포스팅하는 글 입니다.
** 정확한 정보는 보장 할 수 없으니, 참고용으로만 봐주시면 감사하겠습니다.
** 제 개인적인 생각 및 자기 암시목적으로 작성되기에 이점 양해 부탁드립니다.
들어가기에 앞서..
array를 대신하여 편의성 기능이 많은 컬렉션 프레임워크(Collection Framework)를 사용한다.
프레임워크들 중 List 의 ArrayList, LinkedList들의 개념정리 및 메서드 내용이다.
★ 알아볼 내용
1. Collection Framework
2. List
3. ArrayList
4. LinkedList
5. ArrayList vs LinkedList 정리
1. Collection Framework
여러 데이터의 집합을 컬렉션(Collection)이라고 하며, 이러한 컬렉션을 다루는 데편리한 메서드를 정의해 놓은 것을 컬렉션 프레임워크(Collection Framework)라고 한다.
컬렉션 프레임워크(Collection Framework)는 특정 자료 구조에 데이터를 CRUD등의 동작을 수행하는 편리한 메서드를 제공한다.
※ CRUD : Create (생성), Read (읽기), Update (갱신), Delete (삭제)
공통적으로 Collection 인터페이스를 구현하고 있는 List / Set프레임워크의 경우 아래의 메서드를 사용할 수 있다.
기능 | Return Type | Method | Info |
객체 추가 | boolean | add( Object o ) | 객체를 컬렉션에 추가 |
boolean | addAll( Collection c ) | 컬렉션의 객체들을 컬렉션에 추가 | |
객체 검색 | boolean | contains( Object o ) | 객체가 저장되어 있는지 여부 확인 |
boolean | containsAll( Collection c ) | 컬렉션이 저장되어 있는지 여부 확인 | |
lterator | iterator() | 컬렉션의 iterator 리턴 | |
boolean | equals( Object o ) | 컬렉션이 동일한지 여부 확인 | |
boolean | isEmpty() | 컬렉션이 비어있는지 여부 확인 | |
int | size() | 저장된 전체 객체 수 확인 | |
객체 삭제 | void | clear() | 컬렉션 비우기 |
boolean | remove( Object o ) | 객체를 삭제 후 성공 여부 확인 | |
boolean | removeAll( Collection c ) | 컬렉션을 삭제 후 성공 여부 확인 | |
boolean | retainAll( Collection c ) | 매개변수 컬렉션을 제외한 모든 객체를 컬렉션에서 삭제.컬렉션에 변화있는지 여부 확인 | |
객체 변환 | Object[] | toArray() | 컬렉션에 저장된 객체를 객체배열(Object [])로 반환 |
Object[] | toArray( Object[] a ) | 배열에 컬렉션의 객체를 저장 후 반환 |
2. List
여러 데이터의 집합체인 컬렉션 프레임워크중에 순서가 있으며, 중복 값이 허용 가능한 프레임워크이다.
각 객체(데이터)들을 인덱스로 관리하기에 객체를 저장하면 자동으로 인덱스가 부여. 해당 인덱스로 검색/추가/삭제가 가능하다.
List의 종류로는 ArrayList / LinkedList / Stack / Vector 등이 있다.
각 구현체 마다 특징은 아래와 같다.
ArrayList : 동기화 보장하지 않는 List
Vector : 동기화 보장되는 List
Statk : 스택자료구조
LinkedList : node형 연결방식 자료구조
아래는 List 인터페이스에서 공통으로 사용가능한 메서드 이다.
기능 | Return Type | Method | Info |
객체 추가 | void | add(int index, Object o) | 주어진 인덱스에 객체를 추가 |
boolean | addAll(int index, Collection c) | 주어진 인덱스에 컬렉션을 추가 | |
Object | set(int index, Object o) | 주어진 인덱스 위치에 객체를 변경. | |
객체 검색 | Object | get(int index) | 주어진 인덱스에 저장된 객체를 반환 |
int | indexOf(Object o) | 순방향 탐색하여 주어진 객체 인덱스 반환. | |
lastIndexOf(Object o) | 역방향 탐색하여 주어진 객체 인덱스 반환. | ||
Listlterator | listiterator() | List의 객체를 탐색할 수 있는 Listlterator 반환 | |
listiterator(int index) | 주어진 index으로 부터 탐색할 수 있는 Listlterator 반환 | ||
List | subList(int fromIndex, int toIndex) | fromIndex부터 tolndex에 있는 객체를 반환 | |
객체 삭제 | Object | remove(int index) | 주어진 인덱스에 저장된 객체를 삭제 후 삭제된 객체 반환 |
boolean | remove( Object o ) | 주어진 객체를 삭제 | |
객체 변환 | void | sort(Comparator c) | 주어진 비교자로 List를 정렬 |
3. ArrayList
객체가 인덱스로 관리가 되며 자동으로 저장용량이 늘어나고 데이터가 연속적으로 존재한다.
기능적으로 Vector랑 비슷하지만, Vector를 개선한 것으로 주로 ArrayList를 사용한다.
객체가 추가 될 경우 인덱스로 관리되는 점은 배열과 유사하나, ArrayList는 가변배열이다.
또한 연속적으로 존재하며 특정 인덱스의 객체를 제거할 경우 빈 공간을 남기지 않고 당겨오듯 자동정렬이 된다.
그렇기에 많은 데이터에서 앞 인덱스의 값이 제거가 될 경우 모두 한칸씩 당겨와야하는 작업이 진행되기에
자주 삽입 및 삭제가 이루어지는 경우 LinkedList를 사용하는 것이 더 좋은 방법이다.
4. LinkedList
ArrayList에서 데이터를 효율적으로 추가, 삭제, 변경하기 위해 사용이 된다.
그렇기에 데이터가 연속적으로 존재하지 않으며, 대신 Link되어 있다.
연속적으로 존재하지 않기에 데이터가 삭제 및 추가가 되면 각 객체의 Link 주소를 바꿔주는 방식으로 간단하게 진행이 되며 그렇기에 처리속도가 훨씬 빠르다.
5. ArrayList vs LinkedList 정리
ArrayList의 경우..
-. 데이터를 순차적으로 추가하거나 삭제하는 경우에 강점을 가짐
-. 데이트를 불러오는 경우 강점을 가짐
-. 중간에 데이터가 추가 및 삭제될 경우는 약점을 가짐.
LinkedList의 경우..
-. 데이터를 중간에 추가하거나 삭제할 경우 ArrayList보다 빠른속도를 보이는 강점을 가짐
-. 데이터를 불러오는 경우에는 ArrayList보다 상대적으로 느린 약점을 가짐.
즉 데이터의 잦은 변경이 예상된다면 LinkedList를, 데이터의 개수가 변하지 않는다면 ArrayList를 사용하는 것이 좋다..
'JAVA 지식 및 공부' 카테고리의 다른 글
[JAVA] StringTokenizer & StringBuilder & StringBuffer (0) | 2024.06.06 |
---|---|
[JAVA] Collection - 열거형(Enum) 정리 (0) | 2024.05.29 |
[JAVA] Integer 클래스의 메소드 정리 (0) | 2024.04.15 |
[JAVA] 원시 타입(Primitive Type) vs 참조 타입(Refernce Type) (1) | 2024.04.12 |
[JAVA] IntelliJ/인텔리제이 한글 깨짐 현상 개선 (2) | 2024.04.04 |