Java 코드로 TreeMap 정리하기

반응형
728x90
반응형

TreeMap

TreeMap이란, 이진트리를 기반으로 한 Map 컬렉션이다. TreeMap 은 아래와 같이 Key 와 값이 저장된 Map, Entry를 저장한다. 이진 검색트리의 형태로 key-value 쌍으로 이루어진 데이터(Entry)를 저장하는데에 유용하다. HashMap 과 비교하자면, TreeMap 은 범위 검색 또는 정렬 사용에 더 유연하다.

 

TreeMap 에 실행되는 자동 정렬은, 기본적으로 부모 키 값과 비교하여 키 값이 낮은 것은 왼쪽에, 키 값이 높은 것은 오른쪽에 Map.entry 를 저장한다.

 

public static TreeMap<Integer, String> map = new TreeMap<>();

static {
    map.put(1, "ABC");
    map.put(2, "DEF");
}

...

 

  • treeMap의 특정 key 로 value 얻기
Map.Entry<Integer, String> entry = map.get(1); // ABC

 

  • treeMap 기본 출력

출력 결과를 보면 알 수 있듯이, 자동 정렬이 수행된다.

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static TreeMap<Integer, String> mapTest = new TreeMap<>();

    static {
        mapTest.put(1,  "A");
        mapTest.put(5,  "C");
        mapTest.put(10, "E");
        mapTest.put(4,  "B");
        mapTest.put(9,  "D");
    }
    
    public static void main(String[] args) {
         System.out.println(mapTest); // {1=A, 4=B, 5=C, 9=D, 10=E}
    }
}

 

 

 

메서드 정리

1) put : 값을 추가한다.

TreeMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "ABC");
map.put(2, "DEF");

 

2) remove : 값을 삭제한다.

TreeMap<Integer, String> map = new TreeMap<Integer, String>();

map.put(1, "ABC");
map.put(2, "DEF");

map.remove(1); // key 값으로 해당 value 삭제

 

3) get : 값을 가져온다.

TreeMap<Integer, String> map = new TreeMap<Integer, String>();

map.put(1, "ABC");
map.put(2, "DEF");

map.get(1); // ABC

 

4) firstEntry : 최소 Entry 출력

TreeMap<Integer, String> map = new TreeMap<Integer, String>();

map.put(1, "ABC");
map.put(2, "DEF");

map.firstEntry(); // 1=ABC

 

5) firstKey : 최소 Key 출력

TreeMap<Integer, String> map = new TreeMap<Integer, String>();

map.put(1, "ABC");
map.put(2, "DEF");

map.firstKey(); // 1

 

6) lastEntry : 최대 Entry 출력

TreeMap<Integer, String> map = new TreeMap<Integer, String>();

map.put(1, "ABC");
map.put(2, "DEF");

map.lastEntry(); // 2=DEF

 

7) lastKey : 최대 Key 출력 

TreeMap<Integer, String> map = new TreeMap<Integer, String>();

map.put(1, "ABC");
map.put(2, "DEF");

map.lastKey(); // 2

 

 

TreeMap 전체 출력하기

1 : ABC
2 : DEF

위 결과값으로 출력을 예상한다.

 

1) entrySet()

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<Integer, String>();

        map.put(1, "ABC");
        map.put(2, "DEF");

        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

 

2) keySet()

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<Integer, String>();

        map.put(1, "ABC");
        map.put(2, "DEF");

        for (Integer i : map.keySet()) {
            System.out.println(i + " : " + map.get(i));
        }
    }
}

 

3) Iterator ~ entrySet()

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<Integer, String>();

        map.put(1, "ABC");
        map.put(2, "DEF");

        Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();

        while (entries.hasNext()) {
            Map.Entry<Integer, String> entry = entries.next();
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

 

4) Iterator ~ keySet()

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<Integer, String>();

        map.put(1, "ABC");
        map.put(2, "DEF");

        Iterator<Integer> keys = map.keySet().iterator();

        while (keys.hasNext()) {
            int key = keys.next();
            System.out.println(key + " : " + map.get(key));
        }
    }
}

 

 

그 외 메서드 정리

1) ceilingEntry() : 제공된 키 값보다 크거나 같은 값 중 가장 작은 키의 Entry를 반환

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static TreeMap<Integer, String> mapTest = new TreeMap<>();

    static {
        mapTest.put(1,  "A");
        mapTest.put(5,  "C");
        mapTest.put(10, "E");
        mapTest.put(4,  "B");
        mapTest.put(9,  "D");
    }
    
    public static void main(String[] args) {
        System.out.println(mapTest); // {1=A, 4=B, 5=C, 9=D, 10=E}
    
         System.out.println("ceilingEntry() : " + mapTest.ceilingEntry(8)); // 9=D
    }
}

 

2) ceilingKey() : 제공된 키 값보다 크거나 같은 값 중 가장 작은 키의 키값을 반환

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static TreeMap<Integer, String> mapTest = new TreeMap<>();

    static {
        mapTest.put(1,  "A");
        mapTest.put(5,  "C");
        mapTest.put(10, "E");
        mapTest.put(4,  "B");
        mapTest.put(9,  "D");
    }
    
    public static void main(String[] args) {
         System.out.println(mapTest); // {1=A, 4=B, 5=C, 9=D, 10=E}
        
         System.out.println("ceilingKey() : " + mapTest.ceilingKey(8)); // 9
    }
}

 

3) floorEntry() : 제공된 키 값보다 같거나 작은 값 중 가장 큰 키의 Entry를 반환

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static TreeMap<Integer, String> mapTest = new TreeMap<>();

    static {
        mapTest.put(1,  "A");
        mapTest.put(5,  "C");
        mapTest.put(10, "E");
        mapTest.put(4,  "B");
        mapTest.put(9,  "D");
    }
    
    public static void main(String[] args) {
         System.out.println(mapTest); // {1=A, 4=B, 5=C, 9=D, 10=E}
         
         System.out.println("floorEntry() : " + mapTest.floorEntry(8)); // 5=C
    }
}

 

4) floorKey() : 제공된 키 값보다 같거나 작은 값 중 가장 큰 키의 키값을 반환

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    public static TreeMap<Integer, String> mapTest = new TreeMap<>();

    static {
        mapTest.put(1,  "A");
        mapTest.put(5,  "C");
        mapTest.put(10, "E");
        mapTest.put(4,  "B");
        mapTest.put(9,  "D");
    }
    
    public static void main(String[] args) {
         System.out.println(mapTest); // {1=A, 4=B, 5=C, 9=D, 10=E}
         
         System.out.println("floorKey() : " + mapTest.floorKey(8)); // 5
    }
}

 

 

 

반응형

Designed by JB FACTORY