@SafeVarargs static <T> RedBlackTree<T> of(Comparator<? super T> comparator, T... values) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(values, "values is null"); RedBlackTree<T> tree = empty(comparator); for (T value : values) { tree = tree.insert(value); } return tree; }
public static <T> TreeSet<T> empty(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); return new TreeSet<>(RedBlackTree.empty(comparator)); }
@SuppressWarnings("unchecked") private static <K, V> TreeMap<K, V> createTreeMap(EntryComparator<K, V> entryComparator, Iterable<? extends Tuple2<? extends K, ? extends V>> entries) { Objects.requireNonNull(entries, "entries is null"); RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (Tuple2<K, V> entry : (Iterable<Tuple2<K, V>>) entries) { tree = tree.insert(entry); } return new TreeMap<>(tree); }
@SuppressWarnings("unchecked") private static <K, V> TreeMap<K, V> createFromMap(EntryComparator<K, V> entryComparator, java.util.Map<? extends K, ? extends V> map) { Objects.requireNonNull(map, "map is null"); RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (java.util.Map.Entry<K, V> entry : ((java.util.Map<K, V>) map).entrySet()) { tree = tree.insert(Tuple.of(entry.getKey(), entry.getValue())); } return new TreeMap<>(tree); }
private static <K, K2, V, V2> TreeMap<K2, V2> createTreeMap(EntryComparator<K2, V2> entryComparator, Iterable<Tuple2<K, V>> entries, Function<Tuple2<K, V>, Tuple2<K2, V2>> entryMapper) { RedBlackTree<Tuple2<K2, V2>> tree = RedBlackTree.empty(entryComparator); for (Tuple2<K, V> entry : entries) { tree = tree.insert(entryMapper.apply(entry)); } return new TreeMap<>(tree); }
@SuppressWarnings("unchecked") private static <K, V> TreeMap<K, V> createFromTuples(EntryComparator<K, V> entryComparator, Tuple2<? extends K, ? extends V>... entries) { Objects.requireNonNull(entries, "entries is null"); RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (Tuple2<? extends K, ? extends V> entry : entries) { tree = tree.insert((Tuple2<K, V>) entry); } return new TreeMap<>(tree); }
@SafeVarargs private static <K, V> TreeMap<K, V> createFromMapEntries(EntryComparator<K, V> entryComparator, java.util.Map.Entry<? extends K, ? extends V>... entries) { Objects.requireNonNull(entries, "entries is null"); RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (java.util.Map.Entry<? extends K, ? extends V> entry : entries) { final K key = entry.getKey(); final V value = entry.getValue(); tree = tree.insert(Tuple.of(key, value)); } return new TreeMap<>(tree); }
@SuppressWarnings("unchecked") static <T> RedBlackTree<T> ofAll(Comparator<? super T> comparator, Iterable<? extends T> values) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(values, "values is null"); // function equality is not computable => same object check if (values instanceof RedBlackTree && ((RedBlackTree<T>) values).comparator() == comparator) { return (RedBlackTree<T>) values; } else { RedBlackTree<T> tree = empty(comparator); for (T value : values) { tree = tree.insert(value); } return tree; } }
/** * Returns the empty TreeMap. The underlying key comparator is the natural comparator of K. * * @param <K> The key type * @param <V> The value type * @return A new empty TreeMap. */ public static <K extends Comparable<? super K>, V> TreeMap<K, V> empty() { return new TreeMap<>(RedBlackTree.empty(EntryComparator.natural())); }
/** * 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) { return new TreeMap<>(RedBlackTree.empty(EntryComparator.of(keyComparator))); }
@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); }
@SuppressWarnings("unchecked") private static <K, V> TreeMap<K, V> createFromPairs(EntryComparator<K, V> entryComparator, Object... pairs) { RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (int i = 0; i < pairs.length; i += 2) { final K key = (K) pairs[i]; final V value = (V) pairs[i + 1]; tree = tree.insert(Tuple.of(key, value)); } return new TreeMap<>(tree); }
@SafeVarargs static <T> RedBlackTree<T> of(Comparator<? super T> comparator, T... values) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(values, "values is null"); RedBlackTree<T> tree = empty(comparator); for (T value : values) { tree = tree.insert(value); } return tree; }
private static <K, K2, V, V2> TreeMap<K2, V2> createTreeMap(EntryComparator<K2, V2> entryComparator, Iterable<Tuple2<K, V>> entries, Function<Tuple2<K, V>, Tuple2<K2, V2>> entryMapper) { RedBlackTree<Tuple2<K2, V2>> tree = RedBlackTree.empty(entryComparator); for (Tuple2<K, V> entry : entries) { tree = tree.insert(entryMapper.apply(entry)); } return new TreeMap<>(tree); }
@SuppressWarnings("unchecked") private static <K, V> TreeMap<K, V> createFromTuples(EntryComparator<K, V> entryComparator, Tuple2<? extends K, ? extends V>... entries) { Objects.requireNonNull(entries, "entries is null"); RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (Tuple2<? extends K, ? extends V> entry : entries) { tree = tree.insert((Tuple2<K, V>) entry); } return new TreeMap<>(tree); }
@SafeVarargs private static <K, V> TreeMap<K, V> createFromMapEntries(EntryComparator<K, V> entryComparator, java.util.Map.Entry<? extends K, ? extends V>... entries) { Objects.requireNonNull(entries, "entries is null"); RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (java.util.Map.Entry<? extends K, ? extends V> entry : entries) { final K key = entry.getKey(); final V value = entry.getValue(); tree = tree.insert(Tuple.of(key, value)); } return new TreeMap<>(tree); }
@SuppressWarnings("unchecked") private static <K, V> TreeMap<K, V> createTreeMap(EntryComparator<K, V> entryComparator, Iterable<? extends Tuple2<? extends K, ? extends V>> entries) { Objects.requireNonNull(entries, "entries is null"); RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (Tuple2<K, V> entry : (Iterable<Tuple2<K, V>>) entries) { tree = tree.insert(entry); } return new TreeMap<>(tree); }
/** * 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) { return new TreeMap<>(RedBlackTree.empty(EntryComparator.of(keyComparator))); }
/** * Returns the empty TreeMap. The underlying key comparator is the natural comparator of K. * * @param <K> The key type * @param <V> The value type * @return A new empty TreeMap. */ public static <K extends Comparable<? super K>, V> TreeMap<K, V> empty() { return new TreeMap<>(RedBlackTree.empty(EntryComparator.natural())); }
@SuppressWarnings("unchecked") private static <K, V> TreeMap<K, V> createFromPairs(EntryComparator<K, V> entryComparator, Object... pairs) { RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entryComparator); for (int i = 0; i < pairs.length; i += 2) { final K key = (K) pairs[i]; final V value = (V) pairs[i + 1]; tree = tree.insert(Tuple.of(key, value)); } return new TreeMap<>(tree); }