/** * Returns a {@link java.util.stream.Collector} which may be used in conjunction with * {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a {@link TreeMap}. * * @param keyMapper The key mapper * @param valueMapper The value mapper * @param <K> The key type * @param <V> The value type * @param <T> Initial {@link java.util.stream.Stream} elements type * @param keyComparator The comparator used to sort the entries by their key. * @return A {@link TreeMap} Collector. */ public static <K, V, T> Collector<T, ArrayList<T>, TreeMap<K, V>> collector( Comparator<? super K> keyComparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) { Objects.requireNonNull(keyMapper, "key comparator is null"); Objects.requireNonNull(keyMapper, "keyMapper is null"); Objects.requireNonNull(valueMapper, "valueMapper is null"); return createCollector(EntryComparator.of(keyComparator), keyMapper, valueMapper); }
/** * Returns a {@link java.util.stream.Collector} which may be used in conjunction with * {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a {@link TreeMap}. * * @param keyMapper The key mapper * @param <K> The key type * @param <V> The value type * @param <T> Initial {@link java.util.stream.Stream} elements type * @param keyComparator The comparator used to sort the entries by their key. * @return A {@link TreeMap} Collector. */ public static <K, V, T extends V> Collector<T, ArrayList<T>, TreeMap<K, V>> collector( Comparator<? super K> keyComparator, Function<? super T, ? extends K> keyMapper) { Objects.requireNonNull(keyMapper, "key comparator is null"); Objects.requireNonNull(keyMapper, "keyMapper is null"); return createCollector(EntryComparator.of(keyComparator), keyMapper, v -> v); }
@Override public <K2, V2> TreeMap<K2, V2> map(Comparator<? super K2> keyComparator, BiFunction<? super K, ? super V, Tuple2<K2, V2>> mapper) { Objects.requireNonNull(keyComparator, "keyComparator is null"); return map(this, EntryComparator.of(keyComparator), mapper); }
/** * Creates a {@code TreeMap} of the given list of key-value pairs. * * @param keyComparator The comparator used to sort the entries by their key. * @param k1 a key for the map * @param v1 the value for k1 * @param k2 a key for the map * @param v2 the value for k2 * @param k3 a key for the map * @param v3 the value for k3 * @param k4 a key for the map * @param v4 the value for k4 * @param k5 a key for the map * @param v5 the value for k5 * @param k6 a key for the map * @param v6 the value for k6 * @param <K> The key type * @param <V> The value type * @return A new Map containing the given entries */ public static <K, V> TreeMap<K, V> of(Comparator<? super K> keyComparator, K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); }
/** * Creates a {@code TreeMap} of the given list of key-value pairs. * * @param keyComparator The comparator used to sort the entries by their key. * @param k1 a key for the map * @param v1 the value for k1 * @param k2 a key for the map * @param v2 the value for k2 * @param k3 a key for the map * @param v3 the value for k3 * @param k4 a key for the map * @param v4 the value for k4 * @param <K> The key type * @param <V> The value type * @return A new Map containing the given entries */ public static <K, V> TreeMap<K, V> of(Comparator<? super K> keyComparator, K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2, k3, v3, k4, v4); }
/** * Creates a {@code TreeMap} of the given list of key-value pairs. * * @param keyComparator The comparator used to sort the entries by their key. * @param k1 a key for the map * @param v1 the value for k1 * @param k2 a key for the map * @param v2 the value for k2 * @param k3 a key for the map * @param v3 the value for k3 * @param k4 a key for the map * @param v4 the value for k4 * @param k5 a key for the map * @param v5 the value for k5 * @param <K> The key type * @param <V> The value type * @return A new Map containing the given entries */ public static <K, V> TreeMap<K, V> of(Comparator<? super K> keyComparator, K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); }
/** * Returns a TreeMap containing {@code n} values of a given Function {@code f} * over a range of integer values from 0 to {@code n - 1}. * * @param <K> The key type * @param <V> The value type * @param keyComparator The comparator used to sort the entries by their key * @param n The number of elements in the TreeMap * @param f The Function computing element values * @return A TreeMap consisting of elements {@code f(0),f(1), ..., f(n - 1)} * @throws NullPointerException if {@code keyComparator} or {@code f} are null */ public static <K, V> TreeMap<K, V> tabulate(Comparator<? super K> keyComparator, int n, Function<? super Integer, ? extends Tuple2<? extends K, ? extends V>> f) { Objects.requireNonNull(f, "f is null"); return createTreeMap(EntryComparator.of(keyComparator), Collections.tabulate(n, f)); }
/** * Returns a TreeMap containing tuples returned by {@code n} calls to a given Supplier {@code s}. * * @param <K> The key type * @param <V> The value type * @param keyComparator The comparator used to sort the entries by their key * @param n The number of elements in the TreeMap * @param s The Supplier computing element values * @return A TreeMap of size {@code n}, where each element contains the result supplied by {@code s}. * @throws NullPointerException if {@code keyComparator} or {@code s} are null */ @SuppressWarnings("unchecked") public static <K, V> TreeMap<K, V> fill(Comparator<? super K> keyComparator, int n, Supplier<? extends Tuple2<? extends K, ? extends V>> s) { Objects.requireNonNull(s, "s is null"); return createTreeMap(EntryComparator.of(keyComparator), Collections.fill(n, s)); }
/** * Creates a {@code TreeMap} of the given list of key-value pairs. * * @param keyComparator The comparator used to sort the entries by their key. * @param k1 a key for the map * @param v1 the value for k1 * @param k2 a key for the map * @param v2 the value for k2 * @param k3 a key for the map * @param v3 the value for k3 * @param <K> The key type * @param <V> The value type * @return A new Map containing the given entries */ public static <K, V> TreeMap<K, V> of(Comparator<? super K> keyComparator, K k1, V v1, K k2, V v2, K k3, V v3) { return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2, k3, v3); }
/** * Creates a {@code TreeMap} of the given entries 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. * @param entries Map entries * @return A new TreeMap containing the given entries. */ @SuppressWarnings("varargs") @SafeVarargs public static <K, V> TreeMap<K, V> ofEntries(Comparator<? super K> keyComparator, java.util.Map.Entry<? extends K, ? extends V>... entries) { return createFromMapEntries(EntryComparator.of(keyComparator), entries); }
/** * Creates a {@code TreeMap} of the given entries 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. * @param entries Map entries * @return A new TreeMap containing the given entries. */ @SuppressWarnings({ "unchecked", "varargs" }) @SafeVarargs public static <K, V> TreeMap<K, V> ofEntries(Comparator<? super K> keyComparator, Tuple2<? extends K, ? extends V>... entries) { return createFromTuples(EntryComparator.of(keyComparator), entries); }
/** * Creates a {@code TreeMap} of the given list of key-value pairs. * * @param keyComparator The comparator used to sort the entries by their key. * @param k1 a key for the map * @param v1 the value for k1 * @param k2 a key for the map * @param v2 the value for k2 * @param <K> The key type * @param <V> The value type * @return A new Map containing the given entries */ public static <K, V> TreeMap<K, V> of(Comparator<? super K> keyComparator, K k1, V v1, K k2, V v2) { return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2); }
/** * Returns a singleton {@code TreeMap}, i.e. a {@code TreeMap} of one element. * * @param keyComparator The comparator used to sort the entries by their key. * @param key A singleton map key. * @param value A singleton map value. * @param <K> The key type * @param <V> The value type * @return A new Map containing the given entry */ public static <K, V> TreeMap<K, V> of(Comparator<? super K> keyComparator, K key, V value) { return createFromPairs(EntryComparator.of(keyComparator), key, value); }
/** * Returns a {@code TreeMap}, from a source java.util.Map. * * @param keyComparator The comparator used to sort the entries by their key. * @param map A map * @param <K> The key type * @param <V> The value type * @return A new Map containing the given map */ public static <K, V> TreeMap<K, V> ofAll(Comparator<? super K> keyComparator, java.util.Map<? extends K, ? extends V> map) { Objects.requireNonNull(map, "map is null"); return createFromMap(EntryComparator.of(keyComparator), map); }
/** * Returns a {@link Collector} which may be used in conjunction with * {@link java.util.stream.Stream#collect(Collector)} to obtain a * {@link TreeMap}. * * @param <K> The key type * @param <V> The value type * @param keyComparator The comparator used to sort the entries by their key. * @return A {@link TreeMap} Collector. */ public static <K, V> Collector<Tuple2<K, V>, ArrayList<Tuple2<K, V>>, TreeMap<K, V>> collector(Comparator<? super K> keyComparator) { return createCollector(EntryComparator.of(keyComparator)); }
/** * Creates a {@code TreeMap} of the given entries. * * @param <K> The key type * @param <V> The value type * @param keyComparator The comparator used to sort the entries by their key. * @param entries Map entries * @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) { return createTreeMap(EntryComparator.of(keyComparator), entries); }
/** * Returns a singleton {@code TreeMap}, i.e. a {@code TreeMap} of one entry using a specific key comparator. * * @param <K> The key type * @param <V> The value type * @param keyComparator The comparator used to sort the entries by their key. * @param entry A map entry. * @return A new TreeMap containing the given entry. */ public static <K, V> TreeMap<K, V> of(Comparator<? super K> keyComparator, Tuple2<? extends K, ? extends V> entry) { Objects.requireNonNull(entry, "entry is null"); return createFromTuple(EntryComparator.of(keyComparator), entry); }
@Override public <K2, V2> TreeMap<K2, V2> bimap(Comparator<? super K2> keyComparator, Function<? super K, ? extends K2> keyMapper, Function<? super V, ? extends V2> valueMapper) { return bimap(this, EntryComparator.of(keyComparator), keyMapper, valueMapper); }
@Override public <K2, V2> TreeMap<K2, V2> flatMap(Comparator<? super K2> keyComparator, BiFunction<? super K, ? super V, ? extends Iterable<Tuple2<K2, V2>>> mapper) { return flatMap(this, EntryComparator.of(keyComparator), mapper); }
/** * 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))); }