Java 中的键值对

发布于 2024-09-03 19:40:12 字数 140 浏览 4 评论 0原文

我正在寻找 Java 中的 KeyValuePair 类。
由于 java.util 大量使用接口,因此没有提供具体的实现,仅提供 Map.Entry 接口。

我可以导入一些规范的实现吗? 这是我讨厌执行 100 次的“管道工编程”课程之一。

I'm looking for a KeyValuePair class in Java.
Since java.util heavily uses interfaces there is no concrete implementation provided, only the Map.Entry interface.

Is there some canonical implementation I can import?
It is one of those "plumbers programming" classes I hate to implement 100x times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

燕归巢 2024-09-10 19:40:12

AbstractMap.SimpleEntry 是通用的并且很有用。

The class AbstractMap.SimpleEntry is generic and can be useful.

勿忘心安 2024-09-10 19:40:12

Android 程序员可以使用 BasicNameValuePair

更新:< /strong>

BasicNameValuePair 现已弃用 (API 22) 。
请改用配对

用法示例:

Pair<Integer, String> simplePair = new Pair<>(42, "Second");
Integer first = simplePair.first; // 42
String second = simplePair.second; // "Second"

Android programmers could use BasicNameValuePair

Update:

BasicNameValuePair is now deprecated (API 22).
Use Pair instead.

Example usage:

Pair<Integer, String> simplePair = new Pair<>(42, "Second");
Integer first = simplePair.first; // 42
String second = simplePair.second; // "Second"
骄傲 2024-09-10 19:40:12

Commons Lang 的 Pair 类可能会有所帮助:

Pair<String, String> keyValue = new ImmutablePair("key", "value");

当然,您需要包含 commons-lang。

The Pair class from Commons Lang might help:

Pair<String, String> keyValue = new ImmutablePair("key", "value");

Of course, you would need to include commons-lang.

神仙妹妹 2024-09-10 19:40:12

使用 javafx.util.Pair 对于大多数人来说就足够了可以实例化的任意两种类型的简单键值对。

Pair<Integer, String> myPair = new Pair<>(7, "Seven");
Integer key = myPair.getKey();
String value = myPair.getValue();

Use of javafx.util.Pair is sufficient for most simple Key-Value pairings of any two types that can be instantiated.

Pair<Integer, String> myPair = new Pair<>(7, "Seven");
Integer key = myPair.getKey();
String value = myPair.getValue();
影子的影子 2024-09-10 19:40:12
import java.util.Map;

public class KeyValue<K, V> implements Map.Entry<K, V>
{
    private K key;
    private V value;

    public KeyValue(K key, V value)
    {
        this.key = key;
        this.value = value;
    }

    public K getKey()
    {
        return this.key;
    }

    public V getValue()
    {
        return this.value;
    }

    public K setKey(K key)
    {
        return this.key = key;
    }

    public V setValue(V value)
    {
        return this.value = value;
    }
}
import java.util.Map;

public class KeyValue<K, V> implements Map.Entry<K, V>
{
    private K key;
    private V value;

    public KeyValue(K key, V value)
    {
        this.key = key;
        this.value = value;
    }

    public K getKey()
    {
        return this.key;
    }

    public V getValue()
    {
        return this.value;
    }

    public K setKey(K key)
    {
        return this.key = key;
    }

    public V setValue(V value)
    {
        return this.value = value;
    }
}
随风而去 2024-09-10 19:40:12

我喜欢使用

属性

示例:

Properties props = new Properties();

props.setProperty("displayName", "Jim Wilson"); // (key, value)

String name = props.getProperty("displayName"); // => Jim Wilson

String acctNum = props.getProperty("accountNumber"); // => null

String nextPosition = props.getProperty("position", "1"); // => 1

如果您熟悉哈希表,您已经对此非常熟悉了

I like to use

Properties

Example:

Properties props = new Properties();

props.setProperty("displayName", "Jim Wilson"); // (key, value)

String name = props.getProperty("displayName"); // => Jim Wilson

String acctNum = props.getProperty("accountNumber"); // => null

String nextPosition = props.getProperty("position", "1"); // => 1

If you are familiar with a hash table you will be pretty familiar with this already

寂寞美少年 2024-09-10 19:40:12

您可以轻松创建自定义 KeyValuePair 类

public class Key<K, V>{

    K key;
    V value;

    public Key() {

    }

    public Key(K key, V  value) {
        this.key = key;
        this.value = value;
    }

    public void setValue(V value) {
        this.value = value;
    }
    public V getValue() {
        return value;
    }

    public void setKey(K key) {
        this.key = key;
    }
    public K getKey() {
        return key;
    }

}

You can create your custom KeyValuePair class easily

public class Key<K, V>{

    K key;
    V value;

    public Key() {

    }

    public Key(K key, V  value) {
        this.key = key;
        this.value = value;
    }

    public void setValue(V value) {
        this.value = value;
    }
    public V getValue() {
        return value;
    }

    public void setKey(K key) {
        this.key = key;
    }
    public K getKey() {
        return key;
    }

}
沫尐诺 2024-09-10 19:40:12

我最喜欢的是

HashMap<Type1, Type2>

您所要做的就是指定 Type1 的键的数据类型和 Type2 的值的数据类型。这是我在 Java 中见过的最常见的键值对象。

https://docs.oracle.com/javase/ 7/docs/api/java/util/HashMap.html

My favorite is

HashMap<Type1, Type2>

All you have to do is specify the datatype for the key for Type1 and the datatype for the value for Type2. It's the most common key-value object I've seen in Java.

https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

葬シ愛 2024-09-10 19:40:12
Hashtable<String, Object>

它比 java.util.Properties 更好,后者实际上是 Hashtable 的扩展。

Hashtable<String, Object>

It is better than java.util.Properties which is by fact an extension of Hashtable<Object, Object>.

那伤。 2024-09-10 19:40:12

我发布了 < GlobalMentor 的核心库,在 Maven 中可用。这是一个历史悠久的正在进行的项目,因此请提交任何更改或改进请求。

I've published a NameValuePair class in GlobalMentor's core library, available in Maven. This is an ongoing project with a long history, so please submit any request for changes or improvements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文