A Java collection of value pairs? (tuples?)

后端 未结 19 2321
名媛妹妹
名媛妹妹 2020-11-22 05:43

I like how Java has a Map where you can define the types of each entry in the map, for example .

What I\'m looking for is a type

相关标签:
19条回答
  • 2020-11-22 05:44

    You could write a generic Pair<A, B> class and use this in an array or list. Yes, you have to write a class, but you can reuse the same class for all types, so you only have to do it once.

    0 讨论(0)
  • 2020-11-22 05:46

    The preferred solution as you've described it is a List of Pairs (i.e. List).

    To accomplish this you would create a Pair class for use in your collection. This is a useful utility class to add to your code base.

    The closest class in the Sun JDK providing functionality similar to a typical Pair class is AbstractMap.SimpleEntry. You could use this class rather than creating your own Pair class, though you would have to live with some awkward restrictions and I think most people would frown on this as not really the intended role of SimpleEntry. For example SimpleEntry has no "setKey()" method and no default constructor, so you may find it too limiting.

    Bear in mind that Collections are designed to contain elements of a single type. Related utility interfaces such as Map are not actually Collections (i.e. Map does not implement the Collection interface). A Pair would not implement the Collection interface either but is obviously a useful class in building larger data structures.

    0 讨论(0)
  • 2020-11-22 05:47

    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?

    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());
    }
    
    0 讨论(0)
  • 2020-11-22 05:47

    First Thing on my mind when talking about key/value pairs is the Properties Class where you can save and load items to a stream/file.

    0 讨论(0)
  • 2020-11-22 05:49

    The Pair class is one of those "gimme" generics examples that is easy enough to write on your own. For example, off the top of my head:

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

    And yes, this exists in multiple places on the Net, with varying degrees of completeness and feature. (My example above is intended to be immutable.)

    0 讨论(0)
  • 2020-11-22 05:49

    In project Reactor (io.projectreactor:reactor-core) there is advanced support for n-Tuples:

    Tuple2<String, Integer> t = Tuples.of("string", 1)
    

    There you can get t.getT1(), t.getT2(), ... Especially with Stream or Flux you can even map the tuple elements:

    Stream<Tuple2<String, Integer>> s;
    s.map(t -> t.mapT2(i -> i + 2));
    
    0 讨论(0)
提交回复
热议问题