Get the "best" in collection. - Android java.util

Android examples for java.util:Collection

Description

Get the "best" in collection.

Demo Code

/*/*from   w  ww .  j a  v a 2  s  .c  o  m*/
 *******************************************************************************
 * Copyright (C) 1996-2015, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Main {
  /**
   * Get the "best" in collection. That is the least if direction is < 0,
   * otherwise the greatest. The first is chosen if there are multiples.
   * 
   * @param <T>
   * @param <U>
   * @param c
   * @param comp
   * @param direction
   * @return
   */
  public static <T, U extends Collection<T>> T getBest(U c, Comparator<T> comp,
      int direction) {
    Iterator<T> it = c.iterator();
    if (!it.hasNext())
      return null;
    T bestSoFar = it.next();
    if (direction < 0) {
      while (it.hasNext()) {
        T item = it.next();
        int compValue = comp.compare(item, bestSoFar);
        if (compValue < 0) {
          bestSoFar = item;
        }
      }
    } else {
      while (it.hasNext()) {
        T item = it.next();
        int compValue = comp.compare(item, bestSoFar);
        if (compValue > 0) {
          bestSoFar = item;
        }
      }
    }
    return bestSoFar;
  }

  /**
   * Compare, allowing nulls and putting them first
   * 
   * @param a
   * @param b
   * @return
   */
  public static <T extends Comparable> int compare(T a, T b) {
    return a == null ? b == null ? 0 : -1 : b == null ? 1 : a.compareTo(b);
  }

  /**
   * Compare iterators
   * 
   * @param iterator1
   * @param iterator2
   * @return
   */
  public static <T extends Comparable> int compare(Iterator<T> iterator1,
      Iterator<T> iterator2) {
    int diff;
    while (true) {
      if (!iterator1.hasNext()) {
        return iterator2.hasNext() ? -1 : 0;
      } else if (!iterator2.hasNext()) {
        return 1;
      }
      diff = compare(iterator1.next(), iterator2.next());
      if (diff != 0) {
        return diff;
      }
    }
  }

  /**
   * Compare, with shortest first, and otherwise lexicographically
   * 
   * @param a
   * @param b
   * @return
   */
  public static <T extends Comparable, U extends Collection<T>> int compare(
      U o1, U o2) {
    int diff = o1.size() - o2.size();
    if (diff != 0) {
      return diff;
    }
    Iterator<T> iterator1 = o1.iterator();
    Iterator<T> iterator2 = o2.iterator();
    return compare(iterator1, iterator2);
  }

  /**
   * Compare, with shortest first, and otherwise lexicographically
   * 
   * @param a
   * @param b
   * @return
   */
  public static <T extends Comparable, U extends Set<T>> int compare(U o1, U o2) {
    int diff = o1.size() - o2.size();
    if (diff != 0) {
      return diff;
    }
    Collection<T> x1 = SortedSet.class.isInstance(o1) ? o1 : new TreeSet<T>(o1);
    Collection<T> x2 = SortedSet.class.isInstance(o2) ? o2 : new TreeSet<T>(o2);
    return compare(x1, x2);
  }

  /**
   * Compare, allowing nulls and putting them first
   * 
   * @param a
   * @param b
   * @return
   */
  public static <K extends Comparable, V extends Comparable, T extends Entry<K, V>> int compare(
      T a, T b) {
    if (a == null) {
      return b == null ? 0 : -1;
    } else if (b == null) {
      return 1;
    }
    int diff = compare(a.getKey(), b.getKey());
    if (diff != 0) {
      return diff;
    }
    return compare(a.getValue(), b.getValue());
  }

  /**
   * Get the size of an iterator (number of items in it).
   * 
   * @param source
   * @return
   */
  public static int size(Iterator source) {
    int result = 0;
    while (source.hasNext()) {
      source.next();
      ++result;
    }
    return result;
  }
}

Related Tutorials