A sentimental robot

Map 본문

Java

Map

GOD03219 2017. 12. 29. 13:56

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