/** * Returns a LinkedHashMultimap 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 <V2> The value type * @param n The number of elements in the LinkedHashMultimap * @param f The Function computing element values * @return A LinkedHashMultimap consisting of elements {@code f(0),f(1), ..., f(n - 1)} * @throws NullPointerException if {@code f} is null */ @SuppressWarnings("unchecked") public <K, V2 extends V> LinkedHashMultimap<K, V2> tabulate(int n, Function<? super Integer, ? extends Tuple2<? extends K, ? extends V2>> f) { Objects.requireNonNull(f, "f is null"); return ofEntries(Collections.tabulate(n, (Function<? super Integer, ? extends Tuple2<K, V2>>) f)); }
/** * Creates a {@code LinkedHashMultimap} of the given entries. * * @param <K> The key type * @param <V2> The value type * @param entries Multimap entries * @return A new LinkedHashMultimap containing the given entries. */ @SafeVarargs public final <K, V2 extends V> LinkedHashMultimap<K, V2> ofEntries(java.util.Map.Entry<? extends K, ? extends V2>... entries) { Objects.requireNonNull(entries, "entries is null"); LinkedHashMultimap<K, V2> result = empty(); for (java.util.Map.Entry<? extends K, ? extends V2> entry : entries) { result = result.put(entry.getKey(), entry.getValue()); } return result; }
cases.put("LinkedHashMultimapOfSeqString", LinkedHashMultimap.withSeq().of("A", "B", "A", "C")); cases.put("LinkedHashMultimapOfSeqTuple", LinkedHashMultimap.withSeq().of("A", Tuple.of("A", "B"), "A", Tuple.of("C", "D"))); cases.put("TreeMultimapOfSeqString", TreeMultimap.withSet().of("A", "B", "A", "C")); cases.put("TreeMultimapOfSeqTuple", TreeMultimap.withSet().of("A", Tuple.of("A", "B"), "A", Tuple.of("C", "D")));
/** * Creates a LinkedHashMultimap 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 <K> The key type * @param <V2> The value type * @return A new Multimap containing the given entries */ public <K, V2 extends V> LinkedHashMultimap<K, V2> of(K k1, V2 v1, K k2, V2 v2, K k3, V2 v3, K k4, V2 v4, K k5, V2 v5, K k6, V2 v6, K k7, V2 v7, K k8, V2 v8, K k9, V2 v9, K k10, V2 v10) { return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9).put(k10, v10); }
/** * Creates a LinkedHashMultimap 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 <K> The key type * @param <V2> The value type * @return A new Multimap containing the given entries */ public <K, V2 extends V> LinkedHashMultimap<K, V2> of(K k1, V2 v1, K k2, V2 v2, K k3, V2 v3, K k4, V2 v4, K k5, V2 v5, K k6, V2 v6, K k7, V2 v7, K k8, V2 v8, K k9, V2 v9) { return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8).put(k9, v9); }
/** * Creates a LinkedHashMultimap 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 <K> The key type * @param <V2> The value type * @return A new Multimap containing the given entries */ public <K, V2 extends V> LinkedHashMultimap<K, V2> of(K k1, V2 v1, K k2, V2 v2, K k3, V2 v3, K k4, V2 v4, K k5, V2 v5, K k6, V2 v6, K k7, V2 v7, K k8, V2 v8) { return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7).put(k8, v8); }
@Override public Multimap<?, ?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { final java.util.List<Tuple2<Object, Object>> result = new java.util.ArrayList<>(); while (p.nextToken() != JsonToken.END_OBJECT) { String name = p.getCurrentName(); Object key = keyDeserializer.deserializeKey(name, ctxt); p.nextToken(); ArrayList<?> list = (ArrayList<?>) containerDeserializer.deserialize(p, ctxt); for (Object elem : list) { result.add(Tuple.of(key, elem)); } } if (TreeMultimap.class.isAssignableFrom(handledType())) { return TreeMultimap.withSeq().ofEntries(keyComparator, result); } if (LinkedHashMultimap.class.isAssignableFrom(handledType())) { return LinkedHashMultimap.withSeq().ofEntries(result); } // default deserialization [...] -> Map return HashMultimap.withSeq().ofEntries(result); } }
/** * Creates a LinkedHashMultimap 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 <K> The key type * @param <V2> The value type * @return A new Multimap containing the given entries */ public <K, V2 extends V> LinkedHashMultimap<K, V2> of(K k1, V2 v1, K k2, V2 v2, K k3, V2 v3, K k4, V2 v4, K k5, V2 v5, K k6, V2 v6, K k7, V2 v7) { return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6).put(k7, v7); }
/** * Creates a LinkedHashMultimap 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 <K> The key type * @param <V2> The value type * @return A new Multimap containing the given entries */ public <K, V2 extends V> LinkedHashMultimap<K, V2> of(K k1, V2 v1, K k2, V2 v2, K k3, V2 v3, K k4, V2 v4, K k5, V2 v5, K k6, V2 v6) { return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5).put(k6, v6); }
@Test public void testLinkedHashMultimapOfSeqTuple() throws Exception { String src00 = "A"; String src010 = "A"; String src011 = "B"; Tuple2<String, String> src01 = Tuple.of(src010, src011); Tuple2<String, Tuple2<String, String>> src0 = Tuple.of(src00, src01); String src10 = "A"; String src110 = "C"; String src111 = "D"; Tuple2<String, String> src11 = Tuple.of(src110, src111); Tuple2<String, Tuple2<String, String>> src1 = Tuple.of(src10, src11); LinkedHashMultimap<String, Tuple2<String, String>> src = LinkedHashMultimap.withSeq().ofEntries(src0, src1); String json = MAPPER.writeValueAsString(new ParameterizedLinkedHashMultimapPojo<>(src)); Assert.assertEquals(json, "{\"value\":{\"A\":[[\"A\",\"B\"],[\"C\",\"D\"]]}}"); ParameterizedLinkedHashMultimapPojo<java.lang.String, io.vavr.Tuple2<java.lang.String, java.lang.String>> restored = MAPPER.readValue(json, new TypeReference<ParameterizedLinkedHashMultimapPojo<java.lang.String, io.vavr.Tuple2<java.lang.String, java.lang.String>>>(){}); Assert.assertEquals(src, restored.getValue()); }
/** * Creates a LinkedHashMultimap 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 <K> The key type * @param <V2> The value type * @return A new Multimap containing the given entries */ public <K, V2 extends V> LinkedHashMultimap<K, V2> of(K k1, V2 v1, K k2, V2 v2, K k3, V2 v3, K k4, V2 v4, K k5, V2 v5) { return of(k1, v1, k2, v2, k3, v3, k4, v4).put(k5, v5); }
@Test public void testLinkedHashMultimapOfSeqTuple() throws Exception { String src00 = "A"; String src010 = "A"; String src011 = "B"; Tuple2<String, String> src01 = Tuple.of(src010, src011); Tuple2<String, Tuple2<String, String>> src0 = Tuple.of(src00, src01); String src10 = "A"; String src110 = "C"; String src111 = "D"; Tuple2<String, String> src11 = Tuple.of(src110, src111); Tuple2<String, Tuple2<String, String>> src1 = Tuple.of(src10, src11); LinkedHashMultimap<String, Tuple2<String, String>> src = LinkedHashMultimap.withSeq().ofEntries(src0, src1); String json = MAPPER.writeValueAsString(new LinkedHashMultimapOfSeqTuple().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"A\":[[\"A\",\"B\"],[\"C\",\"D\"]]}}"); LinkedHashMultimapOfSeqTuple restored = MAPPER.readValue(json, LinkedHashMultimapOfSeqTuple.class); Assert.assertEquals(src, restored.getValue()); }
/** * Creates a LinkedHashMultimap 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 <K> The key type * @param <V2> The value type * @return A new Multimap containing the given entries */ public <K, V2 extends V> LinkedHashMultimap<K, V2> of(K k1, V2 v1, K k2, V2 v2, K k3, V2 v3, K k4, V2 v4) { return of(k1, v1, k2, v2, k3, v3).put(k4, v4); }
/** * Creates a {@code LinkedHashMultimap} of the given entries. * * @param <K> The key type * @param <V2> The value type * @param entries Multimap entries * @return A new LinkedHashMultimap containing the given entries. */ @SafeVarargs public final <K, V2 extends V> LinkedHashMultimap<K, V2> ofEntries(java.util.Map.Entry<? extends K, ? extends V2>... entries) { Objects.requireNonNull(entries, "entries is null"); LinkedHashMultimap<K, V2> result = empty(); for (java.util.Map.Entry<? extends K, ? extends V2> entry : entries) { result = result.put(entry.getKey(), entry.getValue()); } return result; }
/** * Creates a {@code LinkedHashMultimap} of the given entries. * * @param <K> The key type * @param <V2> The value type * @param entries Multimap entries * @return A new LinkedHashMultimap containing the given entries. */ @SafeVarargs public final <K, V2 extends V> LinkedHashMultimap<K, V2> ofEntries(Tuple2<? extends K, ? extends V2>... entries) { Objects.requireNonNull(entries, "entries is null"); LinkedHashMultimap<K, V2> result = empty(); for (Tuple2<? extends K, ? extends V2> entry : entries) { result = result.put(entry._1, entry._2); } return result; }
/** * Creates a LinkedHashMultimap 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 <K> The key type * @param <V2> The value type * @return A new Multimap containing the given entries */ public <K, V2 extends V> LinkedHashMultimap<K, V2> of(K k1, V2 v1, K k2, V2 v2, K k3, V2 v3) { return of(k1, v1, k2, v2).put(k3, v3); }
@Test public void testLinkedHashMultimapOfSeqString() throws Exception { String src00 = "A"; String src01 = "B"; Tuple2<String, String> src0 = Tuple.of(src00, src01); String src10 = "A"; String src11 = "C"; Tuple2<String, String> src1 = Tuple.of(src10, src11); LinkedHashMultimap<String, String> src = LinkedHashMultimap.withSeq().ofEntries(src0, src1); String json = MAPPER.writeValueAsString(new LinkedHashMultimapOfSeqString().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"A\":[\"B\",\"C\"]}}"); LinkedHashMultimapOfSeqString restored = MAPPER.readValue(json, LinkedHashMultimapOfSeqString.class); Assert.assertEquals(src, restored.getValue()); }
@Test public void testLinkedHashMultimapOfSeqString() throws Exception { String src00 = "A"; String src01 = "B"; Tuple2<String, String> src0 = Tuple.of(src00, src01); String src10 = "A"; String src11 = "C"; Tuple2<String, String> src1 = Tuple.of(src10, src11); LinkedHashMultimap<String, String> src = LinkedHashMultimap.withSeq().ofEntries(src0, src1); String json = MAPPER.writeValueAsString(new ParameterizedLinkedHashMultimapPojo<>(src)); Assert.assertEquals(json, "{\"value\":{\"A\":[\"B\",\"C\"]}}"); ParameterizedLinkedHashMultimapPojo<java.lang.String, java.lang.String> restored = MAPPER.readValue(json, new TypeReference<ParameterizedLinkedHashMultimapPojo<java.lang.String, java.lang.String>>(){}); Assert.assertEquals(src, restored.getValue()); }
/** * Creates a {@code LinkedHashMultimap} of the given entries. * * @param <K> The key type * @param <V2> The value type * @param entries Multimap entries * @return A new LinkedHashMultimap containing the given entries. */ public <K, V2 extends V> LinkedHashMultimap<K, V2> ofEntries(Iterable<? extends Tuple2<? extends K, ? extends V2>> entries) { Objects.requireNonNull(entries, "entries is null"); LinkedHashMultimap<K, V2> result = empty(); for (Tuple2<? extends K, ? extends V2> entry : entries) { result = result.put(entry._1, entry._2); } return result; }
/** * Returns a {@code LinkedHashMultimap}, from entries mapped from stream. * * @param stream the source stream * @param keyMapper the key mapper * @param valueMapper the value mapper * @param <T> The stream element type * @param <K> The key type * @param <V2> The value type * @return A new Multimap */ public <T, K, V2 extends V> LinkedHashMultimap<K, V2> ofAll(java.util.stream.Stream<? extends T> stream, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V2> valueMapper) { return Multimaps.ofStream(empty(), stream, keyMapper, valueMapper); }