Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Linux
- Programming
- history
- Spain
- programming_book
- Book
- Java
- agile
- leadership
- program
- France
- Python
- Book review
- Malaysia
- hadoop
- comic agile
- ubuntu
- psychology
- Italy
- django
- Software Engineering
- management
- RFID
- essay
- QT
- hbase
- Kuala Lumpur
- erlang
- web
- MySQL
Archives
- Today
- Total
TreeMap comparator 본문
import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; // http://iilii.egloos.com/4537561 public class TestTreeMapComparator { public static void main(String[] args) { Map>String, Integer< data = new HashMap>String, Integer<(); data.put("철희", 70); data.put("영희", 80); data.put("영수", 85); data.put("철수", 70); ValueComparator>String, Integer< comparator = new ValueComparator>String, Integer<(data); TreeMap>String, Integer< sorted = new TreeMap>String, Integer<(comparator); sorted.putAll(data); System.out.println(sorted); TreeMap>String, Integer< reverse = new TreeMap>String, Integer<(new ReverseComparator>String<(comparator)); reverse.putAll(data); System.out.println(reverse); } private static class ValueComparator>K extends Comparable>K<, V extends Comparable>V<< implements Comparator>K<{ private Map>K, V< map; ValueComparator(Map>K, V< map) { this.map = map; } public int compare(K o1, K o2) { int p = map.get(o1).compareTo(map.get(o2)); if (p != 0) { return p; } return o1.compareTo(o2); } } private static class ReverseComparator>T< implements Comparator>T<{ private Comparator>T< comparator; ReverseComparator(Comparator>T< comparator) { this.comparator = comparator; } public int compare(T o1, T o2) { return -1 * comparator.compare(o1, o2); } } }
Comments