Example usage for java.util NavigableMap pollLastEntry

List of usage examples for java.util NavigableMap pollLastEntry

Introduction

In this page you can find the example usage for java.util NavigableMap pollLastEntry.

Prototype

Map.Entry<K, V> pollLastEntry();

Source Link

Document

Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Usage

From source file:Main.java

public static void main(String[] args) {
    NavigableMap<String, Integer> navigableMap = new TreeMap<String, Integer>();
    String[] letters = { "a", "b", "c" };
    int[] ints = { 3, 2, 1 };
    for (int i = 0; i < letters.length; i++) {
        navigableMap.put(letters[i], ints[i]);
    }//from   w ww .  jav  a  2 s .  c  om
    System.out.println("Map = " + navigableMap);
    System.out.println("Poll first entry: " + navigableMap.pollLastEntry());
}

From source file:in.java

  public static void main(String[] args) {
  NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
  map.put(2, "two");
  map.put(1, "one");
  map.put(3, "three");
  System.out.println("Original map: " + map + "\n");

  Entry firstEntry = map.pollFirstEntry();
  System.out.println("First entry: " + firstEntry);
  System.out.println("After polling the first entry: " + map + "\n");

  Entry lastEntry = map.pollLastEntry();
  System.out.println("Last entry:" + lastEntry);
  System.out.println("After polling last entry:" + map);
}

From source file:NavigableMapDemo.java

public static void main(String[] args) {
    NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
    map.put(2, "two");
    map.put(1, "one");
    map.put(3, "three");
    System.out.println("Original map: " + map + "\n");

    Entry firstEntry = map.pollFirstEntry();
    System.out.println("First entry: " + firstEntry);
    System.out.println("After polling the first entry: " + map + "\n");

    Entry lastEntry = map.pollLastEntry();
    System.out.println("Last entry:" + lastEntry);
    System.out.println("After polling last entry:" + map);
}

From source file:com.mirth.connect.server.controllers.DonkeyMessageController.java

private List<MessageSearchResult> searchMessages(MessageFilter filter, String channelId, int offset,
        int limit) {
    long startTime = System.currentTimeMillis();

    FilterOptions filterOptions = new FilterOptions(filter, channelId);
    long maxMessageId = filterOptions.getMaxMessageId();
    long minMessageId = filterOptions.getMinMessageId();

    Long localChannelId = ChannelController.getInstance().getLocalChannelId(channelId);
    Map<String, Object> params = getBasicParameters(filter, localChannelId);

    try {// w  w w . j  a v a2  s . c  om
        NavigableMap<Long, MessageSearchResult> messages = new TreeMap<Long, MessageSearchResult>();
        SqlSession session = SqlConfig.getSqlSessionManager();

        int offsetRemaining = offset;
        /*
         * If the limit is greater than the default batch size, use the limit, but cap it at
         * 50000.
         */
        long batchSize = Math.min(Math.max(limit, 500), 50000);
        long totalSearched = 0;

        while (messages.size() < limit && maxMessageId >= minMessageId) {
            /*
             * Slowly increase the batch size in case all the necessary results are found early
             * on.
             */
            if (totalSearched >= 100000 && batchSize < 50000) {
                batchSize = 50000;
            } else if (totalSearched >= 10000 && batchSize < 10000) {
                batchSize = 10000;
            } else if (totalSearched >= 1000 && batchSize < 1000) {
                batchSize = 1000;
            }

            /*
             * Search in descending order so that messages will be found from the greatest to
             * lowest message id
             */
            long currentMinMessageId = Math.max(maxMessageId - batchSize + 1, minMessageId);
            params.put("maxMessageId", maxMessageId);
            params.put("minMessageId", currentMinMessageId);
            maxMessageId -= batchSize;
            totalSearched += batchSize;

            Map<Long, MessageSearchResult> foundMessages = searchAll(session, params, filter, localChannelId,
                    false, filterOptions);

            if (!foundMessages.isEmpty()) {
                /*
                 * Skip results until there is no offset remaining. This is required when
                 * viewing results beyond the first page
                 */
                if (offsetRemaining >= foundMessages.size()) {
                    offsetRemaining -= foundMessages.size();
                } else if (offsetRemaining == 0) {
                    messages.putAll(foundMessages);
                } else {
                    NavigableMap<Long, MessageSearchResult> orderedMessages = new TreeMap<Long, MessageSearchResult>(
                            foundMessages);

                    while (offsetRemaining-- > 0) {
                        orderedMessages.pollLastEntry();
                    }

                    messages.putAll(orderedMessages);
                }
            }
        }

        // Remove results beyond the limit requested
        while (messages.size() > limit) {
            messages.pollFirstEntry();
        }

        List<MessageSearchResult> results = new ArrayList<MessageSearchResult>(messages.size());

        /*
         * Now that we have the message and metadata ids that should be returned as the result,
         * we need to retrieve the message data for those.
         */
        if (!messages.isEmpty()) {
            Iterator<Long> iterator = messages.descendingKeySet().iterator();

            while (iterator.hasNext()) {
                Map<String, Object> messageParams = new HashMap<String, Object>();
                messageParams.put("localChannelId", localChannelId);

                ListRangeIterator listRangeIterator = new ListRangeIterator(iterator,
                        ListRangeIterator.DEFAULT_LIST_LIMIT, false, null);

                while (listRangeIterator.hasNext()) {
                    ListRangeItem item = listRangeIterator.next();
                    List<Long> list = item.getList();
                    Long startRange = item.getStartRange();
                    Long endRange = item.getEndRange();

                    if (list != null || (startRange != null && endRange != null)) {
                        if (list != null) {
                            messageParams.remove("minMessageId");
                            messageParams.remove("maxMessageId");
                            messageParams.put("includeMessageList", StringUtils.join(list, ","));
                        } else {
                            messageParams.remove("includeMessageList");
                            messageParams.put("minMessageId", endRange);
                            messageParams.put("maxMessageId", startRange);
                        }

                        // Get the current batch of results
                        List<MessageSearchResult> currentResults = session
                                .selectList("Message.selectMessagesById", messageParams);

                        // Add the metadata ids to each result
                        for (MessageSearchResult currentResult : currentResults) {
                            currentResult.setMetaDataIdSet(
                                    messages.get(currentResult.getMessageId()).getMetaDataIdSet());
                        }

                        // Add the current batch to the final list of results
                        results.addAll(currentResults);
                    }
                }
            }
        }

        return results;
    } finally {
        long endTime = System.currentTimeMillis();
        logger.debug("Search executed in " + (endTime - startTime) + "ms");
    }
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

public void testPollLastEntry() {
    K[] keys = getSortedKeys();/*from w  w w  . java2  s .com*/
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();

    assertNull(map.pollLastEntry());
    assertEquals(0, map.size());

    map.put(keys[0], values[0]);
    assertEquals(keys[0], map.pollLastEntry().getKey());
    assertEquals(0, map.size());

    map.put(keys[0], values[0]);
    map.put(keys[1], values[1]);
    assertEquals(keys[1], map.pollLastEntry().getKey());
    assertEquals(1, map.size());
    Entry<K, V> entry = map.pollLastEntry();
    verifyEntry(entry);
    assertEquals(keys[0], entry.getKey());
    assertEquals(0, map.size());
    assertNull(map.pollLastEntry());
}