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

前端 未结 30 1010
一个人的身影
一个人的身影 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 02:58

    HashMap compatible Pair class:

    public class Pair<A, B> {
        private A first;
        private B second;
    
        public Pair(A first, B second) {
            super();
            this.first = first;
            this.second = second;
        }
    
        public int hashCode() {
            int hashFirst = first != null ? first.hashCode() : 0;
            int hashSecond = second != null ? second.hashCode() : 0;
    
            return (hashFirst + hashSecond) * hashSecond + hashFirst;
        }
    
        public boolean equals(Object other) {
            if (other instanceof Pair) {
                Pair otherPair = (Pair) other;
                return 
                ((  this.first == otherPair.first ||
                    ( this.first != null && otherPair.first != null &&
                      this.first.equals(otherPair.first))) &&
                 (  this.second == otherPair.second ||
                    ( this.second != null && otherPair.second != null &&
                      this.second.equals(otherPair.second))) );
            }
    
            return false;
        }
    
        public String toString()
        { 
               return "(" + first + ", " + second + ")"; 
        }
    
        public A getFirst() {
            return first;
        }
    
        public void setFirst(A first) {
            this.first = first;
        }
    
        public B getSecond() {
            return second;
        }
    
        public void setSecond(B second) {
            this.second = second;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:59

    This is Java. You have to make your own tailored Pair class with descriptive class and field names, and not to mind that you will reinvent the wheel by writing hashCode()/equals() or implementing Comparable again and again.

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

    Good News JavaFX has a key value Pair.

    just add javafx as a dependency and import javafx.util.Pair;

    and use simply as in c++ .

    Pair <Key, Value> 
    

    e.g.

    Pair <Integer, Integer> pr = new Pair<Integer, Integer>()
    
    pr.get(key);// will return corresponding value
    
    0 讨论(0)
  • 2020-11-22 03:01

    As many others have already stated, it really depends on the use case if a Pair class is useful or not.

    I think for a private helper function it is totally legitimate to use a Pair class if that makes your code more readable and is not worth the effort of creating yet another value class with all its boiler plate code.

    On the other hand, if your abstraction level requires you to clearly document the semantics of the class that contains two objects or values, then you should write a class for it. Usually that's the case if the data is a business object.

    As always, it requires skilled judgement.

    For your second question I recommend the Pair class from the Apache Commons libraries. Those might be considered as extended standard libraries for Java:

    https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/Pair.html

    You might also want to have a look at Apache Commons' EqualsBuilder, HashCodeBuilder and ToStringBuilder which simplify writing value classes for your business objects.

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

    Pair would be a good stuff, to be a basic construction unit for a complex generics, for instance, this is from my code:

    WeakHashMap<Pair<String, String>, String> map = ...
    

    It is just the same as Haskell's Tuple

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

    In a thread on comp.lang.java.help, Hunter Gratzner gives some arguments against the presence of a Pair construct in Java. The main argument is that a class Pair doesn't convey any semantics about the relationship between the two values (how do you know what "first" and "second" mean ?).

    A better practice is to write a very simple class, like the one Mike proposed, for each application you would have made of the Pair class. Map.Entry is an example of a pair that carry its meaning in its name.

    To sum up, in my opinion it is better to have a class Position(x,y), a class Range(begin,end) and a class Entry(key,value) rather than a generic Pair(first,second) that doesn't tell me anything about what it's supposed to do.

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