Пример #1
0
 /**
  * Returns a {@code TreeMap}, from a source java.util.Map.
  *
  * @param map A map entry.
  * @param <K> The key type
  * @param <V> The value type
  * @return A new Map containing the given map
  */
 public static <K extends Comparable<? super K>, V> TreeMap<K, V> ofAll(
     java.util.Map<? extends K, ? extends V> map) {
   Objects.requireNonNull(map, "map is null");
   RedBlackTree<Tuple2<K, V>> result = RedBlackTree.empty();
   for (java.util.Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
     result = result.insert(Tuple.of(entry.getKey(), entry.getValue()));
   }
   return new TreeMap<>(result);
 }
Пример #2
0
 /**
  * Internal factory method, used with Tuple2 comparator instead of a key comparator.
  *
  * @param comparator An Tuple2 comparator
  * @param entries Map entries
  * @param <K> Key type
  * @param <V> Value type
  * @return A new TreeMap.
  */
 @SuppressWarnings("unchecked")
 private static <K, V> TreeMap<K, V> createTreeMap(
     Comparator<? super Tuple2<K, V>> comparator,
     Iterable<? extends Tuple2<? extends K, ? extends V>> entries) {
   RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(comparator);
   for (Tuple2<? extends K, ? extends V> entry : entries) {
     tree = tree.insert((Tuple2<K, V>) entry);
   }
   return tree.isEmpty() ? (TreeMap<K, V>) empty() : new TreeMap<>(tree);
 }
Пример #3
0
 @Override
 public TreeMap<K, V> retainAll(Iterable<? extends Tuple2<K, V>> elements) {
   Objects.requireNonNull(elements, "elements is null");
   RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entries.comparator());
   for (Tuple2<K, V> entry : elements) {
     if (contains(entry)) {
       tree = tree.insert(entry);
     }
   }
   return new TreeMap<>(tree);
 }
Пример #4
0
 /**
  * Creates a {@code TreeMap} of the given entries using the given key comparator.
  *
  * @param <K> The key type
  * @param <V> The value type
  * @param entries Map entries
  * @param keyComparator A key comparator
  * @return A new TreeMap containing the given entries.
  */
 @SafeVarargs
 public static <K, V> TreeMap<K, V> ofEntries(
     Comparator<? super K> keyComparator,
     java.util.Map.Entry<? extends K, ? extends V>... entries) {
   Objects.requireNonNull(keyComparator, "keyComparator is null");
   Objects.requireNonNull(entries, "entries is null");
   RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(new EntryComparator<>(keyComparator));
   for (java.util.Map.Entry<? extends K, ? extends V> entry : entries) {
     tree = tree.insert(Tuple.of(entry.getKey(), entry.getValue()));
   }
   return tree.isEmpty() ? empty(keyComparator) : new TreeMap<>(tree);
 }
Пример #5
0
 /**
  * Creates a {@code TreeMap} of the given entries using the given key comparator.
  *
  * @param <K> The key type
  * @param <V> The value type
  * @param entries Map entries
  * @param keyComparator A key comparator
  * @return A new TreeMap containing the given entries.
  */
 @SuppressWarnings("unchecked")
 @SafeVarargs
 public static <K, V> TreeMap<K, V> ofEntries(
     Comparator<? super K> keyComparator, Tuple2<? extends K, ? extends V>... entries) {
   Objects.requireNonNull(keyComparator, "keyComparator is null");
   Objects.requireNonNull(entries, "entries is null");
   RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(new EntryComparator<>(keyComparator));
   for (Tuple2<? extends K, ? extends V> entry : entries) {
     tree = tree.insert((Tuple2<K, V>) entry);
   }
   return tree.isEmpty() ? empty(keyComparator) : new TreeMap<>(tree);
 }
Пример #6
0
 /**
  * Creates a TreeMap of the given list of key-value pairs.
  *
  * @param pairs A list of key-value pairs
  * @param <K> The key type
  * @param <V> The value type
  * @return A new Map containing the given entries
  */
 @SuppressWarnings("unchecked")
 public static <K, V> TreeMap<K, V> of(Object... pairs) {
   Objects.requireNonNull(pairs, "pairs is null");
   if ((pairs.length & 1) != 0) {
     throw new IllegalArgumentException("Odd length of key-value pairs list");
   }
   RedBlackTree<Tuple2<K, V>> result = RedBlackTree.empty();
   for (int i = 0; i < pairs.length; i += 2) {
     result = result.insert(Tuple.of((K) pairs[i], (V) pairs[i + 1]));
   }
   return new TreeMap<>(result);
 }
Пример #7
0
 /**
  * Creates a {@code TreeMap} of the given entries.
  *
  * @param <K> The key type
  * @param <V> The value type
  * @param entries Map entries
  * @param keyComparator A key comparator
  * @return A new TreeMap containing the given entries.
  */
 @SuppressWarnings("unchecked")
 public static <K, V> TreeMap<K, V> ofEntries(
     Comparator<? super K> keyComparator,
     Iterable<? extends Tuple2<? extends K, ? extends V>> entries) {
   Objects.requireNonNull(keyComparator, "keyComparator is null");
   Objects.requireNonNull(entries, "entries is null");
   if (entries instanceof TreeMap) {
     return (TreeMap<K, V>) entries;
   } else {
     RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(new EntryComparator<>(keyComparator));
     for (Tuple2<? extends K, ? extends V> entry : entries) {
       tree = tree.insert((Tuple2<K, V>) entry);
     }
     return new TreeMap<>(tree);
   }
 }
Пример #8
0
 /**
  * Returns the empty TreeMap using the given key comparator.
  *
  * @param <K> The key type
  * @param <V> The value type
  * @param keyComparator The comparator used to sort the entries by their key.
  * @return A new empty TreeMap.
  */
 public static <K, V> TreeMap<K, V> empty(Comparator<? super K> keyComparator) {
   Objects.requireNonNull(keyComparator, "keyComparator is null");
   return new TreeMap<>(RedBlackTree.empty(new EntryComparator<>(keyComparator)));
 }