io.vavr.collection.TreeMap Maven / Gradle / Ivy
Show all versions of vavr Show documentation
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/
*
* Copyright 2014-2019 Vavr, http://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vavr.collection;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.control.Option;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.*;
import java.util.stream.Collector;
/**
* SortedMap implementation, backed by a Red/Black Tree.
*
* @param Key type
* @param Value type
* @author Daniel Dietrich
*/
// DEV-NOTE: use entries.min().get() in favor of iterator().next(), it is faster!
public final class TreeMap implements SortedMap, Serializable {
private static final long serialVersionUID = 1L;
private final RedBlackTree> entries;
private TreeMap(RedBlackTree> entries) {
this.entries = entries;
}
/**
* Returns a {@link Collector} which may be used in conjunction with
* {@link java.util.stream.Stream#collect(Collector)} to obtain a
* {@link TreeMap}.
*
* The natural comparator is used to compare TreeMap keys.
*
* @param The key type
* @param The value type
* @return A {@link TreeMap} Collector.
*/
public static , V> Collector, ArrayList>, TreeMap> collector() {
return createCollector(EntryComparator.natural());
}
/**
* Returns a {@link Collector} which may be used in conjunction with
* {@link java.util.stream.Stream#collect(Collector)} to obtain a
* {@link TreeMap}.
*
* @param The key type
* @param The value type
* @param keyComparator The comparator used to sort the entries by their key.
* @return A {@link TreeMap} Collector.
*/
public static Collector, ArrayList>, TreeMap> collector(Comparator super K> keyComparator) {
return createCollector(EntryComparator.of(keyComparator));
}
/**
* 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}.
*
* The natural comparator is used to compare TreeMap keys.
*
* @param keyMapper The key mapper
* @param The key type
* @param The value type
* @param Initial {@link java.util.stream.Stream} elements type
* @return A {@link TreeMap} Collector.
*/
public static , V, T extends V> Collector, TreeMap> collector(
Function super T, ? extends K> keyMapper) {
Objects.requireNonNull(keyMapper, "key comparator is null");
Objects.requireNonNull(keyMapper, "keyMapper is null");
return createCollector(EntryComparator.natural(), keyMapper, v -> v);
}
/**
* 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}.
*
* The natural comparator is used to compare TreeMap keys.
*
* @param keyMapper The key mapper
* @param valueMapper The value mapper
* @param The key type
* @param The value type
* @param Initial {@link java.util.stream.Stream} elements type
* @return A {@link TreeMap} Collector.
*/
public static , V, T> Collector, TreeMap> collector(
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.natural(), 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 The key type
* @param The value type
* @param 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 Collector, TreeMap> 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);
}
/**
* 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 The key type
* @param The value type
* @param 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 Collector, TreeMap> 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 the empty TreeMap. The underlying key comparator is the natural comparator of K.
*
* @param The key type
* @param The value type
* @return A new empty TreeMap.
*/
public static , V> TreeMap empty() {
return new TreeMap<>(RedBlackTree.empty(EntryComparator.natural()));
}
/**
* Returns the empty TreeMap using the given key comparator.
*
* @param The key type
* @param The value type
* @param keyComparator The comparator used to sort the entries by their key.
* @return A new empty TreeMap.
*/
public static TreeMap empty(Comparator super K> keyComparator) {
return new TreeMap<>(RedBlackTree.empty(EntryComparator.of(keyComparator)));
}
/**
* Narrows a widened {@code TreeMap extends K, ? extends V>} to {@code TreeMap}
* by performing a type-safe cast. This is eligible because immutable/read-only
* collections are covariant.
*
* CAUTION: If {@code K} is narrowed, the underlying {@code Comparator} might fail!
*
* @param treeMap A {@code TreeMap}.
* @param Key type
* @param Value type
* @return the given {@code treeMap} instance as narrowed type {@code TreeMap}.
*/
@SuppressWarnings("unchecked")
public static TreeMap narrow(TreeMap extends K, ? extends V> treeMap) {
return (TreeMap) treeMap;
}
/**
* Returns a singleton {@code TreeMap}, i.e. a {@code TreeMap} of one entry.
* The underlying key comparator is the natural comparator of K.
*
* @param The key type
* @param The value type
* @param entry A map entry.
* @return A new TreeMap containing the given entry.
*/
public static , V> TreeMap of(Tuple2 extends K, ? extends V> entry) {
Objects.requireNonNull(entry, "entry is null");
return createFromTuple(EntryComparator.natural(), entry);
}
/**
* Returns a singleton {@code TreeMap}, i.e. a {@code TreeMap} of one entry using a specific key comparator.
*
* @param The key type
* @param 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 TreeMap of(Comparator super K> keyComparator, Tuple2 extends K, ? extends V> entry) {
Objects.requireNonNull(entry, "entry is null");
return createFromTuple(EntryComparator.of(keyComparator), entry);
}
/**
* Returns a {@code TreeMap}, from a source java.util.Map.
*
* @param map A map
* @param The key type
* @param The value type
* @return A new Map containing the given map
*/
public static , V> TreeMap ofAll(java.util.Map extends K, ? extends V> map) {
Objects.requireNonNull(map, "map is null");
return createFromMap(EntryComparator.natural(), map);
}
/**
* Returns a {@code TreeMap}, from entries mapped from stream.
*
* @param stream the source stream
* @param keyMapper the key mapper
* @param valueMapper the value mapper
* @param The stream element type
* @param The key type
* @param The value type
* @return A new Map
*/
public static , V> TreeMap ofAll(java.util.stream.Stream extends T> stream,
Function super T, ? extends K> keyMapper,
Function super T, ? extends V> valueMapper) {
return Maps.ofStream(TreeMap. empty(), stream, keyMapper, valueMapper);
}
/**
* Returns a {@code TreeMap}, from entries mapped from stream.
*
* @param keyComparator The comparator used to sort the entries by their key.
* @param stream the source stream
* @param keyMapper the key mapper
* @param valueMapper the value mapper
* @param The stream element type
* @param The key type
* @param The value type
* @return A new Map
*/
public static TreeMap ofAll(Comparator super K> keyComparator,
java.util.stream.Stream extends T> stream,
Function super T, ? extends K> keyMapper,
Function super T, ? extends V> valueMapper) {
return Maps.ofStream(empty(keyComparator), stream, keyMapper, valueMapper);
}
/**
* Returns a {@code TreeMap}, from entries mapped from stream.
*
* @param stream the source stream
* @param entryMapper the entry mapper
* @param The stream element type
* @param The key type
* @param The value type
* @return A new Map
*/
public static , V> TreeMap ofAll(java.util.stream.Stream extends T> stream,
Function super T, Tuple2 extends K, ? extends V>> entryMapper) {
return Maps.ofStream(TreeMap. empty(), stream, entryMapper);
}
/**
* Returns a {@code TreeMap}, from entries mapped from stream.
*
* @param keyComparator The comparator used to sort the entries by their key.
* @param stream the source stream
* @param entryMapper the entry mapper
* @param The stream element type
* @param The key type
* @param The value type
* @return A new Map
*/
public static TreeMap ofAll(Comparator super K> keyComparator,
java.util.stream.Stream extends T> stream,
Function super T, Tuple2 extends K, ? extends V>> entryMapper) {
return Maps.ofStream(empty(keyComparator), stream, entryMapper);
}
/**
* 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 The key type
* @param The value type
* @return A new Map containing the given map
*/
public static TreeMap 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 singleton {@code TreeMap}, i.e. a {@code TreeMap} of one element.
*
* @param key A singleton map key.
* @param value A singleton map value.
* @param The key type
* @param The value type
* @return A new Map containing the given entry
*/
public static , V> TreeMap of(K key, V value) {
return createFromPairs(EntryComparator.natural(), key, value);
}
/**
* Creates a {@code TreeMap} of the given list of key-value pairs.
*
* @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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(K k1, V v1, K k2, V v2) {
return createFromPairs(EntryComparator.natural(), k1, v1, k2, v2);
}
/**
* Creates a {@code TreeMap} of the given list of key-value pairs.
*
* @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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(K k1, V v1, K k2, V v2, K k3, V v3) {
return createFromPairs(EntryComparator.natural(), k1, v1, k2, v2, k3, v3);
}
/**
* Creates a {@code TreeMap} of the given list of key-value pairs.
*
* @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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
return createFromPairs(EntryComparator.natural(), k1, v1, k2, v2, k3, v3, k4, v4);
}
/**
* Creates a {@code TreeMap} of the given list of key-value pairs.
*
* @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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
return createFromPairs(EntryComparator.natural(), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
}
/**
* Creates a {@code TreeMap} of the given list of key-value pairs.
*
* @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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(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.natural(), 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 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 k7 a key for the map
* @param v7 the value for k7
* @param The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) {
return createFromPairs(EntryComparator.natural(), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7);
}
/**
* Creates a {@code TreeMap} of the given list of key-value pairs.
*
* @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 k7 a key for the map
* @param v7 the value for k7
* @param k8 a key for the map
* @param v8 the value for k8
* @param The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) {
return createFromPairs(EntryComparator.natural(), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8);
}
/**
* Creates a {@code TreeMap} of the given list of key-value pairs.
*
* @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 k7 a key for the map
* @param v7 the value for k7
* @param k8 a key for the map
* @param v8 the value for k8
* @param k9 a key for the map
* @param v9 the value for k9
* @param The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) {
return createFromPairs(EntryComparator.natural(), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9);
}
/**
* Creates a {@code TreeMap} of the given list of key-value pairs.
*
* @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 k7 a key for the map
* @param v7 the value for k7
* @param k8 a key for the map
* @param v8 the value for k8
* @param k9 a key for the map
* @param v9 the value for k9
* @param k10 a key for the map
* @param v10 the value for k10
* @param The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static , V> TreeMap of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {
return createFromPairs(EntryComparator.natural(), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10);
}
/**
* 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 The key type
* @param The value type
* @return A new Map containing the given entry
*/
public static TreeMap of(Comparator super K> keyComparator, K key, V value) {
return createFromPairs(EntryComparator.of(keyComparator), key, value);
}
/**
* 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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap of(Comparator super K> keyComparator, K k1, V v1, K k2, V v2) {
return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2);
}
/**
* 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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap 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 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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap 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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap 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);
}
/**
* 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 The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap 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 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 k7 a key for the map
* @param v7 the value for k7
* @param The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap 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, K k7, V v7) {
return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7);
}
/**
* 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 k7 a key for the map
* @param v7 the value for k7
* @param k8 a key for the map
* @param v8 the value for k8
* @param The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap 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, K k7, V v7, K k8, V v8) {
return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8);
}
/**
* 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 k7 a key for the map
* @param v7 the value for k7
* @param k8 a key for the map
* @param v8 the value for k8
* @param k9 a key for the map
* @param v9 the value for k9
* @param The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap 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, K k7, V v7, K k8, V v8, K k9, V v9) {
return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9);
}
/**
* 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 k7 a key for the map
* @param v7 the value for k7
* @param k8 a key for the map
* @param v8 the value for k8
* @param k9 a key for the map
* @param v9 the value for k9
* @param k10 a key for the map
* @param v10 the value for k10
* @param The key type
* @param The value type
* @return A new Map containing the given entries
*/
public static TreeMap 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, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {
return createFromPairs(EntryComparator.of(keyComparator), k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10);
}
/**
* 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 The key type
* @param 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 TreeMap 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 {@code n} values of a given Function {@code f}
* over a range of integer values from 0 to {@code n - 1}.
* The underlying key comparator is the natural comparator of K.
*
* @param The key type
* @param The value type
* @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 f} is null
*/
public static , V> TreeMap tabulate(int n, Function super Integer, ? extends Tuple2 extends K, ? extends V>> f) {
Objects.requireNonNull(f, "f is null");
return createTreeMap(EntryComparator.natural(), Collections.tabulate(n, f));
}
/**
* Returns a TreeMap containing tuples returned by {@code n} calls to a given Supplier {@code s}.
*
* @param The key type
* @param 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 TreeMap 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));
}
/**
* Returns a TreeMap containing tuples returned by {@code n} calls to a given Supplier {@code s}.
* The underlying key comparator is the natural comparator of K.
*
* @param The key type
* @param The value type
* @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 s} is null
*/
public static , V> TreeMap fill(int n, Supplier extends Tuple2 extends K, ? extends V>> s) {
Objects.requireNonNull(s, "s is null");
return createTreeMap(EntryComparator.natural(), Collections.fill(n, s));
}
/**
* Creates a {@code TreeMap} of the given entries using the natural key comparator.
*
* @param The key type
* @param The value type
* @param entries Map entries
* @return A new TreeMap containing the given entries.
*/
@SuppressWarnings("varargs")
@SafeVarargs
public static , V> TreeMap ofEntries(Tuple2 extends K, ? extends V>... entries) {
return createFromTuples(EntryComparator.natural(), entries);
}
/**
* Creates a {@code TreeMap} of the given entries using the given key comparator.
*
* @param The key type
* @param 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 TreeMap ofEntries(Comparator super K> keyComparator, Tuple2 extends K, ? extends V>... entries) {
return createFromTuples(EntryComparator.of(keyComparator), entries);
}
/**
* Creates a {@code TreeMap} of the given entries using the natural key comparator.
*
* @param The key type
* @param The value type
* @param entries Map entries
* @return A new TreeMap containing the given entries.
*/
@SuppressWarnings("varargs")
@SafeVarargs
public static , V> TreeMap ofEntries(java.util.Map.Entry extends K, ? extends V>... entries) {
return createFromMapEntries(EntryComparator.natural(), entries);
}
/**
* Creates a {@code TreeMap} of the given entries using the given key comparator.
*
* @param The key type
* @param 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 TreeMap 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.
*
* @param The key type
* @param The value type
* @param entries Map entries
* @return A new TreeMap containing the given entries.
*/
public static , V> TreeMap ofEntries(Iterable extends Tuple2 extends K, ? extends V>> entries) {
return createTreeMap(EntryComparator.natural(), entries);
}
/**
* Creates a {@code TreeMap} of the given entries.
*
* @param The key type
* @param 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 TreeMap ofEntries(Comparator super K> keyComparator, Iterable extends Tuple2 extends K, ? extends V>> entries) {
return createTreeMap(EntryComparator.of(keyComparator), entries);
}
// -- TreeMap API
@Override
public TreeMap bimap(Function super K, ? extends K2> keyMapper, Function super V, ? extends V2> valueMapper) {
return bimap(this, EntryComparator.natural(), keyMapper, valueMapper);
}
@Override
public TreeMap 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 Tuple2> computeIfAbsent(K key, Function super K, ? extends V> mappingFunction) {
return Maps.computeIfAbsent(this, key, mappingFunction);
}
@Override
public Tuple2