Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package by.defascat.saved;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Random;
- import java.util.stream.Collectors;
- /**
- *
- * @author andy
- */
- public class SortingEx {
- public static void main(String[] args) {
- List<O> al = new ArrayList<>();
- List<O> al2 = new ArrayList<>();
- new Random().ints(1000000).forEach(i -> al.add(new O(i)));
- al2.addAll(al);
- long time1 = System.currentTimeMillis();
- al.stream().sorted(Comparator
- .comparing(O::getA)
- .thenComparing(O::getB)
- .thenComparing(O::getC)
- .thenComparing(O::getD)
- )
- .collect(Collectors.toList());
- long time2 = System.currentTimeMillis();
- al.stream().sorted(Comparator
- .comparingInt(O::getA)
- .thenComparingInt(O::getB)
- .thenComparingInt(O::getC)
- .thenComparingInt(O::getD)
- )
- .collect(Collectors.toList());
- long time3 = System.currentTimeMillis();
- Collections.sort(al2);
- long time4 = System.currentTimeMillis();
- System.out.println("Time 1: " + (time2 - time1) + "; Time 2: " + (time3 - time2) + "; Time 3: " + (time4 - time3));
- }
- static class O implements Comparable<O> {
- private final int a;
- private final int b;
- private final int c;
- private final int d;
- public O(int i) {
- a = i % 2;
- b = i % 10;
- c = i % 100;
- d = i;
- }
- public int getA() {
- return a;
- }
- public int getB() {
- return b;
- }
- public int getC() {
- return c;
- }
- public int getD() {
- return d;
- }
- @Override
- public int compareTo(O o) {
- int comp = Integer.compare(getA(), o.getA());
- if (comp == 0) {
- comp = Integer.compare(getB(), o.getB());
- if (comp == 0) {
- comp = Integer.compare(getC(), o.getC());
- if (comp == 0) {
- comp = Integer.compare(getD(), o.getD());
- }
- }
- }
- return comp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement