크게 List, Set, Map 계열로 나눌 수 있다.
순서(index)가 있다.
ArrayList, Vector, LinkedList 등이 해당된다.
//ArrayList 생성
ArrayList<String> ar = new ArrayList<>(); //비어있는 ArrayList 생성
ArrayList<String> ar = new ArrayList<>(co); //지정된 컬렉션의 요소를 포함하는 ArrayList 생성.하단참조.
ArrayList<String> ar = new ArrayList<>(int값); //초기 size를 정함. 실제size는 데이터 추가삭제에 따라 달라짐.
//ArrayList 추가 및 삽입
ar.add(Object); //요소를 List의 마지막에 추가
ar.add(index, Object); //지정된 위치에 요소를 삽입
//ArrayList 요소 삭제
ar.remove(Object); //지정된 요소 삭제
ar.remove(index); //지정된 index의 요소 삭제
ar.clear(); //리스트 내 모든 요소 삭제
//ArrayList 검색 및 접근
ar.get(index); //지정된 index 위치의 요소를 반환
ar.indexOf(Object); //지정된 요소가 처음으로 등장하는 위치의 index 반환
ar.lastIndexOf(Object); //지정된 요소가 마지막으로 등장하는 위치의 index 반환
ar.contains(Object); //리스트가 특정 요소를 포함하는지 여부를 반환
//ArrayList 크기 및 상태
ar.size(); //리스트의 요소 갯수를 반환
ar.isEmpty(); //리스트가 비어있는지 여부를 반환
//ArrayList 내 요소 수정
ar.set(index, Object); //지정된 index의 요소를 새로 지정한 요소로 수정
//ArrayList 반복자
iterator(); //리스트의 요소에 대한 순차적 접근을 위한 반복자를 반환
listIterator(); //리스트의 요소를 순차적으로 앞 또는 뒤로 이동하면서 접근할 수 있는 리스트 반복자를 반환
//* iterator 예시) 요소에 각각 1을 더해주는 경우 (ArrayList도 integer.)
Iterator<Integer> iterator = numbers.iterator();
while (iterator.hasNext()) {
int index = ar.indexOf(iterator.next());
int updatedValue = ar.get(index) + 1;
ar.set(index, updatedValue);
}
//* listIterator 예시) 요소에 각각 1을 더해주는 경우. 순방향으로 진행. (ArrayList도 integer.)
ListIterator<Integer> listIterator = numbers.listIterator();
while (listIterator.hasNext()) {
int index = ar.indexOf(listIterator.next());
int updatedValue = ar.get(index) + 1;
ar.set(index, updatedValue);
}
//* listIterator 예시) 요소에 각각 1을 더해주는 경우. 역방향으로 진행. (ArrayList도 integer.)
ListIterator<Integer> listIterator = numbers.listIterator();
while (listIterator.hasPrevious()) {
int index = listIterator.previousIndex();
int updatedValue = ar.get(index) + 1;
ar.set(index, updatedValue);
}
// ArrayList를 배열로 변환
ar.toArray(); //리스트 요소들을 배열로 반환
ar.toArray(type); //지정한 타입의 배열로 반환 (integer 등)
* 예시) String[] array = ar.toArray(new String[0]); //default 타입이 object기 때문에 배열 타입을 지정
보통은 순서가 없으며, 중복 저장이 안된다.
단, TreeSet 의 경우 자동으로 사전순으로 정렬해준다.
//HashSet 선언
HashSet<String> hs = new HashSet<>(); // <>안에 타입을 명시
//HashSet에 요소 추가
hs.add("element"); //요소를 set에 추가
//HashSet 내 요소 제거
hs.remove("element"); //set에서 해당 요소를 제거
hs.clear(); //set의 모든 요소 제거
//HashSet 내 요소 검색
hs.contains("element"); //set에 해당 요소가 존재하는지 여부를 반환
//HashSet의 크기 및 상태
hs.size(); //set에 포함된 요소의 갯수를 반환
hs.isEmpty(); //set이 비어있는지 여부를 반환
//HashSet을 배열로 변환
hs.toArray(); //set의 요소들을 배열로 반환
hs.toArray(type); //set의 요소들을 특정 타입의 배열로 반환
*예시) String[] array = hs.toArray(new String[0]);
//HashSet의 반복자
iterator(); //set의 모든 요소에 대해 접근을 하여 처리하기 위한 반복자. 순서가 없다.
//*예시) 모든 요소에 +1을 해야하는 경우
Iterator<Integer> iterator = numbers.iterator();
while (iterator.hasNext()) {
int currentValue = iterator.next();
iterator.remove(); // 현재 요소 삭제
numbers.add(currentValue + 1); // +1을 적용한 값을 추가
}
// HashSet 집합 연산
hs.unionWith(collection); //합집합. 다른 컬렉션의 요소중 중복 안되는 요소를 현재 set에 추가
hs.intersectWith(collecton); //교집합. 현재 set에서 교집합인 부분만 남김.
hs.exceptWith(collection); //차집합. 현재 set에서 다른 collection에 있는 요소를 다 지움.
//HashSet 비교 및 분석
hs.isSubsetOf(collection); //해당 set이 다른 컬렉션의 부분집합인지 확인.
hs.isSupersetOf(collection); //해당 set이 다른 컬렉션의 상위 집합인지 확인.
hs.overlaps(collection); //해당 set이 다른 컬렉션과 중복되는 요소를 가지고 있는지 확인.
hs.setEquals(collection); //해당 set의 요소들과 다른 컬렉션의 요소들이 전부 같은지 확인.
//*참고) 집합 연산과 비교 및 분석이 안될경우
hs.addAll(collection); //합집합
hs.retainAll(collection); //교집합
hs.removeAll(collection); //차집합
hs.containsAll(collection); //해당 set이 다른 컬렉션의 부분집합인지 확인
collection.contatinsAll(hs); //해당 set이 다른 컬렉션의 상위 집합인지 확인(위와 반대경우)
hs.retainAll(collection); //이걸 boolean 형태로 선언하면 다른 컬렉션과 중복요소가 있으면 true, 아니면 false반환.
hs.equals(collection); //해당 set의 요소들과 다른 컬렉션의 요소들이 전부 같은지 확인.
key , value 형태쌍으로 저장된다. HashMap, HashTable 등이 있다.
//HashMap 선언
HashMap <String, String> hs = new HashMap<>(); // <key, value> 순으로 타입 지정. class도 가능.
//HashMap에 요소 추가 및 수정
hs.put("key", "value"); //키-값 쌍을 맵에 저장
hs.putIfAbsent("key", "value") ; //해당 키가 아직 맵에 없는 경우에만 저장
hs.put("key", hs.getOrDefault("key", 0) +1); //해당 키가 있다면 value에 +1, 없다면 1로 새로 저장.
//HashMap 내 요소 제거
hs.remove("key"); //해당 키를 가진 요소 제거
hs.clear(); //맵의 모든 요소 제거
//HashMap 내 요소 검색
hs.get("key"); //지정된 key에 연결되있는 value값을 반환 (해당 key 없으면 null반환)
hs.getOrDefault("key", 0); //지정된 key에 연결되있는 value값을 반환하되, key값이 없으면 0을 반환.
hs.containsKey("key"); //맵에 지정된 key가 존재하는지 여부를 반환
hs.containsValue("value"); //맵에 지정된 value 값이 존재하는지 여부를 반환
//HashMap의 크기 및 상태
hs.size(); //맵에 저장된 key-value 쌍의 갯수를 반환
hs.isEmpty(); //맵이 비어 있는지 여부를 반환
//HashMap의 집합 및 리스트 변환
keySet(); //맵에 있는 모든 key의 집합을 반환
values(); //맵에 있는 모든 value의 컬렉션을 반환
entrySet(); //맵에 있는 모든 key-value 쌍의 집합을 반환 (아래에 예시)
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " = " + value);
}