일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- #3차원배열
- border-box
- #bubbleSort
- #성적관리프로그램
- html multimedia
- html id
- mac terminal command
- 토큰경제
- html5 new tag
- #C++ has~a
- docker example
- relative path
- #1차원배열
- 하이퍼레저패브릭
- git flow
- #android activity
- #2차원배열
- html object
- #다차원포인터
- hyperledger transaction
- html charset
- #JAVASCRIPT
- #CallByAddress
- html code
- html plug-in
- #자바상속#자바이즈어#is~a
- html youtube
- #binary
- #C++ 연산자함수오버로딩
- html video
- Today
- Total
A sentimental robot
Map 본문
import java.util.TreeMap;
public class CTreeMap {
public static void main(String[] args) {
TreeMap<String, Integer> tm = new TreeMap<>();
tm.put("C", 100);
tm.put("A", 90);
tm.put("B", 40);
System.out.println(tm);
}
}
TreeMap 정렬 출력
import java.util.Iterator;
import java.util.TreeMap;
public class CTreeMap2 {
public static void main(String[] args) {
TreeMap<Character, Character> tm = new TreeMap<>();
for (int i = 25; i >= 0 ; i--) {
char ch = (char) (65 + i);
tm.put(ch, ch);
}
for (Iterator<Character> itr = tm.values().iterator(); itr.hasNext();) {
System.out.print(tm.get(itr.next()) + " ");
}
}
}
검색메소드
import java.util.TreeMap;
public class CTreeMap3 {
public static void main(String[] args) {
TreeMap<String, Integer> tm = new TreeMap<>();
tm.put("kor", 100);
tm.put("eng", 90);
tm.put("mat", 40);
if (tm.containsKey("kor") == true) {
System.out.printf("kor exists");
} else {
System.out.println("none");
}
if (tm.containsKey("scien") == true) {
System.out.printf("scien exists");
} else {
System.out.println("\n none");
}
}
}
추가
import java.util.TreeMap;
public class CTreeMap4 {
public static void main(String[] args) {
TreeMap<String, Integer> tm = new TreeMap<>();
tm.put("kor", 100);
tm.put("eng", 90);
tm.put("mat", 40);
for (String iter : tm.keySet()) {
System.out.printf("\n %s : %d", iter, tm.get(iter));
}
if (tm.containsKey("scien") == true) {
System.out.printf("scien exists");
} else {
System.out.println("\n none");
}
// scien 추가
System.out.println("==science score 추가==" + "");
tm.put("scien", 50);
for (String iter : tm.keySet()) {
System.out.printf("\n %s :%d", iter, tm.get(iter));
}
}
}
수정
import java.util.TreeMap;
public class CTreeMap5 {
public static void main(String[] args) {
TreeMap<String, Integer> tm = new TreeMap<>();
tm.put("kor", 100);
tm.put("eng", 90);
tm.put("mat", 40);
for (String iter : tm.keySet()) {
System.out.printf("\n %s : %d", iter, tm.get(iter));
}
System.out.println("\n=== 수정후=====");
// kor 수정
tm.put("kor", 50);
for (String iter : tm.keySet()) {
System.out.printf("\n %s :%d", iter, tm.get(iter));
}
}
}
오름차순 정렬
port java.util.TreeMap;
public class CTreeMap6 {
public static void main(String[] args) {
TreeMap<String, Integer> tm = new TreeMap<>();
tm.put("kor", 100);
tm.put("eng", 90);
tm.put("mat", 40);
for (String iter : tm.keySet()) {
System.out.printf("\n %s : %d", iter, tm.get(iter));
}
System.out.println("\n==점수 순서대로 정렬==");
TreeMap<Integer, String> rank = new TreeMap<Integer, String>();
for (String iter : tm.keySet()) {
rank.put(tm.get(iter), iter);
}
for (Integer iter : rank.keySet()) {
System.out.printf("\n %d : %s", iter, rank.get(iter));
}
}
}
내림차순 정렬
import java.util.TreeMap;
public class CTreeMap7 {
public static void main(String[] args) {
TreeMap<String, Integer> tm = new TreeMap<>();
tm.put("kor", 100);
tm.put("eng", 90);
tm.put("mat", 40);
for (String iter : tm.keySet()) {
System.out.printf("\n %s : %d", iter, tm.get(iter));
}
System.out.println("\n==점수 높은순서대로 정렬==");
TreeMap<Integer, String> top = new TreeMap<Integer, String>();
for (String iter : tm.keySet()) {
top.put(tm.get(iter), iter);
}
for (Integer iter : top.descendingKeySet()) {
System.out.printf("\n %d : %s", iter, top.get(iter));
}
}
}
삭제 및 초기화
import java.util.TreeMap;
public class CTreeMap8 {
public static void main(String[] args) {
TreeMap<String, Integer> tm = new TreeMap<>();
tm.put("kor", 100);
tm.put("eng", 90);
tm.put("mat", 40);
for (String iter : tm.keySet()) {
System.out.printf("\n %s : %d", iter, tm.get(iter));
}
System.out.println("\n==kor 삭제==");
tm.remove("kor");
for (String iter : tm.keySet()) {
System.out.printf("\n %s : %d", iter, tm.get(iter));
}
System.out.printf("\n Size : %d", tm.size());
System.out.println("\n==전체 초기화==");
tm.clear();
for (String iter : tm.keySet()) {
System.out.printf("\n %s : %d", iter, tm.get(iter));
}
System.out.printf("\n size : %d", tm.size());
}
}
'Java' 카테고리의 다른 글
InetAddress Class (0) | 2017.12.29 |
---|---|
Map을 이용한 성적관리 (0) | 2017.12.29 |
Exception Handling (0) | 2017.12.29 |
Thread Synchronization (0) | 2017.12.29 |
Thread (0) | 2017.12.29 |