Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5.  
  6. class MyClass
  7. {
  8. static HashMap<Integer, Integer> map;// = new HashMap<Integer,Integer>();
  9. public static void main(String[] arrg)
  10. {
  11. int[] arr = {2,5,2,8,5,6,8,8};
  12. map = new HashMap<Integer,Integer>();
  13. ArrayList<Integer> list = new ArrayList<Integer>();
  14. for(int i=0; i<arr.length; i++)
  15. {
  16. if(map.get(arr[i]) == null)
  17. {
  18. map.put(arr[i],1);
  19. }
  20. else
  21. {
  22. int count = map.get(arr[i]);
  23. map.put(arr[i],++count);
  24. }
  25.  
  26. list.add(arr[i]);
  27. }
  28.  
  29. Collections.sort(list, new MyComp());
  30. System.out.println(list);
  31. }
  32.  
  33. static class MyComp implements Comparator<Integer>
  34. {
  35. public int compare(Integer o1, Integer o2) {
  36. int count1 = map.get(o1);
  37. int count2 = map.get(o2);
  38.  
  39. if(count1 > count2)
  40. {
  41. return -1;
  42. }
  43. else if(count1 < count2)
  44. {
  45. return 1;
  46. }
  47. else
  48. {
  49. return 0;
  50. }
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement