Example usage for java.util NavigableSet descendingIterator

List of usage examples for java.util NavigableSet descendingIterator

Introduction

In this page you can find the example usage for java.util NavigableSet descendingIterator.

Prototype

Iterator<E> descendingIterator();

Source Link

Document

Returns an iterator over the elements in this set, in descending order.

Usage

From source file:NavigableSetDemo.java

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(3, 2, 4, 1, 5);
    NavigableSet<Integer> ns = new TreeSet<Integer>(list);
    System.out.println("Ascending order (default): " + ns);

    Iterator<Integer> descendingIterator = ns.descendingIterator();
    StringBuilder sb = new StringBuilder("Descending order: ");
    while (descendingIterator.hasNext()) {
        int m = descendingIterator.next();
        sb.append(m + " ");
    }//from   w  w  w .  j av  a2  s. c om
    System.out.println(sb);

    int greatest = ns.lower(3);
    System.out.println("Lower of 3 = " + greatest);

    int smallest = ns.higher(3);
    System.out.println("Higher of 3 = " + smallest);
}

From source file:is.java

  public static void main(String[] args) {
  List<Integer> list = Arrays.asList(3, 2, 4, 1, 5);
  NavigableSet<Integer> ns = new TreeSet<Integer>(list);
  System.out.println("Ascending order (default): " + ns);

  Iterator<Integer> descendingIterator = ns.descendingIterator();
  StringBuilder sb = new StringBuilder("Descending order: ");
  while (descendingIterator.hasNext()) {
    int m = descendingIterator.next();
    sb.append(m + " ");
  }/*  w  w  w .  j  a va2s  .c  om*/
  System.out.println(sb);

  int greatest = ns.lower(3);
  System.out.println("Lower of 3 = " + greatest);

  int smallest = ns.higher(3);
  System.out.println("Higher of 3 = " + smallest);
}

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

public void testDescendingIterator() {
    NavigableSet<E> set = createNavigableSet();
    set.add(getKeys()[0]);/*from w  ww  .java  2s.  c o m*/

    Iterator<E> descendingIterator = set.descendingIterator();
    _assertEquals(set, reverseCollection(asCollection(descendingIterator)));

    set.add(getKeys()[1]);
    set.add(getKeys()[2]);
    descendingIterator = set.descendingIterator();
    _assertEquals(set, reverseCollection(asCollection(descendingIterator)));

    descendingIterator = set.descendingIterator();
    while (descendingIterator.hasNext()) {
        descendingIterator.next();
        descendingIterator.remove();
    }
    assertEquals(0, set.size());
}

From source file:org.apache.hadoop.hbase.regionserver.DefaultMemStore.java

private Member memberOfPreviousRow(NavigableSet<KeyValue> set, final GetClosestRowBeforeTracker state,
        final KeyValue firstOnRow) {
    NavigableSet<KeyValue> head = set.headSet(firstOnRow, false);
    if (head.isEmpty())
        return null;
    for (Iterator<KeyValue> i = head.descendingIterator(); i.hasNext();) {
        KeyValue found = i.next();
        if (state.isExpired(found)) {
            i.remove();//w  ww  .j  a v a2s .c o m
            continue;
        }
        return new Member(head, found);
    }
    return null;
}

From source file:org.apache.hadoop.hbase.regionserver.Memcache.java

private void getRowKeyBefore(ConcurrentSkipListSet<KeyValue> set, KeyValue search,
        NavigableSet<KeyValue> candidates, final NavigableSet<KeyValue> deletes, final long now) {
    NavigableSet<KeyValue> headSet = set.headSet(search);
    // If we tried to create a headMap and got an empty map, then there are
    // no keys at or before the search key, so we're done.
    if (headSet.isEmpty()) {
        return;//from w ww  .  ja v  a 2  s .  c  om
    }

    // If there aren't any candidate keys at this point, we need to search
    // backwards until we find at least one candidate or run out of headMap.
    if (candidates.isEmpty()) {
        KeyValue lastFound = null;
        for (Iterator<KeyValue> i = headSet.descendingIterator(); i.hasNext();) {
            KeyValue found = i.next();
            // if the last row we found a candidate key for is different than
            // the row of the current candidate, we can stop looking -- if its
            // not a delete record.
            boolean deleted = found.isDeleteType();
            if (lastFound != null && this.comparator.matchingRows(lastFound, found) && !deleted) {
                break;
            }
            // If this isn't a delete, record it as a candidate key. Also 
            // take note of this candidate so that we'll know when
            // we cross the row boundary into the previous row.
            if (!deleted) {
                if (Store.notExpiredAndNotInDeletes(this.ttl, found, now, deletes)) {
                    lastFound = found;
                    candidates.add(found);
                } else {
                    // Its expired.
                    Store.expiredOrDeleted(set, found);
                }
            } else {
                // We are encountering items in reverse.  We may have just added
                // an item to candidates that this later item deletes.  Check.  If we
                // found something in candidates, remove it from the set.
                if (Store.handleDeletes(found, candidates, deletes)) {
                    remove(set, found);
                }
            }
        }
    } else {
        // If there are already some candidate keys, we only need to consider
        // the very last row's worth of keys in the headMap, because any 
        // smaller acceptable candidate keys would have caused us to start
        // our search earlier in the list, and we wouldn't be searching here.
        SortedSet<KeyValue> rowTailMap = headSet.tailSet(headSet.last().cloneRow(HConstants.LATEST_TIMESTAMP));
        Iterator<KeyValue> i = rowTailMap.iterator();
        do {
            KeyValue found = i.next();
            if (found.isDeleteType()) {
                Store.handleDeletes(found, candidates, deletes);
            } else {
                if (ttl == HConstants.FOREVER || now < found.getTimestamp() + ttl || !deletes.contains(found)) {
                    candidates.add(found);
                } else {
                    Store.expiredOrDeleted(set, found);
                }
            }
        } while (i.hasNext());
    }
}