What is the best way to store three attributes entry sets in Java?

Whe*_*ble 1 java mapping

HashMaps allow for mapping key value pairs; what is the recommendation if there is an extra value?

i.e HashMap maps key-value pairs: Key, Value

What is the best way to map: Key, Value, Value1?

Thanks.

Lar*_*ren 5

我喜欢的一个漂亮,干净的解决方案是编写一个匹配一对的实用程序类:

public class Pair<U, V> {

    private U first;

    private V second;

public Pair(U first, V second) {

 this.first = first;
 this.second = second;
}

// getters for "first" and "second"
Run Code Online (Sandbox Code Playgroud)

然后将其作为Value地图中的内容:

Map<Key, Pair<U,V>> map;
Run Code Online (Sandbox Code Playgroud)

但是,这将是您始终只有两个值的情况.如果您感觉将来可能会有更多,那么在地图中使用List<Object>或者Set<Object>更好地使用它Value.

编辑你也可以有一个静态创建者方法:

public static <U, V> Pair<U, V> newInstance(U first, V second) {
        return new Pair<U, V>(first, second);
    }
Run Code Online (Sandbox Code Playgroud)