价值对的Java集合? (元组?)

本文翻译自:A Java collection of value pairs? (tuples?)

I like how Java has a Map where you can define the types of each entry in the map, for example <String, Integer> . 我喜欢Java有一个Map,你可以在其中定义地图中每个条目的类型,例如<String, Integer>

What I'm looking for is a type of collection where each element in the collection is a pair of values. 我正在寻找的是一种集合,其中集合中的每个元素都是一对值。 Each value in the pair can have its own type (like the String and Integer example above), which is defined at declaration time. 对中的每个值都可以有自己的类型(如上面的String和Integer示例),它是在声明时定义的。

The collection will maintain its given order and will not treat one of the values as a unique key (as in a map). 该集合将保持其给定的顺序,并且不会将其中一个值视为唯一键(如在地图中)。

Essentially I want to be able to define an ARRAY of type <String,Integer> or any other 2 types. 基本上我希望能够定义类型<String,Integer>或任何其他2种类型的ARRAY。

I realize that I can make a class with nothing but the 2 variables in it, but that seems overly verbose. 我意识到我可以创建一个只有2个变量的类,但这看起来过于冗长。

I also realize that I could use a 2D array, but because of the different types I need to use, I'd have to make them arrays of OBJECT, and then I'd have to cast all the time. 我也意识到我可以使用2D数组,但由于我需要使用不同的类型,我必须使它们成为OBJECT数组,然后我必须一直投射。

I only need to store pairs in the collection, so I only need two values per entry. 我只需要在集合中存储对,所以每个条目只需要两个值。 Does something like this exist without going the class route? 如果没有上课路线,这样的事情会存在吗? Thanks! 谢谢!


#1楼

参考:https://stackoom.com/question/2BZz/价值对的Java集合-元组


#2楼

那com.sun.tools.javac.util.Pair怎么样?


#3楼

AbstractMap.SimpleEntry AbstractMap.SimpleEntry

Easy you are looking for this: 你很容易找到这个:

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

How can you fill it? 你怎么能填补它?

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

This simplifies to: 这简化为:

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

And, with the help of a createEntry method, can further reduce the verbosity to: 并且,在createEntry方法的帮助下,可以进一步减少详细程度:

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

Since ArrayList isn't final, it can be subclassed to expose an of method (and the aforementioned createEntry method), resulting in the syntactically terse: 由于ArrayList不是终点,它可以被继承到露出of方法(和前述createEntry方法),从而在语法上简洁:

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

#4楼

Apache common lang3 has Pair class and few other libraries mentioned in this thread What is the equivalent of the C++ Pair<L,R> in Java? Apache常见的lang3有Pair类和这个线程中提到的其他几个库什么相当于Java中的C ++ Pair <L,R>?

Example matching the requirement from your original question: 匹配原始问题的要求的示例:

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

#5楼

Expanding on the other answers a generic immutable Pair should have a static method to avoid cluttering your code with the call to the constructor: 扩展其他答案,通用不可变对应该有一个静态方法,以避免在调用构造函数时混乱代码:

class Pair<L,R> {
      final L left;
      final R right;

      public Pair(L left, R right) {
        this.left = left;
        this.right = right;
      }

      static <L,R> Pair<L,R> of(L left, R right){
          return new Pair<L,R>(left, right);
      }
}

if you name the static method "of" or "pairOf" the code becomes fluent as you can write either: 如果您将静态方法命名为“of”或“pairOf”,则代码会变得流畅,因为您可以编写:

    list.add(Pair.of(x,y)); // my preference
    list.add(pairOf(x,y)); // use with import static x.y.Pair.pairOf

its a real shame that the core java libraries are so sparse on such things that you have to use commons-lang or other 3rd parties to do such basic stuff. 真正的耻辱是核心java库在这些事情上如此稀疏,你必须使用commons-lang或其他第三方来做这些基本的东西。 yet another reason to upgrade to scala... 升级到scala的另一个原因......


#6楼

Apache Crunch还有一个Pair类: http//crunch.apache.org/apidocs/0.5.0/org/apache/crunch/Pair.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值