본문 바로가기

Algorithm

TreeSet : 트리에서 인덱스 번호 반환, 범위 값출력

반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package LineSweep;
 
 
public class TreesetTest {
    public static void main(String[] args) {
        TreeSet<Integer> set = new TreeSet<Integer>();
        
        set.add(5);
        set.add(1);
        set.add(7);
        set.add(4);
        set.add(9);
        set.add(0);
        
        //TreeSet 의 인덱스번호를 반환하는 방법
        System.out.println("인덱스 번호 반환");
        System.out.println(set.headSet(1).size());
        System.out.println(set.headSet(4).size());
        System.out.println(set.headSet(9).size());
        
        System.out.println("subSet");
        for(Integer tmp : set.subSet(16)) {
            System.out.println(tmp);
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
set.headSet 어떤 특정 값 보다 아래에 있는 값들을 반환한다.
headSet(Object,boolean) 형태이다 boolean 이 참이라면 현재값 포함이고 false라면 미만이다.
따라서 1미만값들을 반환하게되면 0하나만 반환하게 되고 size =1 이다 그게 Index 번호가 된다.

또한 subSet을 사용하게되면 Object 와 Object 사이에잇는값들 모두 반환한다. 
반응형

'Algorithm' 카테고리의 다른 글

파라메트릭  (0) 2020.03.13
JAVA : nextPermutation 다음순열  (2) 2020.03.12
Line Sweep 알고리즘 (백준 : 2261) JAVA  (0) 2020.03.06
Upper Bound, Lower Bound  (0) 2020.03.06
JAVA : LinkedList , ArrayList 순환도중에 값 삭제하기  (0) 2020.03.06