Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package optimizationMethods;
  2.  
  3. import com.google.common.collect.Multiset;
  4. import com.google.common.collect.SortedMultiset;
  5. import com.google.common.collect.TreeMultiset;
  6.  
  7. import java.util.*;
  8.  
  9. public class Test {
  10.  
  11.     private static int i = 0;
  12.     private static int j = 100;
  13.     private static int z = 400;
  14.  
  15.     private static final String NEWS = "News";
  16.     private static final String AFISHA = "Afisha";
  17.     private static final String DEFAULT = "somestr";
  18.  
  19.     public static void main(String[] args) {
  20.         SortedMultiset<Item> set = TreeMultiset.create(new MComparator());
  21.         set.addAll(Arrays.asList(
  22.                 new Item(i++, NEWS),
  23.                 new Item(i++, NEWS),
  24.                 new Item(j++, AFISHA),
  25.                 new Item(i++, NEWS),
  26.                 new Item(i++, NEWS),
  27.                 new Item(j++, AFISHA),
  28.                 new Item(i++, NEWS),
  29.                 new Item(z++, DEFAULT),
  30.                 new Item(j++, AFISHA),
  31.                 new Item(z++, DEFAULT),
  32.                 new Item(j++, AFISHA),
  33.                 new Item(z++, DEFAULT),
  34.                 new Item(j++, AFISHA),
  35.                 new Item(z++, DEFAULT),
  36.                 new Item(z++, DEFAULT)
  37.         ));
  38.  
  39.         for (Item e : set.elementSet()) {
  40.             System.out.println(e.getCount() + " Type:" + e.getContext());
  41.         }
  42.  
  43.     }
  44.  
  45. }
  46.  
  47. class MComparator implements Comparator<Item> {
  48.  
  49.  
  50.     @Override
  51.     public int compare(Item o1, Item o2) {
  52.  
  53.         if (!Objects.equals(o1.getContext(), o2.getContext())) return -1;
  54.         if (Objects.equals(o1.getContext(), o2.getContext())) return 1;
  55.         return 2;
  56.     }
  57. }
  58.  
  59. class Item {
  60.  
  61.     private int count;
  62.     private String context;
  63.     private String news;
  64.  
  65.     public Item(int count, String context) {
  66.         this.count = count;
  67.         this.context = context;
  68.     }
  69.  
  70.     public Item(int count, String context, String news) {
  71.         this.count = count;
  72.         this.context = context;
  73.         this.news = news;
  74.     }
  75.  
  76.     public int getCount() {
  77.         return count;
  78.     }
  79.  
  80.     public void setCount(int count) {
  81.         this.count = count;
  82.     }
  83.  
  84.     public String getContext() {
  85.         return context;
  86.     }
  87.  
  88.     public void setContext(String context) {
  89.         this.context = context;
  90.     }
  91.  
  92.     public String getNews() {
  93.         return news;
  94.     }
  95.  
  96.     public void setNews(String news) {
  97.         this.news = news;
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement