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