일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- 추상화
- Java
- process.env
- git
- 서브셋폰트
- 디자인 패턴
- PostgreSQL
- azure
- 상속
- dfs
- 다형성
- npm
- netlify
- dotenv
- Secret
- github
- MariaDB
- 클라우드
- 동적계획법
- 캡슐화
- 메모이제이션
- AOP
- mock
- bfs
- 객체지향
- GOF
- CSS
- Solid
- DP
- package
Archives
- Today
- Total
이것저것 해보기🌼
[자료구조] List, Map, Set, Stack, Queue 연습 (JAVA) 본문
기본적인 자료구조 다루기 연습
(Collections 를 사용한 sort)
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.ArrayList; import java.util.Collections; import java.util.Stack; import java.util.Queue; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.HashSet; import java.util.TreeSet; import java.util.Iterator; public class Main { public static void main(String[] args) { // List List <Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.remove(2); for(int i=0 ;i < list.size(); i++) System.out.print(list.get(i)+" "); //1 2 // HashMap HashMap <Integer, String> map = new HashMap<>(); map.put(26, "AAA"); map.put(28, "CCC"); map.put(29, "BBB"); System.out.println("\n"+ map.getOrDefault(30, "NO")); //NO for(Map.Entry<Integer, String> entry: map.entrySet()) { System.out.println(entry.getKey() +" " + entry.getValue()); } // 26 AAA // 28 CCC // 29 BBB List<Integer> keylist = new ArrayList<>(map.keySet()); List<String> vallist = new ArrayList<>(map.values()); System.out.println(keylist); //26 28 29 System.out.println(vallist); //AAA CCC BBB System.out.println(keylist.contains(25)); //false //sort, max, min System.out.println(Collections.max(keylist)); //29 System.out.println(Collections.min(vallist)); //AAA Collections.sort(vallist); System.out.println(vallist); //AAA BBB CCC //Stack Stack <Integer> stack = new Stack<>(); stack.push(1); stack.push(5); stack.push(3); System.out.println(stack); //1, 5, 3 System.out.println(stack.peek()); //3 stack.pop(); System.out.println(stack.pop()); //5 System.out.println(stack); //1 for(int i=0; i< stack.size(); i++) { System.out.println(stack.get(i)); } //1 //Queue Queue<Integer> queue = new LinkedList<>(); queue.add(1); queue.add(5); queue.add(3); System.out.println(queue); //1, 5, 3 System.out.println(queue.remove()); //1 System.out.println(queue.remove()); //5 //PriorityQueue PriorityQueue<Integer> pq = new PriorityQueue<>(); //우선순위 : 낮은 숫자 순 pq.add(30); pq.add(50); pq.add(10); pq.add(20); pq.remove(); //10 제거 System.out.println(pq.peek()); //20 System.out.println(pq.remove()); //20 System.out.println(pq.remove()); //30 System.out.println(pq.remove()); //50 //PriorityQueue<Integer> rpq = new PriorityQueue<>(Collections.reverseOrder()); //우선순위 : 높은 숫자 순 //HashSet HashSet<Integer> set = new HashSet<>(); set.add(3); set.add(4); set.add(1); set.add(5); set.add(2); Iterator<Integer> it = set.iterator(); while(it.hasNext()) { System.out.print(it.next()+" "); //순서없으므로 있는 원소 전부 출력 } System.out.println(); //hash set 정렬 List<Integer> setsort = new ArrayList<>(set); Collections.sort(setsort); System.out.println(setsort); // 1 2 3 4 5 Collections.reverse(setsort); //내림차순 정렬 System.out.println(setsort); // 5 4 3 2 1 System.out.println(setsort.contains(3)); //true TreeSet<Integer> ts = new TreeSet<>(); //오름차순 ts.add(3); ts.add(2); ts.add(10); ts.add(7); for(Integer i:ts) { System.out.print(i+" "); //2 3 7 10 } System.out.println(); } } | cs |
'코딩테스트' 카테고리의 다른 글
DP (dynamic programming) 연습 (0) | 2022.09.07 |
---|---|
[프로그래머스] 송전탑 분할 (0) | 2021.07.11 |
[백준 온라인 저지] 1991번: 트리 순회 (0) | 2021.07.02 |
[SQL] postgreSQL 문법 정리(CASE,JOIN,GROUP BY등) (0) | 2021.07.02 |
[DFS/BFS/백트래킹] 참고자료, 코드 (0) | 2021.07.02 |