SHOW:
|
|
- or go back to the newest paste.
1 | package ch.fhnw.claudemartin; | |
2 | ||
3 | import java.util.Arrays; | |
4 | import java.util.Collections; | |
5 | - | import java.util.HashMap; |
5 | + | |
6 | import java.util.List; | |
7 | import java.util.stream.Collectors; | |
8 | ||
9 | public class SomeClass { | |
10 | ||
11 | - | final int[] sorted = Arrays.stream(arr).distinct().boxed() |
11 | + | |
12 | - | .sorted(new MyComp(arr)).mapToInt(i -> i).toArray(); |
12 | + | |
13 | - | System.out.println(Arrays.toString(sorted)); |
13 | + | System.out.println(Arrays.toString(sortByFrequency(arr))); |
14 | } | |
15 | ||
16 | - | static class MyComp implements Comparator<Integer> { |
16 | + | static final int[] sortByFrequency(final int[] input) { |
17 | final List<Integer> list = Arrays.stream(input).boxed().collect(Collectors.toList()); | |
18 | - | final HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); |
18 | + | final Comparator<Integer> comp = (a,b) -> Integer.compare(Collections.frequency(list, b), Collections.frequency(list, a)); |
19 | - | final int[] data; |
19 | + | return Arrays.stream(input).distinct().boxed().sorted(comp).mapToInt(i -> i).toArray(); |
20 | } | |
21 | - | private boolean initialized = false; |
21 | + | |
22 | - | public MyComp(final int[] data) { |
22 | + |