차곡차곡 성 쌓기

자바 자료구조 문법

Queue<Integer> que = new LinkedList();

// 꺼내기, 없어짐
int cur = que.poll();

// 꺼내기, 안없어짐
int cur = que.peek();

// 넣기
que.add(cur);

우선순위 큐

PriorityQueue<Integer> que = new PriorityQueue();

// 꺼내기, 없어짐
int cur = que.poll();

// 꺼내기, 안없어짐
int cur = que.peek();

// 넣기
que.add(cur);

스택

Stack<Integer> stack = new Stack();

// 꺼내기, 없어짐
int cur = stack.pop();

// 넣기
stack.add(cur);

리스트

List<Integer> list = new ArrayList();

// 맨 뒤에 1 삽입
list.add(1);

// 2 자리에 3삽입, 사이즈가 작으면 에러남
list.add(2, 3);

// 오름차순 정렬
Collections.sort(list);

해쉬맵, 해쉬셋

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

map.put(1, 10);    // 넣기 <1, 10>
map.get(1); // 10
map.containsKey(1) // true

HashSet<String, Integer> set = new HashSet();
set.add(10);

트리맵, 트리셋 - 정렬 되는 set

TreeSet<Integer> set = new TreeSet();

정렬 기준 바꾸고 싶거나 객체를 정렬하고 싶을 때

  • 객체에 Comparable 상속하고, compareTo 오버라이딩

  • 이 방법은 매우 중요하니 별도로 공부해야 함

    class Mirror implements Comparable<Mirror> {
      int r;
      int c;
      int dir;
      int cnt;
    
      public Mirror(int r, int c, int dir, int cnt) {
          this.r = r;
          this.c = c;
          this.dir = dir;
          this.cnt = cnt;
      }
    
      @Override
    //cnt에 대해서 오름차순
      public int compareTo(Mirror o){
          return this.cnt - o.cnt;
      }
    

}
```

profile

차곡차곡 성 쌓기

@nagrang

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!