SHOW:
|
|
- or go back to the newest paste.
| 1 | - | import java.util.ArrayList; |
| 1 | + | package ch.fhnw.claudemartin; |
| 2 | - | import java.util.Collections; |
| 2 | + | |
| 3 | import java.util.Arrays; | |
| 4 | import java.util.Comparator; | |
| 5 | import java.util.HashMap; | |
| 6 | - | class MyClass |
| 6 | + | |
| 7 | - | {
|
| 7 | + | public class SomeClass {
|
| 8 | - | static HashMap<Integer, Integer> map;// = new HashMap<Integer,Integer>(); |
| 8 | + | |
| 9 | - | public static void main(String[] arrg) |
| 9 | + | public static void main(final String[] arrg) {
|
| 10 | - | {
|
| 10 | + | final int[] arr = { 2, 5, 2, 8, 5, 6, 8, 8 };
|
| 11 | - | int[] arr = {2,5,2,8,5,6,8,8};
|
| 11 | + | final int[] sorted = Arrays.stream(arr).distinct().boxed() |
| 12 | - | map = new HashMap<Integer,Integer>(); |
| 12 | + | .sorted(new MyComp(arr)).mapToInt(i -> i).toArray(); |
| 13 | - | ArrayList<Integer> list = new ArrayList<Integer>(); |
| 13 | + | System.out.println(Arrays.toString(sorted)); |
| 14 | - | for(int i=0; i<arr.length; i++) |
| 14 | + | } |
| 15 | - | {
|
| 15 | + | |
| 16 | - | if(map.get(arr[i]) == null) |
| 16 | + | static class MyComp implements Comparator<Integer> {
|
| 17 | - | {
|
| 17 | + | |
| 18 | - | map.put(arr[i],1); |
| 18 | + | final HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); |
| 19 | - | } |
| 19 | + | final int[] data; |
| 20 | - | else |
| 20 | + | |
| 21 | - | {
|
| 21 | + | private boolean initialized = false; |
| 22 | - | int count = map.get(arr[i]); |
| 22 | + | public MyComp(final int[] data) {
|
| 23 | - | map.put(arr[i],++count); |
| 23 | + | this.data = data; |
| 24 | - | } |
| 24 | + | |
| 25 | - | |
| 25 | + | |
| 26 | - | list.add(arr[i]); |
| 26 | + | private void init() {
|
| 27 | if(this.initialized) return; | |
| 28 | - | |
| 28 | + | this.initialized = true; |
| 29 | - | Collections.sort(list, new MyComp()); |
| 29 | + | |
| 30 | - | System.out.println(list); |
| 30 | + | for (final int i : this.data) {
|
| 31 | // could use map.computeIfAbsent(key, mappingFunction) | |
| 32 | - | |
| 32 | + | if (this.map.get(i) == null) {
|
| 33 | - | static class MyComp implements Comparator<Integer> |
| 33 | + | this.map.put(i, 1); |
| 34 | - | {
|
| 34 | + | } else {
|
| 35 | - | public int compare(Integer o1, Integer o2) {
|
| 35 | + | int count = this.map.get(i); |
| 36 | - | int count1 = map.get(o1); |
| 36 | + | this.map.put(i, ++count); |
| 37 | - | int count2 = map.get(o2); |
| 37 | + | |
| 38 | - | |
| 38 | + | } |
| 39 | - | if(count1 > count2) |
| 39 | + | |
| 40 | - | {
|
| 40 | + | |
| 41 | - | return -1; |
| 41 | + | |
| 42 | - | } |
| 42 | + | @Override |
| 43 | - | else if(count1 < count2) |
| 43 | + | public int compare(final Integer o1, final Integer o2) {
|
| 44 | - | {
|
| 44 | + | this.init(); |
| 45 | - | return 1; |
| 45 | + | final int count1 = this.map.get(o1); |
| 46 | - | } |
| 46 | + | final int count2 = this.map.get(o2); |
| 47 | - | else |
| 47 | + | return Integer.compare(count2, count1); |
| 48 | - | {
|
| 48 | + | |
| 49 | - | return 0; |
| 49 | + | } |
| 50 | - | } |
| 50 | + |