/** * Alias for {@link LinkedHashMap#empty()} * * @param <K> The key type. * @param <V> The value type. * @return A singleton instance of empty {@link LinkedHashMap} */ public static <K, V> Map<K, V> LinkedMap() { return LinkedHashMap.empty(); }
private LinkedHashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) { return LinkedHashMap.ofEntries(tuples); }
/** * Alias for {@link LinkedHashMap#of(Object, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key * @param v1 The value * @return A new {@link LinkedHashMap} instance containing the given entries */ public static <K, V> Map<K, V> LinkedMap(K k1, V v1) { return LinkedHashMap.of(k1, v1); }
@Override public LinkedHashMap<K, V> retainAll(Iterable<? extends Tuple2<K, V>> elements) { Objects.requireNonNull(elements, "elements is null"); LinkedHashMap<K, V> result = empty(); for (Tuple2<K, V> entry : elements) { if (contains(entry)) { result = result.put(entry._1, entry._2); } } return result; }
@Override public <K2, V2> LinkedHashMap<K2, V2> flatMap(BiFunction<? super K, ? super V, ? extends Iterable<Tuple2<K2, V2>>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return foldLeft(LinkedHashMap.<K2, V2> empty(), (acc, entry) -> { for (Tuple2<? extends K2, ? extends V2> mappedEntry : mapper.apply(entry._1, entry._2)) { acc = acc.put(mappedEntry); } return acc; }); }
/** * Construct Map with given values and key order. * * @param list The list of key-value tuples with unique keys. * @param map The map of key-value tuples. * @param <K> The key type * @param <V> The value type * @return A new Map containing the given map with given key order */ private static <K, V> LinkedHashMap<K, V> wrap(Queue<Tuple2<K, V>> list, HashMap<K, V> map) { return list.isEmpty() ? empty() : new LinkedHashMap<>(list, map); }
/** * Returns a {@code LinkedHashMap}, from a source java.util.Map. * * @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> LinkedHashMap<K, V> ofAll(java.util.Map<? extends K, ? extends V> map) { Objects.requireNonNull(map, "map is null"); LinkedHashMap<K, V> result = LinkedHashMap.empty(); for (java.util.Map.Entry<? extends K, ? extends V> entry : map.entrySet()) { result = result.put(entry.getKey(), entry.getValue()); } return result; }
/** * Creates a LinkedHashSet of the given elements. * * <pre><code>LinkedHashSet.of(1, 2, 3, 4)</code></pre> * * @param <T> Component type of the LinkedHashSet. * @param elements Zero or more elements. * @return A set containing the given elements. * @throws NullPointerException if {@code elements} is null */ @SafeVarargs public static <T> LinkedHashSet<T> of(T... elements) { Objects.requireNonNull(elements, "elements is null"); LinkedHashMap<T, Object> map = LinkedHashMap.empty(); for (T element : elements) { map = map.put(element, element); } return map.isEmpty() ? LinkedHashSet.empty() : new LinkedHashSet<>(map); }
/** * Creates a LinkedHashSet of the given elements. * * @param elements Set elements * @param <T> The value type * @return A new LinkedHashSet containing the given entries */ @SuppressWarnings("unchecked") public static <T> LinkedHashSet<T> ofAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (elements instanceof LinkedHashSet) { return (LinkedHashSet<T>) elements; } else { final LinkedHashMap<T, Object> mao = addAll(LinkedHashMap.empty(), elements); return mao.isEmpty() ? empty() : new LinkedHashSet<>(mao); } }
@Override public <K2, V2> LinkedHashMap<K2, V2> bimap(Function<? super K, ? extends K2> keyMapper, Function<? super V, ? extends V2> valueMapper) { Objects.requireNonNull(keyMapper, "keyMapper is null"); Objects.requireNonNull(valueMapper, "valueMapper is null"); final Iterator<Tuple2<K2, V2>> entries = iterator().map(entry -> Tuple.of(keyMapper.apply(entry._1), valueMapper.apply(entry._2))); return LinkedHashMap.ofEntries(entries); }
@Test public void testLinkedHashMap() throws Exception { LinkedHashMap<String, A> src = LinkedHashMap.of("a", new B("a", "b")); String json = MAPPER.writeValueAsString(new LinkedHashMapPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"a\":{\"ExtFieldsPojoTest$B\":{\"a\":\"a\",\"b\":\"b\"}}}}"); LinkedHashMapPojo pojo = MAPPER.readValue(json, LinkedHashMapPojo.class); LinkedHashMap<String, A> restored = pojo.getValue(); Assert.assertTrue(restored.get("a").get() instanceof B); Assert.assertEquals(restored.get("a").get().a, "a"); Assert.assertEquals(((B) restored.get("a").get()).b, "b"); }
public io.vavr.collection.LinkedHashMap<String, String> build() { return io.vavr.collection.LinkedHashMap.ofAll(metaData); }
/** * Associates the specified value with the specified key in this map. * If the map previously contained a mapping for the key, the old value is * replaced by the specified value. * <p> * Note that this method has a worst-case linear complexity. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return A new Map containing these elements and that entry. */ @Override public LinkedHashMap<K, V> put(K key, V value) { final Queue<Tuple2<K, V>> newList; final Option<V> currentEntry = get(key); if (currentEntry.isDefined()) { newList = list.replace(Tuple.of(key, currentEntry.get()), Tuple.of(key, value)); } else { newList = list.append(Tuple.of(key, value)); } final HashMap<K, V> newMap = map.put(key, value); return wrap(newList, newMap); }
@Override public boolean contains(T element) { return map.get(element).isDefined(); }
/** * Construct Map with given values and key order. * * @param list The list of key-value tuples with non-unique keys. * @param map The map of key-value tuples. * @param <K> The key type * @param <V> The value type * @return A new Map containing the given map with given key order */ private static <K, V> LinkedHashMap<K, V> wrapNonUnique(Queue<Tuple2<K, V>> list, HashMap<K, V> map) { return list.isEmpty() ? empty() : new LinkedHashMap<>(list.reverse().distinctBy(Tuple2::_1).reverse().toQueue(), map); }
/** * Read an object from a deserialization stream. * * @param s An object deserialization stream. * @throws ClassNotFoundException If the object's class read from the stream cannot be found. * @throws InvalidObjectException If the stream contains no list elements. * @throws IOException If an error occurs reading from the stream. */ private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); final int size = s.readInt(); if (size < 0) { throw new InvalidObjectException("No elements"); } LinkedHashMap<T, Object> temp = LinkedHashMap.empty(); for (int i = 0; i < size; i++) { @SuppressWarnings("unchecked") final T element = (T) s.readObject(); temp = temp.put(element, element); } map = temp; }