Collection - Tree

TreeSet

์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœํ•œ Set Collection์ด๋‹ค. TreeSet์— ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ •๋ ฌ๋˜๋Š”๋ฐ ์ด์ง„ํƒ์ƒ‰ํŠธ๋ฆฌ์ฒ˜๋Ÿผ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ’๊ณผ ๋น„๊ตํ•ด ๋‚ฎ์œผ๋ฉด ์™ผ์ชฝ ์ž์‹๋…ธ๋“œ, ๋†’์œผ๋ฉด ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ์— ์ €์žฅํ•œ๋‹ค.

TreeSet<E> treeSet = new TreeSet<E>();

๋ฆฌํ„ดํƒ€์ž…

๋ฉ”์†Œ๋“œ

์„ค๋ช…

E

first()

์ œ์ผ ๋‚ฎ์€ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด

E

last()

์ œ์ผ ๋†’์€ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด

E

lower(E e)

์ฃผ์–ด์ง„ ๊ฐ์ฒด๋ณด๋‹ค ๋ฐ”๋กœ ์•„๋ž˜ ๊ฐ์ฒด ๋ฆฌํ„ด

E

higher(E e)

์ฃผ์–ด์ง„ ๊ฐ์ฒด๋ณด๋‹ค ๋ฐ”๋กœ ์œ„ ๊ฐ์ฒด ๋ฆฌํ„ด

E

floor(E e)

์ฃผ์–ด์ง„ ๊ฐ์ฒด์™€ ๋™๋“ฑํ•œ ๊ฐ์ฒด๊ฐ€ ์žˆ์œผ๋ฉด ๋ฆฌํ„ด, ๋งŒ์•ฝ ์—†๋‹ค๋ฉด ๋ฐ”๋กœ ์•„๋ž˜์˜ ๊ฐ์ฒด ๋ฆฌํ„ด

E

ceiling(E e)

์ฃผ์–ด์ง„ ๊ฐ์ฒด์™€ ๋™๋“ฑํ•œ ๊ฐ์ฒด๊ฐ€ ์žˆ์œผ๋ฉด ๋ฆฌํ„ด, ๋งŒ์•ฝ ์—†๋‹ค๋ฉด ๋ฐ”๋กœ ์œ„์˜ ๊ฐ์ฒด ๋ฆฌํ„ด

E

pollFirst()

์ œ์ผ ๋‚ฎ์€ ๊ฐ์ฒด ๊บผ๋‚ด์˜ค๊ณ  Collection์—์„œ ์ œ๊ฑฐ

E

pollLst()

์ œ์ผ ๋†’์€ ๊ฐ์ฒด ๊บผ๋‚ด์˜ค๊ณ  Collection์—์„œ ์ œ๊ฑฐ

TreeSet<Integer> scores = new TreeSet<Integer>();
scores.add(new Integer(87));
scores.add(new Integer(95));
scores.add(new Integer(90));

System.out.println("first() : "+ scores.first());

๋ฆฌํ„ดํƒ€์ž…

๋ฉ”์†Œ๋“œ

์„ค๋ช…

Iterator\

descendingIterator()

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ Iterator ๋ฆฌํ„ด

NavigableSet\

descendingSet()

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ navigableSet ๋ฆฌํ„ด

NavigableSet<Integer> descendingSet = treeSet.descendingSet();

๋งŒ์•ฝ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, descendingSet()์„ ๋‘๋ฒˆ ํ˜ธ์ถœํ•˜๋ฉด๋œ๋‹ค.

NavigableSet<Integer> ascendingSet = descendingSet.descendingSet();

๋ฆฌํ„ดํƒ€์ž…

๋ฉ”์†Œ๋“œ

์„ค๋ช…

NavigableSet\

headSet( E toElement, boolean inclusive)

์ฃผ์–ด์ง„ ๊ฐ์ฒด๋ณด๋‹ค ๋‚ฎ์€ ๊ฐ์ฒด๋“ค์„ NavigableSet์œผ๋กœ ๋ฆฌํ„ด ์ฃผ์–ด์ง„ ๊ฐ์ฒด ํฌํ•จ ์—ฌ๋ถ€๋Š” ๋‘ ๋ฒˆ์งธ boolean๊ฐ’์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.

NavigableSet\

tailSet( E fromElement, boolean inclusive)

์ฃผ์–ด์ง„ ๊ฐ์ฒด๋ณด๋‹ค ํฐ ๊ฐ์ฒด๋“ค์„ NavigableSet์œผ๋กœ ๋ฆฌํ„ด ์ฃผ์–ด์ง„ ๊ฐ์ฒด ํฌํ•จ ์—ฌ๋ถ€๋Š” ๋‘ ๋ฒˆ์งธ boolean๊ฐ’์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.

NavigableSet\

subSet( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

์‹œ์ž‘๊ณผ ๋์œผ๋กœ ์ฃผ์–ด์ง„ ๊ฐ์ฒด ์‚ฌ์ด์˜ ๊ฐ์ฒด๋“ค์„ NavigableSet์œผ๋กœ ๋ฆฌํ„ด

TreeSet<String> treeSet = new TreeSet<String>();
treeSet.add("banana");
treeSet.add("apple");
treeSet.add("grape");
treeSet.add("cherry");
treeSet.add("strawberry");

NavigableSet<String> rangeSet = treeSet.subSet("c", true, "f", true);
for(String word : rangeSet){
  System.out.println(word); // cherry
}

TreeMap

TreeMap์€ ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ Map Collection์ด๋‹ค. TreeSet๊ณผ์˜ ์ฐจ์ด์ ์€ ํ‚ค์™€ ๊ฐ’์ด ์ €์žฅ๋œ Map.Entry๋ฅผ ์ €์žฅํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ํ‚ค๊ฐ’์„ ๋น„๊ตํ•ด์„œ ์ž๋™์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.

TreeMap<K, V> treeMap = new TreeMap<K, V>();

Map ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž… ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•ด๋„๋˜์ง€๋งŒ TreeMap ํด๋ž˜์Šค ํƒ€์ž…์œผ๋กœ ๋Œ€์ž…ํ•œ ์ด์œ ๋Š” ํŠน์ • ๊ฐ์ฒด๋ฅผ ์ฐพ๊ฑฐ๋‚˜ ๋ฒ”์œ„ ๊ฒ€์ƒ‰๊ณผ ๊ด€๋ จ๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค.

๋ฆฌํ„ดํƒ€์ž…

๋ฉ”์†Œ๋“œ

์„ค๋ช…

Map.Entry

firstEntry()

์ œ์ผ ๋‚ฎ์€ Map.Entry ๋ฆฌํ„ด

Map.Entry

lastEntry()

์ œ์ผ ๋†’์€ Map.Entry ๋ฆฌํ„ด

Map.Entry

lowerEntry(K key)

์ฃผ์–ด์ง„ ํ‚ค๋ณด๋‹ค ๋ฐ”๋กœ ์•„๋ž˜ Map.Entry ๋ฆฌํ„ด

Map.Entry

higherEntry(K key)

์ฃผ์–ด์ง„ ํ‚ค๋ณด๋‹ค ๋ฐ”๋กœ ์œ„ Map.Entry ๋ฆฌํ„ด

Map.Entry

flooreEntry(K key)

์ฃผ์–ด์ง„ ํ‚ค์™€ ๊ฐ™์€ ํ‚ค๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹น Map.Entry ๋ฆฌํ„ด ์—†๋‹ค๋ฉด ์ฃผ์–ด์ง„ ํ‚ค ๋ฐ”๋กœ ์•„๋ž˜ Map.Entry ๋ฆฌํ„ด

Map.Entry

ceilingEntry(K key)

์ฃผ์–ด์ง„ ํ‚ค์™€ ๊ฐ™์€ ํ‚ค๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹น Map.Entry ๋ฆฌํ„ด ์—†๋‹ค๋ฉด ์ฃผ์–ด์ง„ ํ‚ค ๋ฐ”๋กœ ์œ„ Map.Entry ๋ฆฌํ„ด

Map.Entry

pollFirstEntry()

์ œ์ผ ๋‚ฎ์€ Map.Entry ๋ฆฌํ„ดํ•˜๊ณ  Collection์—์„œ ์ œ๊ฑฐ

Map.Entry

pollLastEntry()

์ œ์ผ ๋†’์€ Map.Entry ๋ฆฌํ„ดํ•˜๊ณ  Collection์—์„œ ์ œ๊ฑฐ

๋ฆฌํ„ดํƒ€์ž…

๋ฉ”์†Œ๋“œ

์„ค๋ช…

NavigableSet\

descendingKeySet()

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ํ‚ค์˜ NavigableSet ๋ฆฌํ„ด

NavigableMap\

descendingMap()

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ Map.Entry์˜ NavigableSet ๋ฆฌํ„ด

์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, descendingMap()์„ ๋‘๋ฒˆ ํ˜ธ์ถœํ•˜๋ฉด๋œ๋‹ค.

๋ฆฌํ„ดํƒ€์ž…

๋ฉ”์†Œ๋“œ

์„ค๋ช…

NavigableMap\

headMap( K toKey, boolean inclusive)

์ฃผ์–ด์ง„ ํ‚ค๋ณด๋‹ค ๋‚ฎ์€ Map.Entry๋ฅผ NavigableMap์œผ๋กœ ๋ฆฌํ„ด ์ฃผ์–ด์ง„ ๊ฐ์ฒด ํฌํ•จ ์—ฌ๋ถ€๋Š” ๋‘ ๋ฒˆ์งธ boolean๊ฐ’์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.

NavigableMap\

tailMap( K fromKey, boolean inclusive)

์ฃผ์–ด์ง„ ํ‚ค๋ณด๋‹ค ๋†’์€ Map.Entry๋ฅผ NavigableMap์œผ๋กœ ๋ฆฌํ„ด ์ฃผ์–ด์ง„ ๊ฐ์ฒด ํฌํ•จ ์—ฌ๋ถ€๋Š” ๋‘ ๋ฒˆ์งธ boolean๊ฐ’์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.

NavigableMap\

subMap( K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)

์‹œ์ž‘๊ณผ ๋ ์‚ฌ์ด์˜ Map.Entry๋ฅผ NavigableMap์œผ๋กœ ๋ฆฌํ„ด

TreeMap<String,Integer> TreeMap = new TreeMap<String,Integer>();
TreeMap.put("banana", new Integer(10));
TreeMap.put("apple", new Integer(50));
TreeMap.put("grape", new Integer(30));
TreeMap.put("cherry", new Integer(20));
TreeMap.put("strawberry", new Integer(40));

NavigableMap<String,Integer> rangeMap = TreeMap.subMap("c", true, "f", true);
for(Map.Entry<String,Integer> entry : rangeMap.entrySet()){
    System.out.println(entry.getKey()); // cherry
}

Comparable, Comparator

TreeSet๊ณผ TreeMap์€ ์ •๋ ฌ์„ ์œ„ํ•ด java.lang.Comparable ์„ ๊ตฌํ˜„ํ•œ ๊ฐ์ฒด๋ฅผ ์š”๊ตฌํ•œ๋‹ค. ๋งŒ์•ฝ Comparable์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ์ €์žฅํ•˜๋Š” ์ˆœ๊ฐ„ ClassCastException์ด ๋ฐœ์ƒํ•œ๋‹ค. ์ด๋•Œ, ์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ ๊ฐ’์œผ๋กœ Comparator๋ฅผ ์ œ๊ณตํ•˜๋ฉด Comparable์ด ์—†๋Š” ๊ฐ์ฒด๋„ ์ •๋ ฌ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

ํ˜น์€ ์‚ฌ์šฉ์ž ์ •์˜ ํด๋ž˜์Šค์—์„œ compareTo() ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๋ฉด๋œ๋‹ค.

Comparable

public class Fruit implemets Comparable<Fruit>{
  public String name;
  public Integer stock;
  ...
  @Override
  public int compareTo(Fruit o){
    // ๋กœ์ง
  }
}

Comparator

public class DescendingComparator implements Comparator<Fruit>{
  @Override
  public int compare(Fruit o1, Fruit o2){
    // ๋กœ์ง
  }
}
TreeSet<Fruit> treeSet = new TreeSet<Fruit>(new DescendingComparator())

Last updated