여러 데이터를 편리하게 관리할 수 있도록 만들어놓은 것이다.
자료구조와 알고리즘이 구조화된 형태이며,
실제로 자료구조 및 알고리즘 문제를 푸는 데 있어서 유용하게 쓰인다.
ArrayList
LinkedList
HashSet
TreeSet
HashMap
TreeMap
원래 더 많지만 자주 쓰이는 것들은 위와 같다.
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);
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);
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
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}
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 에 들어가는 자료형이 같아야 한다.
위와 같이 문자열도 자동으로 정렬해주는 것을 확인할 수 있다.