336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.lgcns.test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
public class TestMap {
HashMap<String,String> strMap;
HashMap<String,Bus> busMap;
public TestMap(){
}
public void doWork(){
init();
// sortStringbyKeyMap();
sortObjectByValueMap();
}
public void init(){
strMap = new HashMap<String,String>();
strMap.put("15", "A"); strMap.put("45", "C"); strMap.put("87", "E"); strMap.put("12", "G"); strMap.put("11", "J");
strMap.put("25", "B"); strMap.put("77", "D"); strMap.put("88", "F"); strMap.put("09", "H"); strMap.put("98", "K");
busMap = new HashMap<String,Bus>();
busMap.put("B01", new Bus("B01","10"));
busMap.put("B02", new Bus("B02","30"));
busMap.put("B03", new Bus("B03","40"));
busMap.put("B04", new Bus("B04","90"));
busMap.put("B05", new Bus("B05","15"));
}
public void sortStringbyKeyMap(){
System.out.println("String map 정렬 전");
printStringMap(strMap);
TreeMap<String,String> treeMap = new TreeMap<String,String>(strMap); // 기본 오름차순 정렬
Iterator<String> iter = treeMap.keySet().iterator();
System.out.println("String map 오름 정렬 후");
while(iter.hasNext()){
String key = (String) iter.next();
System.out.print(key+":"+strMap.get(key)+"\t");
}
System.out.println();
treeMap = new TreeMap<String,String>(Collections.reverseOrder()); // 내림 정렬 방식
treeMap.putAll(strMap);
iter = treeMap.keySet().iterator();
System.out.println("String map 내림 정렬 후");
while(iter.hasNext()){
String key = (String) iter.next();
System.out.print(key+":"+strMap.get(key)+"\t");
}
}
public void printStringMap(HashMap<String,String> map){
Iterator iter = map.keySet().iterator();
while(iter.hasNext()){
String key = (String) iter.next();
System.out.print(key+":"+map.get(key)+"\t");
}
System.out.println();
}
public void sortObjectByValueMap(){
System.out.println("Object map 정렬 전");
printObjectMap(busMap);
List<String> list = new ArrayList();
list.addAll(busMap.keySet());
Collections.sort(list,new Comparator(){
public int compare(Object o1,Object o2) {
Object v1 = busMap.get(o1);
Object v2 = busMap.get(o2);
return ((Comparable) v2).compareTo(v1);
}
});
Collections.reverse(list);
System.out.println();
System.out.println("Object map 정렬 후");
System.out.println(list.toString());
}
public void printObjectMap(HashMap<String,Bus> map){
Iterator iter = map.keySet().iterator();
while(iter.hasNext()){
String key = (String) iter.next();
System.out.print(map.get(key).toString());
}
System.out.println();
}
public static void main(String[] args){
new TestMap().doWork();
}
}
class Bus implements Comparable<Bus>{
String name,location;
public Bus(String name, String location){
this.name = name;
this.location = location;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Bus [name=" + name + ", location=" + location + "]";
}
@Override
public int compareTo(Bus arg0) {
// TODO Auto-generated method stub
if (this.location.compareTo(arg0.getLocation()) < 0) {
return -1;
} else if (this.location.compareTo(arg0.getLocation()) > 0) {
return 1;
}
return 0;
}
// map value에 의한 정렬
/*
* //메인메소드에서 구현
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("a",3);
map.put("b",12);
map.put("c",54);
map.put("d",51);
map.put("e",8);
System.out.println("------------sort 전 -------------");
Iterator iterator = map.keySet().iterator();
while(iterator.hasNext {
String temp = (String) iterator.next();
System.out.println(temp + " = " + map.get(temp));
}
Iterator it = sortByValue(map).iterator();
System.out.println("------------sort 후 -------------");
while(it.hasNext()) {
String temp = (String) it.next();
System.out.println(temp + " = " + map.get(temp));
}
//별도의 스태틱 함수로 구현
public static List sortByValue(final Map map) {
List<String> list = new ArrayList();
list.addAll(map.keySet());
Collections.sort(list,new Comparator() {
public int compare(Object o1,Object o2) {
Object v1 = map.get(o1);
Object v2 = map.get(o2);
return ((Comparable) v2).compareTo(v1);
}
});
Collections.reverse(list); // 주석시 오름차순
return list;
}
*
* */
}