What is the equivalent of the C++ Pair in Java?

前端 未结 30 1066
一个人的身影
一个人的身影 2020-11-22 02:29

Is there a good reason why there is no Pair in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own.<

相关标签:
30条回答
  • 2020-11-22 03:03

    Another way to implement Pair with.

    • Public immutable fields, i.e. simple data structure.
    • Comparable.
    • Simple hash and equals.
    • Simple factory so you don't have to provide the types. e.g. Pair.of("hello", 1);

      public class Pair<FIRST, SECOND> implements Comparable<Pair<FIRST, SECOND>> {
      
          public final FIRST first;
          public final SECOND second;
      
          private Pair(FIRST first, SECOND second) {
              this.first = first;
              this.second = second;
          }
      
          public static <FIRST, SECOND> Pair<FIRST, SECOND> of(FIRST first,
                  SECOND second) {
              return new Pair<FIRST, SECOND>(first, second);
          }
      
          @Override
          public int compareTo(Pair<FIRST, SECOND> o) {
              int cmp = compare(first, o.first);
              return cmp == 0 ? compare(second, o.second) : cmp;
          }
      
          // todo move this to a helper class.
          private static int compare(Object o1, Object o2) {
              return o1 == null ? o2 == null ? 0 : -1 : o2 == null ? +1
                      : ((Comparable) o1).compareTo(o2);
          }
      
          @Override
          public int hashCode() {
              return 31 * hashcode(first) + hashcode(second);
          }
      
          // todo move this to a helper class.
          private static int hashcode(Object o) {
              return o == null ? 0 : o.hashCode();
          }
      
          @Override
          public boolean equals(Object obj) {
              if (!(obj instanceof Pair))
                  return false;
              if (this == obj)
                  return true;
              return equal(first, ((Pair) obj).first)
                      && equal(second, ((Pair) obj).second);
          }
      
          // todo move this to a helper class.
          private boolean equal(Object o1, Object o2) {
              return o1 == null ? o2 == null : (o1 == o2 || o1.equals(o2));
          }
      
          @Override
          public String toString() {
              return "(" + first + ", " + second + ')';
          }
      }
      
    0 讨论(0)
  • 2020-11-22 03:04

    It depends on what you want to use it for. The typical reason to do so is to iterate over maps, for which you simply do this (Java 5+):

    Map<String, Object> map = ... ; // just an example
    for (Map.Entry<String, Object> entry : map.entrySet()) {
      System.out.printf("%s -> %s\n", entry.getKey(), entry.getValue());
    }
    
    0 讨论(0)
  • 2020-11-22 03:04
    Collections.singletonMap(left, rigth);
    
    0 讨论(0)
  • 2020-11-22 03:06

    You can use javafx utility class, Pair which serves the same purpose as pair <> in c++. https://docs.oracle.com/javafx/2/api/javafx/util/Pair.html

    0 讨论(0)
  • 2020-11-22 03:08

    For programming languages like Java, the alternate data structure used by most programmers to represent pair like data-structures are two array, and data is accessed via the same index

    example: http://www-igm.univ-mlv.fr/~lecroq/string/node8.html#SECTION0080

    This isn't ideal as the data should be bound together, but also turn out to be pretty cheap. Also, if your use case demands storing co-ordinates then its better to build your own data structure.

    I've something like this in my library

    public class Pair<First,Second>{.. }
    
    0 讨论(0)
  • 2020-11-22 03:09

    Despite being syntactically similar, Java and C++ have very different paradigms. Writing C++ like Java is bad C++, and writing Java like C++ is bad Java.

    With a reflection based IDE like Eclipse, writing the necessarily functionality of a "pair" class is quick and simple. Create class, define two fields, use the various "Generate XX" menu options to fill out the class in a matter of seconds. Maybe you'd have to type a "compareTo" real quick if you wanted the Comparable interface.

    With separate declaration / definition options in the language C++ code generators aren't so good, so hand writing little utility classes is more time consuming tedium. Because the pair is a template, you don't have to pay for functions you don't use, and the typedef facility allows assigning meaningful typenames to the code, so the objections about "no semantics" don't really hold up.

    0 讨论(0)
提交回复
热议问题