Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Set;
- class MyClass {
- static HashMap<Integer, Integer> map;// = new HashMap<Integer,Integer>();
- public static void main(final String[] arrg) {
- final int[] arr = { 2, 5, 2, 8, 5, 6, 8, 8 };
- map = new HashMap<Integer, Integer>();
- final Set<Integer> set = new HashSet<>();
- for (int i = 0; i < arr.length; i++) {
- if (map.get(arr[i]) == null) {
- map.put(arr[i], 1);
- } else {
- int count = map.get(arr[i]);
- map.put(arr[i], ++count);
- }
- set.add(arr[i]);
- }
- final ArrayList<Integer> list = new ArrayList<Integer>(set);
- Collections.sort(list, new MyComp());
- System.out.println(list);
- }
- static class MyComp implements Comparator<Integer> {
- @Override
- public int compare(final Integer o1, final Integer o2) {
- final int count1 = map.get(o1);
- final int count2 = map.get(o2);
- if (count1 > count2) {
- return -1;
- } else if (count1 < count2) {
- return 1;
- } else {
- return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement