Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. class FontAlphabetizer
  2. implements Comparator<Font> {
  3. @Override
  4. public int compare(Font font1, Font font2) {
  5. return font1.getName().compareTo(font2.getName());
  6. }
  7. }
  8.  
  9. class MapValueComparator<K, V extends Comparable<V>>
  10. implements Comparator<Map<K, V>> {
  11. final K key;
  12.  
  13. MapValueComparator(K key) {
  14. this.key = key;
  15. }
  16.  
  17. @Override
  18. public int compare(Map<K, V> map1, Map<K, V> map2) {
  19. return map1.get(key).compareTo(map2.get(key));
  20. }
  21. }
  22.  
  23. static void sortByTown(List<Map<String, String>> list) {
  24. Collections.sort(list, new MapValueComparator<String, String>("town"));
  25. }
  26.  
  27. class PopulationComparator
  28. implements Comparator<Map<String, String>> {
  29. @Override
  30. public int compare(Map<String, String> map1, Map<String, String> map2) {
  31. final Long pop1 = Long.valueOf(map1.get("population"));
  32. final Long pop2 = Long.valueOf(map2.get("population"));
  33.  
  34. return pop1.compareTo(pop2);
  35. }
  36. }
  37.  
  38. public class CustomComparator implements Comparator<MyObjectType>
  39. {
  40. public int compare(MyObjectType ob1 , MyObjectType ob2)
  41. {
  42. //code to compare the 2 objects
  43. }
  44. }
  45.  
  46. final List<Map<String, Object>> towns = new ArrayList<Map<String, Object>>();
  47.  
  48. final Map<String, Object> toronto = new HashMap<String, Object>();
  49. toronto.put("town", "Toronto");
  50. toronto.put("population", 2500000);
  51. toronto.put("age", 147);
  52. towns.add(toronto);
  53.  
  54. final Map<String, Object> ottawa = new HashMap<String, Object>();
  55. ottawa.put("town", "Ottawa");
  56. ottawa.put("population", 883000);
  57. ottawa.put("age", 159);
  58. towns.add(ottawa);
  59.  
  60. final Map<String, Object> montreal = new HashMap<String, Object>();
  61. montreal.put("town", "Montreal");
  62. montreal.put("population", 1600000);
  63. montreal.put("age", 372);
  64. towns.add(montreal);
  65.  
  66. final Map<String, Object> quebec = new HashMap<String, Object>();
  67. quebec.put("town", "Quebec City");
  68. quebec.put("population", 600000);
  69. quebec.put("age", 406);
  70. towns.add(quebec);
  71.  
  72. final Map<String, Object> vancouver = new HashMap<String, Object>();
  73. vancouver.put("town", "Vancouver");
  74. vancouver.put("population", 600000);
  75. vancouver.put("age", 128);
  76. towns.add(vancouver);
  77.  
  78. Collections.sort(towns, new Comparator<Map<String, Object>>() {
  79. @Override
  80. public int compare(final Map<String, Object> o1, final Map<String, Object> o2) {
  81. if (o1.get("population") instanceof Integer && o2.get("population") instanceof Integer && !((Integer)o1.get("population")).equals((Integer)o2.get("population"))) {
  82. return ((Integer)o1.get("population")).compareTo((Integer)o2.get("population"));
  83. }
  84. if (o1.get("age") instanceof Integer && o2.get("age") instanceof Integer) {
  85. return ((Integer)o1.get("age")).compareTo((Integer)o2.get("age"));
  86. }
  87. // Default if there is no population/no age, shouldn't happen.
  88. // TODO : do something else.
  89. return o1.toString().compareTo(o2.toString());
  90. }
  91. });
  92.  
  93. for (final Map<String, Object> town: towns) {
  94. System.out.println(town.get("population")+"t"+town.get("age")+"t"+town.get("town"));
  95. }
  96.  
  97. 600000 128 Vancouver
  98. 600000 406 Quebec City
  99. 883000 159 Ottawa
  100. 1600000 372 Montreal
  101. 2500000 147 Toronto
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement