Java - How to create new Entry (key, value)

前端 未结 11 1510
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 09:55

I\'d like to create new item that similarly to Util.Map.Entry that will contain the structure key, value.

The problem is that

相关标签:
11条回答
  • 2020-11-27 10:13

    Why Map.Entry? I guess something like a key-value pair is fit for the case.

    Use java.util.AbstractMap.SimpleImmutableEntry or java.util.AbstractMap.SimpleEntry

    0 讨论(0)
  • 2020-11-27 10:14

    You could actually go with: Map.Entry<String, String> en= Maps.immutableEntry(key, value);

    0 讨论(0)
  • 2020-11-27 10:22

    org.apache.commons.lang3.tuple.Pair implements java.util.Map.Entry and can also be used standalone.

    Also as others mentioned Guava's com.google.common.collect.Maps.immutableEntry(K, V) does the trick.

    I prefer Pair for its fluent Pair.of(L, R) syntax.

    0 讨论(0)
  • 2020-11-27 10:23

    Try Maps.immutableEntry from Guava

    This has the advantage of being compatible with Java 5 (unlike AbstractMap.SimpleEntry which requires Java 6.)

    0 讨论(0)
  • 2020-11-27 10:23

    If you are using Clojure, you have another option:

    (defn map-entry
      [k v]
      (clojure.lang.MapEntry/create k v))
    
    0 讨论(0)
提交回复
热议问题