JCF : Collection

Jamangstangs·2022년 3월 13일
0

Roadmap

목록 보기
7/8
post-thumbnail

Collection

Interface Collection < E >

  • E : Collection에 담겨있는 Element를 의미한다.

  • E -> 하나의 객체를 관리하기 위한 인터페이스이다. 이때, 담기는 Element는 Collection Interface를 무엇으로 구현하는가에 따라 달라진다.

    • 중복을 허용한다 or 중복을 허용하지 않는다
    • 순서대로 담는다 or 순서를 상관하지 않고 담는다
  • Colletion이구현해야하는 메소드는 다음과 같다. 주요한 메소드만 설명하고, 나머지는 출처에 가서 확인하자.

    1. add(E e) : Collection에 E타입의 element를 추가한다.
    2. addAll(Collections<? extends E> c) : Collection에 인자로 전달된 Collection을 추가한다.
    3. remove(Object o) : Object로 직접 명시한 자료를 제거한다.
    4. size() : Collection의 Size를 반환한다.
    5. iterator() : element의 iterator를 반환한다.

    ... 이외에도 구현해야하는 많은 메소드가 있다.

  • Collection의 SubInterface는 다음과 같다.

    1. List < E >
    2. Set < E >
    3. Queue < E >

    우리는 List와 Set에 대해서 후에 알아볼 것이다.

출처 : https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html

List

ArrayList

  • 크기가 가변적으로 변하는 선형리스트
  • Array vs ArrayList
    • Array : 한번 생성되면 크기가 변하지 않는다.
    • ArrayList : 객체를 추가하여 저장 용량을 초과하면, 가변적으로 크기가 늘어난다.
  • 사용법은 아래와 같다.
// Object형의 ArrayList
ArrayList arrayList1 = new ArrayList();
// Generic 사용한 ArrayList
ArrayList<Integer> arrayList2 = new ArrayList<Integer>();
// 용량 지정한 Arraylist
ArrayList<Integer> arrayList3 = new ArrayList<Integer>(100);

Method

  • 추가
ArrayList<Interger> arrayList= new ArrayList();

arrayList.add(1);		// 원하는 값 추가
arrayList.add(2);
arrayList.add(3);

arrayList.add(1,3); // index 지정
  • 제거
ArrayList<Interger> arrayList= new ArrayList();

arrayList.remove(3); // index 지정해서 삭제

arrayList.clear(); 	 // 전체 값 제거
  • 크기
ArrayList<Interger> arrayList= new ArrayList();

System.out.println(arrayList.size())	// 사이즈 출력
  • 출력
ArrayList<Interger> arrayList= new ArrayList();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);

// Iterator로 출력
Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
  System.out.println(iterator.next());
}
// for문으로 출력
for(Interger i : arrayList){
  System.out.println(i);
}
  • 탐색
ArrayList<Interger> arrayList= new ArrayList();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);

// 값으로 탐색
System.out.println(arrayList.contains(3));

// 인덱스로 탐색
System.out.println(arrayList.indexOf(1));

Linked List

  • Node가 줄줄이 연결되어 있는 자료구조이다. Node의 구성은 다음과 같다.
    • Data : Node가 담고있는 자료이다.
    • Next : 현재 node가 가리키고 있는 다음 node이다.
  • 사용법은 아래와 같다.
// Object형의 LinkedList
LinkedList linkedList1 = new LinkedList();
// Generic 사용한 LinkedList
LinkedList<Integer> linkedList2 = new LinkedList<Integer>();
  • ArrayList와 달리, Node로 자료를 구성하였기에 크기에 제약이 없다.

Method

  • 추가
LinkedList<Interger> linkedList= new LinkedList();

linkedList.add(2);				// 마지막에 데이터 추가
linkedList.addFirst(1);	// 맨 앞에 데이터 추가
linkedList.addLast(3);		// 맨 뒤에 데이터 추가

linkedList.add(1,3); 		// index 지정해서 데이터 추가
  • 제거
LinkedList<Interger> linkedList= new LinkedList();

linkedList.remove(); 			// index 0의 값 제거
linkedList.remove(2);			// index 2를 지정하여 값 제거
linkedList.removeFirst(); // 맨 앞의 데이터 제거
linkedList.removeLast(): 	// 맨 뒤의 데이터 제거

linkedList.clear(); 	 		// 전체 값 제거
  • 크기
LinkedList<Interger> linkedList= new LinkedList();

System.out.println(linkedList.size())	// 사이즈 출력
  • 출력
LinkedList<Interger> linkedList= new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);

// Iterator로 출력
Iterator iterator = linkedList.iterator();
while(iterator.hasNext()){
  System.out.println(iterator.next());
}
// for문으로 출력
for(Interger i : linkedList){
  System.out.println(i);
}
  • 탐색
LinkedList<Interger> linkedList= new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);

// 값으로 탐색
System.out.println(linkedList.contains(3));

// 인덱스로 탐색
System.out.println(linkedList.indexOf(1));

Set

HashSet

  • 객체를 중복해서 저장할 수 없다. 중복을 어떻게 제거할까?

    • hashCode() : 각 객체의 주소값을 변환하여 생성한 객체의 고유한 정수값을 반환한다.
    • Obejct.hashCode() -> 해시코드가 나오면, 이를 Set 안에 있는 객체들과 비교한다.
    • 이때, 같은 값이 있으면 저장하지 않고, 없으면 저장한다.

    이러한 메커니즘으로

  • 저장 순서가 유지되지 않는다. -> TreeSet과 차이점

    • TreeSet에서 저장 순서를 유지하는 방법은 이후 TreeSet에서 다루겠다.
  • 사용 방법은 아래와 같다.

// Generic으로 HashSet 선언
HashSet<Integer> hashSet1 = new HashSet<Integer>();
// 용량 지정해서 HashSet 선언
HashSet<Integer> hashSet2 = new HashSet<Integer>(100);
// 용량 지정하고 ,load factor도 지정해준다. 
HashSet<Integer> hashSet2 = new HashSet<Integer>(100, 0.8f);
  • Load Factor : 용량이 load factor로 선언된 비율과 같아지면, 그 시점에서 용량을 2배 증가시킨다. 즉, 용량을 2배로 증가시키는 기준수치가 된다.

Method

  • 추가
HashSet<Integer> hashSet = new HashSet<Integer>();

hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
  • 제거
HashSet<Integer> hashSet = new HashSet<Integer>();
hashSet.add(1);hashSet.add(2);hashSet.add(3);

hashSet.remove(1);	// 1을 제거한다. 
hashSet.clear();		// 모든 값을 제거한다. 
  • 크기
HashSet<Integer> hashSet = new HashSet<Integer>();

System.out.println(hashSet.size());
  • 출력
HashSet<Integer> hashSet = new HashSet<Integer>();

// Iterator로 값을 출력한다. 
Iterator iterator = hashSet.iterator();
while(iterator.hasNext()) {
  System.out.println(iterator.next());
}
  • 탐색
HashSet<Integer> hashSet = new HashSet<Integer>();
hashSet.add(1);hashSet.add(2);hashSet.add(3);

System.out.println(hashSet.contains(3));

TreeSet

  • 객체를 중복해서 저장할 수 없다.

  • 정렬을 지원한다. -> Red-Black Tree 자료구조로 데이터를 저장하기에 가능하다.

  • Red - Black Tree의 원리

  • 사용 방법은 아래와 같다.

// TreeSet 생성
TreeSet<Integer> treeSet = new TreeSet<Interger>();

Method

  • 추가
TreeSet<Integer> treeSet = new TreeSet<Integer>();

treeSet.add(1);
treeSet.add(2);
treeSet.add(3);
  • 제거
TreeSet<Integer> treeSet = new TreeSet<Integer>();
treeSet.add(1);treeSet.add(2);treeSet.add(3);

treeSet.remove(1);	// 1을 제거한다. 
treeSet.clear();		// 모든 값을 제거한다. 
  • 크기
TreeSet<Integer> treeSet = new TreeSet<Integer>();

System.out.println(treeSet.size());
  • 출력
TreeSet<Integer> treeSet = new TreeSet<Integer>();

// Iterator로 값을 출력한다. 
Iterator iterator = treeSet.iterator();
while(iterator.hasNext()) {
  System.out.println(iterator.next());
}

// HashSet과 달리, 정렬을 하기 때문에 아래와 같은 메소드를 지원한다. 
System.out.println(treeSet.first());	// 최소값 출력
System.out.println(treeSet.last());		// 최대값 출력
profile
자망스탕스

0개의 댓글