Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String[] args) {
- Map<Long,Double> map = new LinkedHashMap<Long, Double>();
- map.put(1L, 3.0);
- map.put(2L, 1.0);
- map.put(3L, 4.0);
- map.put(4L, 2.0);
- List<Map.Entry<Long, Double>> entriesOld = new ArrayList<Map.Entry<Long, Double>>(map.entrySet());
- Collections.sort(entriesOld, new Old.ValueComparator());
- // entriesOld = [3=4.0, 1=3.0, 4=2.0, 2=1.0]
- System.out.println("entriesOld = " + entriesOld);
- List<Map.Entry<Long, Double>> entriesNew = new ArrayList<Map.Entry<Long, Double>>(map.entrySet());
- Collections.sort(entriesNew, new New.ValueComparator());
- // entriesNew = [3=4.0, 1=3.0, 4=2.0, 2=1.0]
- System.out.println("entriesNew = " + entriesNew);
- Assert.assertEquals(entriesNew, entriesOld); // ok
- }
- // Implementation before
- public static class Old {
- public static class ValueComparator implements Comparator{
- public ValueComparator() {
- }
- public int compare(Object a, Object b) {
- if( (Double)(((Map.Entry)(a)).getValue()) < ((Double)((Map.Entry) (b)).getValue()) ) {
- return 1;
- } else if((Double)(((Map.Entry)(a)).getValue()) == (Double)(((Map.Entry)(b)).getValue())) {
- return 0;
- } else {
- return -1;
- }
- }
- }
- }
- // Implementation after
- public static class New {
- public static class ValueComparator implements Comparator<Map.Entry<Long, Double>> {
- public int compare(Map.Entry<Long, Double> a, Map.Entry<Long, Double> b) {
- return Double.compare(b.getValue(), a.getValue());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement