Java Collections.reverseOrder Examples

Java Collections.reverseOrder - 30 examples found. These are the top rated real world Java examples of java.util.Collections.reverseOrder extracted from open source projects. You can rate examples to help us improve the quality of examples.
 @Test
 public void toSortedSet_with_comparator() {
   LazyIterable<Integer> integers = this.newWith(2, 4, 4, 2, 1, 4, 1, 3);
   MutableSortedSet<Integer> set = integers.toSortedSet(Collections.<Integer>reverseOrder());
   Verify.assertSortedSetsEqual(
       TreeSortedSet.newSetWith(Collections.<Integer>reverseOrder(), 1, 2, 3, 4), set);
 }
Example #2
0
 /**
  * Ranks the set of items by associated score.
  *
  * @param userItems map with scores for each item
  * @return the ranked list
  */
 protected List<Long> rankItems(Map<Long, Double> userItems) {
   List<Long> sortedItems = new ArrayList<Long>();
   if (userItems == null) {
     return sortedItems;
   }
   Map<Double, Set<Long>> itemsByRank = new HashMap<Double, Set<Long>>();
   for (Map.Entry<Long, Double> e : userItems.entrySet()) {
     long item = e.getKey();
     double pref = e.getValue();
     if (Double.isNaN(pref)) {
       // we ignore any preference assigned as NaN
       continue;
     }
     Set<Long> items = itemsByRank.get(pref);
     if (items == null) {
       items = new HashSet<Long>();
       itemsByRank.put(pref, items);
     }
     items.add(item);
   }
   List<Double> sortedScores = new ArrayList<Double>(itemsByRank.keySet());
   Collections.sort(sortedScores, Collections.reverseOrder());
   for (double pref : sortedScores) {
     List<Long> sortedPrefItems = new ArrayList<Long>(itemsByRank.get(pref));
     // deterministic output when ties in preferences: sort by item id
     Collections.sort(sortedPrefItems, Collections.reverseOrder());
     for (long itemID : sortedPrefItems) {
       sortedItems.add(itemID);
     }
   }
   return sortedItems;
 }
Example #3
0
  public void testSortOrder() {
    Comparator<Long> comp = Collections.reverseOrder();
    assertSortOrder(
        new ReverseLexicoder<Long>(new LongLexicoder()),
        comp,
        Long.MIN_VALUE,
        0xff1234567890abcdl,
        0xffff1234567890abl,
        0xffffff567890abcdl,
        0xffffffff7890abcdl,
        0xffffffffff90abcdl,
        0xffffffffffffabcdl,
        0xffffffffffffffcdl,
        -1l,
        0l,
        0x01l,
        0x1234l,
        0x123456l,
        0x12345678l,
        0x1234567890l,
        0x1234567890abl,
        0x1234567890abcdl,
        0x1234567890abcdefl,
        Long.MAX_VALUE);

    Comparator<String> comp2 = Collections.reverseOrder();
    assertSortOrder(
        new ReverseLexicoder<String>(new StringLexicoder()), comp2, "a", "aa", "ab", "b", "aab");
  }
  public Map<Integer, Integer> withdrawAmount(int expectedAmount) throws NotEnoughMoneyException {
    Map<Integer, Integer> result = new TreeMap<>(Collections.reverseOrder());
    Map<Integer, Integer> temp = new TreeMap<>(Collections.reverseOrder());
    int n, rest = 0;

    for (Map.Entry<Integer, Integer> entry : denominations.entrySet()) {
      n = expectedAmount / entry.getKey();
      if (n != 0) {
        if (n > entry.getValue()) {
          rest = (n - entry.getValue()) * entry.getKey();
          n = entry.getValue();
        }
        result.put(entry.getKey(), n);
      }

      temp.put(entry.getKey(), entry.getValue() - n);
      expectedAmount = expectedAmount % entry.getKey() + rest;

      rest = 0;
    }

    if (expectedAmount != 0) {
      throw new NotEnoughMoneyException();
    }

    denominations.clear();
    denominations.putAll(temp);

    // ConsoleHelper.writeMessage("Withdrawal request was successfully processed");
    return result;
  }
 @Override
 @Test
 public void toSortedBag_with_comparator() {
   RichIterable<Integer> integers = this.newWith(2, 4, 1, 3);
   MutableSortedBag<Integer> bag = integers.toSortedBag(Collections.<Integer>reverseOrder());
   Verify.assertSortedBagsEqual(
       TreeBag.newBagWith(Collections.<Integer>reverseOrder(), 4, 3, 2, 1), bag);
 }
 @Test
 public void toSortedSet_with_comparator() {
   this.unmodifiableCollection = this.newWith("2", "4", "4", "2", "1", "4", "1", "3");
   MutableSortedSet<String> set =
       this.unmodifiableCollection.toSortedSet(Collections.<String>reverseOrder());
   Verify.assertSortedSetsEqual(
       TreeSortedSet.newSetWith(Collections.<String>reverseOrder(), "1", "2", "3", "4"), set);
 }