compare Entry Sets - Android java.util

Android examples for java.util:Set

Description

compare Entry Sets

Demo Code

/*//from w  w  w.j a va  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.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Main {
  public static <K extends Comparable, V extends Comparable, T extends Entry<K, V>> int compareEntrySets(
      Collection<T> o1, Collection<T> o2) {
    int diff = o1.size() - o2.size();
    if (diff != 0) {
      return diff;
    }
    Iterator<T> iterator1 = o1.iterator();
    Iterator<T> iterator2 = o2.iterator();
    while (true) {
      if (!iterator1.hasNext()) {
        return iterator2.hasNext() ? -1 : 0;
      } else if (!iterator2.hasNext()) {
        return 1;
      }
      T item1 = iterator1.next();
      T item2 = iterator2.next();
      diff = compare(item1, item2);
      if (diff != 0) {
        return diff;
      }
    }
  }

  /**
   * 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;
  }

  /**
   * 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());
  }
}

Related Tutorials