Correct type listing (or any other method) for the Java Generic Class

In this question, What is the equivalent of C ++ Pair <L, R> in Java? , there is this example code that I will use in my code

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

Eclipse warns me on the line "Pair otherPair = (Pair)"; that "Pair is a raw type. References to the general type of Pair must be parameterized." I tried to rewrite it to “Pair”, “B” and “Other” = (“Pair”, “B”) another; but the warning still exists. How can I print another correctly so that there are no warnings? Thank!

+3
source share
1 answer

you can use

Pair<?, ?> otherPair = (Pair<?, ?>)other;

equals , . .

+3

Source: https://habr.com/ru/post/1792347/


All Articles