Advertisement
Guest User

Sort comparison

a guest
Jan 14th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package by.defascat.saved;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import java.util.Random;
  8. import java.util.stream.Collectors;
  9.  
  10. /**
  11.  *
  12.  * @author andy
  13.  */
  14. public class SortingEx {
  15.  
  16.     public static void main(String[] args) {
  17.         List<O> al = new ArrayList<>();
  18.         List<O> al2 = new ArrayList<>();
  19.         new Random().ints(1000000).forEach(i -> al.add(new O(i)));
  20.         al2.addAll(al);
  21.  
  22.         long time1 = System.currentTimeMillis();
  23.         al.stream().sorted(Comparator
  24.                 .comparing(O::getA)
  25.                 .thenComparing(O::getB)
  26.                 .thenComparing(O::getC)
  27.                 .thenComparing(O::getD)
  28.         )
  29.                 .collect(Collectors.toList());
  30.         long time2 = System.currentTimeMillis();
  31.         al.stream().sorted(Comparator
  32.                 .comparingInt(O::getA)
  33.                 .thenComparingInt(O::getB)
  34.                 .thenComparingInt(O::getC)
  35.                 .thenComparingInt(O::getD)
  36.         )
  37.                 .collect(Collectors.toList());
  38.  
  39.         long time3 = System.currentTimeMillis();
  40.  
  41.         Collections.sort(al2);
  42.  
  43.         long time4 = System.currentTimeMillis();
  44.  
  45.         System.out.println("Time 1: " + (time2 - time1) + "; Time 2: " + (time3 - time2) + "; Time 3: " + (time4 - time3));
  46.     }
  47.  
  48.     static class O implements Comparable<O> {
  49.  
  50.         private final int a;
  51.         private final int b;
  52.         private final int c;
  53.         private final int d;
  54.  
  55.         public O(int i) {
  56.             a = i % 2;
  57.             b = i % 10;
  58.             c = i % 100;
  59.             d = i;
  60.         }
  61.  
  62.         public int getA() {
  63.             return a;
  64.         }
  65.  
  66.         public int getB() {
  67.             return b;
  68.         }
  69.  
  70.         public int getC() {
  71.             return c;
  72.         }
  73.  
  74.         public int getD() {
  75.             return d;
  76.         }
  77.  
  78.         @Override
  79.         public int compareTo(O o) {
  80.             int comp = Integer.compare(getA(), o.getA());
  81.             if (comp == 0) {
  82.                 comp = Integer.compare(getB(), o.getB());
  83.                 if (comp == 0) {
  84.                     comp = Integer.compare(getC(), o.getC());
  85.                     if (comp == 0) {
  86.                         comp = Integer.compare(getD(), o.getD());
  87.                     }
  88.                 }
  89.             }
  90.             return comp;
  91.         }
  92.  
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement