Guest User

Untitled

a guest
Jan 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. public static List objectCounter(List o, int number) {
  2.         List<Brand> l = new ArrayList();
  3.         HashMap<Integer,Object> objectlist = new HashMap<Integer, Object>();
  4.         for (int i = 0; i < o.size(); i++) {
  5.             Brand object = (Brand) o.get(i);
  6.             if(objectlist.get(object.hashCode())==null){
  7.                 objectlist.put(object.hashCode(), object);
  8.                 object.setCounter(0);
  9.                 l.add(object);
  10.             }else{
  11.                 object.setCounter(object.getCounter()+1);
  12.             }
  13.            
  14.            
  15.         }
  16.         Collections.sort(l,new Comparator() {
  17.  
  18.             @Override
  19.             public int compare(Object o1, Object o2) {
  20.                 Brand b1 = (Brand) o1;
  21.                 Brand b2 = (Brand) o2;
  22.                 int one = b1.getCounter();
  23.                 int two = b1.getCounter();
  24.                 if(one==two){
  25.                     return 0;
  26.                 }else if (one<two){
  27.                     return -1;
  28.                 }else{
  29.                     return 1;
  30.                 }
  31.             }
  32.         });
  33.         return l;
  34.     }
Add Comment
Please, Sign In to add comment