Java 컬렉션 프레임워크

LIM JAEHO·2022년 7월 2일
0

Java 학습

목록 보기
17/19

Collection Framework 컬렉션 프레임워크

여러 데이터를 편리하게 관리할 수 있도록 만들어놓은 것이다.
자료구조와 알고리즘이 구조화된 형태이며,
실제로 자료구조 및 알고리즘 문제를 푸는 데 있어서 유용하게 쓰인다.

컬렉션 프레임워크 종류

  • List 인터페이스
    • Abstract List
      • ArrayList
      • LinkedList
  • Set 인터페이스
    • HashSet
    • Sorted Set
      • TreeSet
  • Map 인터페이스
    • HashMap
    • Sorted Map
      • TreeMap

원래 더 많지만 자주 쓰이는 것들은 위와 같다.

List 인터페이스

  • 순서가 있다.
  • 데이터 중복을 허용한다.

ArrayList

  • add
  • get
  • size
  • remove
  • clear
  • sort
  • contains
/* ArrayList */
ArrayList list = new ArrayList();
list1.add(2);
list1.add(1);
list1.add(3);
System.out.println("list1 = " + list1);

ArrayList 의 메서드들

LinkedList

  • add
    • addFirst
    • addLast
  • remove
    • removeFirst
    • removeLast
  • size
  • contains
  • indexOf
  • clear
  • sort
/* LinkedList */
LinkedList list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.addFirst(5);
list.addLast(0);
System.out.println(list);

list.remove(0);
System.out.println(list);
list.remove(Integer.valueOf(1));
System.out.println(list);
list.removeFirst();
System.out.println(list);
list.removeLast();
System.out.println(list);

System.out.println(list.size());
System.out.println(list.contains(3));
System.out.println(list.indexOf(3));

list.clear();

list.add(1);
list.add(3);
list.add(2);
System.out.println(list);

list.sort(Comparator.naturalOrder());
System.out.println(list);

Set 인터페이스

  • 순서가 없다.
  • 중복을 허용하지 않는다.
    HashSet
  • add
  • remove
  • clear
HashSet hSet = new HashSet();

hSet.add(2);
hSet.add(1);
hSet.add(3);
System.out.println(hSet);	// [1, 2, 3] 

hSet.add(1);
System.out.println(hSet);	// [1, 2, 3]

hSet.remove(1);
System.out.println(hSet);	// [2, 3]

hSet.clear();
System.out.println(hSet);	// []

TreeSet
탐색 중 Binary Search 에 특화된 자료구조이다.

  • add
  • remove
  • clear
  • first
  • last
  • lower
  • higher
TreeSet tSet = new TreeSet();

tSet.add(1);
tSet.add(2);
tSet.add(3);
System.out.println(tSet);	// [1, 2, 3]
tSet.remove(2);
System.out.println(tSet);	// [1, 3]
tSet.clear();
System.out.println(tSet);	// []

tSet.add(10);
tSet.add(5);
tSet.add(15);
System.out.println(tSet);				// [5, 10, 15]
System.out.println(tSet.first());		// 5
System.out.println(tSet.last());		// 15
System.out.println(tSet.lower(10));		// 5
System.out.println(tSet.higher(10));	// 15

Map 인터페이스

  • [ 키 : 값 ] 의 쌍으로 이뤄진 데이터 구조이다.
  • 순서가 없다.
  • 키의 중복을 허용하지 않는다.

HashMap

  • put
  • get
  • size
  • remove
  • containsKey
HashMap hMap = new HashMap();
hMap.put(1, "kiwi");
hMap.put(3, "mango");
hMap.put(2, "apple");
System.out.println(hMap);	// {1=kiwi, 2=mango, 3=apple}

hMap.remove(2);
System.out.println(hMap);
System.out.println(hMap.get(1));
HashMap hMap = new HashMap();
hMap.put(1, "kiwi");
hMap.put(3, "apple");
hMap.put(2, "mango");
hMap.put("a", 5);
hMap.put("n", 6);
hMap.put("b", 8);
System.out.println(hMap); // {1=kiwi, a=5, 2=mango, b=8, 3=apple, n=6}

HashMap 의 메서드들

TreeMap

  • put
  • get
  • size
  • remove
  • containsKey
  • firstEntry
  • lastEntry
  • firstKey
  • lastKey
  • LowerEntry
  • higherEntry
System.out.println("=== TreeMap ===");
TreeMap tMap = new TreeMap();

tMap.put(10, "kiwi");
tMap.put(5, "apple");
tMap.put(15, "mango");
System.out.println(tMap);					// {5=apple, 10=kiwi, 15=mango}

System.out.println(tMap.firstEntry());		// 5=apple
System.out.println(tMap.lastEntry());		// 15=mango
System.out.println(tMap.firstKey());		// 5
System.out.println(tMap.lastKey());			// 15
System.out.println(tMap.lowerEntry(10));	// 5=apple
System.out.println(tMap.higherEntry(10));	// 15=mango => 10보다 큰거부터
TreeMap tMap = new TreeMap();
tMap.put("kiwi", 10);
tMap.put("apple", 5);
tMap.put("mango", 15);
System.out.println(tMap);					// {apple=5, kiwi=10, mango=15}

System.out.println(tMap.firstEntry());		// apple=5
System.out.println(tMap.firstKey());		// apple
System.out.println(tMap.lastEntry());		// mango=15
System.out.println(tMap.lastKey());			// mango
System.out.println(tMap.lowerEntry("k"));	// apple=5
System.out.println(tMap.higherEntry("k"));	// kiwi=10 => k부터

중요한 것은 동일한 TreeMap 객체 내에 데이터를 저장할 때는 key 에 들어가는 자료형이 같아야 한다.
위와 같이 문자열도 자동으로 정렬해주는 것을 확인할 수 있다.

0개의 댓글