Java 中的 KeyValuePair

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2973041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:04:41  来源:igfitidea点击:

A KeyValuePair in Java

javakey-value

提问by maayank

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.

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

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

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

采纳答案by Eyal Schneider

The class AbstractMap.SimpleEntryis generic and can be useful.

AbstractMap.SimpleEntry类是通用的,很有用。

回答by kreker

Android programmers could use BasicNameValuePair

Android 程序员可以使用BasicNameValuePair

Update:

更新:

BasicNameValuePairis now deprecated (API 22). Use Pairinstead.

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

Example usage:

用法示例:

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

回答by remipod

The Pair class from Commons Lang might help:

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

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

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

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

回答by Neoheurist

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;
    }
}

回答by AaronCarson

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

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

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

回答by Matthew Park

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.

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

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

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

回答by Garret Wilson

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

NameValuePairGlobalMentor 的核心库中发布了一个类,可在 Maven 中使用。这是一个历史悠久的持续项目,因此请提交任何更改或改进请求。

回答by Nathan Clark Baumgartner

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

如果您熟悉哈希表,您就会对此非常熟悉

回答by Oleg Mikhailov

Hashtable<String, Object>

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

它比java.util.Properties实际上是Hashtable<Object, Object>.

回答by Mohhamed Nabil

You can create your custom KeyValuePair class easily

您可以轻松创建自定义 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;
    }

}