package optimizationMethods; import com.google.common.collect.Multiset; import com.google.common.collect.SortedMultiset; import com.google.common.collect.TreeMultiset; import java.util.*; public class Test { private static int i = 0; private static int j = 100; private static int z = 400; private static final String NEWS = "News"; private static final String AFISHA = "Afisha"; private static final String DEFAULT = "somestr"; public static void main(String[] args) { SortedMultiset set = TreeMultiset.create(new MComparator()); set.addAll(Arrays.asList( new Item(i++, NEWS), new Item(i++, NEWS), new Item(j++, AFISHA), new Item(i++, NEWS), new Item(i++, NEWS), new Item(j++, AFISHA), new Item(i++, NEWS), new Item(z++, DEFAULT), new Item(j++, AFISHA), new Item(z++, DEFAULT), new Item(j++, AFISHA), new Item(z++, DEFAULT), new Item(j++, AFISHA), new Item(z++, DEFAULT), new Item(z++, DEFAULT) )); for (Item e : set.elementSet()) { System.out.println(e.getCount() + " Type:" + e.getContext()); } } } class MComparator implements Comparator { @Override public int compare(Item o1, Item o2) { if (!Objects.equals(o1.getContext(), o2.getContext())) return -1; if (Objects.equals(o1.getContext(), o2.getContext())) return 1; return 2; } } class Item { private int count; private String context; private String news; public Item(int count, String context) { this.count = count; this.context = context; } public Item(int count, String context, String news) { this.count = count; this.context = context; this.news = news; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public String getNews() { return news; } public void setNews(String news) { this.news = news; } }