我喜欢Java有一个映射,你可以在映射中定义每个条目的类型,例如<String, Integer>。

我正在寻找的是一种类型的集合,其中集合中的每个元素都是一对值。对中的每个值都可以有自己的类型(如上面的String和Integer示例),该类型在声明时定义。

该集合将保持其给定的顺序,不会将其中一个值视为唯一键(就像在映射中一样)。

本质上,我希望能够定义一个数组类型<String,Integer>或任何其他2种类型。

我意识到,我可以让一个类只有2个变量,但这似乎过于啰嗦。

我也意识到我可以使用一个2D数组,但因为我需要使用不同的类型,我必须使他们OBJECT数组,然后我必须一直强制转换。

我只需要在集合中存储对,所以每个条目只需要两个值。像这样的东西不走阶级路线就存在吗?谢谢!


当前回答

Apache common lang3有Pair类和本线程中提到的少数其他库,在Java中等价于c++ Pair<L,R> ?

匹配你最初问题要求的例子:

List<Pair<String, Integer>> myPairs = new ArrayList<Pair<String, Integer>>();
myPairs.add(Pair.of("val1", 11));
myPairs.add(Pair.of("val2", 17));

//...

for(Pair<String, Integer> pair : myPairs) {
  //following two lines are equivalent... whichever is easier for you...
  System.out.println(pair.getLeft() + ": " + pair.getRight());
  System.out.println(pair.getKey() + ": " + pair.getValue());
}

其他回答

地图。条目

这些内置类也是一个选项。两者都实现了Map。输入接口。

AbstractMap。SimpleEntry AbstractMap。SimpleImmutableEntry

AbstractMap。SimpleEntry

很容易你正在寻找这个:

java.util.List<java.util.Map.Entry<String,Integer>> pairList= new java.util.ArrayList<>();

你怎么填满它?

java.util.Map.Entry<String,Integer> pair1=new java.util.AbstractMap.SimpleEntry<>("Not Unique key1",1);
java.util.Map.Entry<String,Integer> pair2=new java.util.AbstractMap.SimpleEntry<>("Not Unique key2",2);
pairList.add(pair1);
pairList.add(pair2);

这可以简化为:

Entry<String,Integer> pair1=new SimpleEntry<>("Not Unique key1",1);
Entry<String,Integer> pair2=new SimpleEntry<>("Not Unique key2",2);
pairList.add(pair1);
pairList.add(pair2);

并且,在createEntry方法的帮助下,可以进一步减少冗长:

pairList.add(createEntry("Not Unique key1", 1));
pairList.add(createEntry("Not Unique key2", 2));

由于ArrayList不是final,所以可以子类化它来公开of方法(以及前面提到的createEntry方法),从而在语法上简洁:

TupleList<java.util.Map.Entry<String,Integer>> pair = new TupleList<>();
pair.of("Not Unique key1", 1);
pair.of("Not Unique key2", 2);

这是基于JavaHelp4u的代码。

少啰嗦,并展示了如何在一行中做,以及如何在事情上循环。

//======>  Imports
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

//======>  Single Entry
SimpleEntry<String, String> myEntry = new SimpleEntry<String, String>("ID", "Text");
System.out.println("key: " + myEntry.getKey() + "    value:" + myEntry.getValue());
System.out.println();

//======>  List of Entries
List<Entry<String,String>> pairList = new ArrayList<>();

//-- Specify manually
Entry<String,String> firstButton = new SimpleEntry<String, String>("Red ", "Way out");
pairList.add(firstButton);

//-- one liner:
pairList.add(new SimpleEntry<String,String>("Gray", "Alternate route"));  //Ananomous add.

//-- Iterate over Entry array:
for (Entry<String, String> entr : pairList) {
    System.out.println("Button: " + entr.getKey() + "    Label: " + entr.getValue());
}

您需要的是某种对象的List。我个人不喜欢使用泛型的Pair类。他们有太多的缺点:

他们不太善于表达。除了类型参数外,它们不提供任何上下文信息。 它们阻止您使用基本类型 您不能对键或值应用任何约束(除了它们 当然是打字)。 它们迫使您在许多地方携带详细类型参数声明(尽管var和<>在某种程度上减轻了这一点)

我更喜欢使用特别类。虽然几年前,这是恼人的样板文件,但现在不再是这样了。

这里有两个替代选项,使得类的声明非常容易:

Java 14及以上版本:使用记录类

record TypedCount(String type, int count) {}

好了,就是这样。你可以免费获得toString()和equals()

Java 14之前:使用lombok的@Value注释

@Value
class TypedCount {
    String type;
    int count;
}

也是这样。Lombok自动将字段设置为私有final、构造函数、getter、toString()和equals()。

虽然这需要您将lombok作为依赖项添加,但这只是一个编译时依赖项,可以为您生成代码。lombok库在运行时不需要在类路径中。

Pair类是那些“给我”泛型示例之一,很容易自己编写。例如,在我的脑海中:

public class Pair<L,R> {

  private final L left;
  private final R right;

  public Pair(L left, R right) {
    assert left != null;
    assert right != null;

    this.left = left;
    this.right = right;
  }

  public L getLeft() { return left; }
  public R getRight() { return right; }

  @Override
  public int hashCode() { return left.hashCode() ^ right.hashCode(); }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Pair)) return false;
    Pair pairo = (Pair) o;
    return this.left.equals(pairo.getLeft()) &&
           this.right.equals(pairo.getRight());
  }

}

是的,它存在于网络上的多个地方,具有不同程度的完整性和特征。(我上面的例子是不可变的。)