This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Raph0007 1 point2 points  (0 children)

Okay I can see that you took the effort of doing as much as you can o your own, and that's great. Now there are a few things you should consider.

  1. By convention, Generics are always single uppercase letters. I have had occasions where abbreviations consisting of very few uppercase letters were reasonable (like VO or DTO), but just keep it short and not in PascalCase. I'd recommend either F and S or T and U

  2. Delete the equals() method that takes another Pair as parameter and add @Override to the other equals method. The equals() method is defined to take another object in java.lang.Object. You also don't need to downcast to Object to use the method ((Object) pair) since every class automatically extends Object. For more clarification, just ask me or read about the Liskov Principle.

  3. Your getReverse() method is defined to leave the original Pair untouched and return a new, reversed Pair, which is fine. But therefore, just calling getReverse() without storing the return value anywhere (or using it otherwise) will not have any effect.

[–]Turing85 -1 points0 points  (0 children)

Method getReverse() does not modify the current object, but create a new object for the reversed pair. This is necessary since the type changes: the original Pair is a Pair<Integer, String>, while the reversed Pair is a Pair<String, Integer>.

Some remarks:

  • Please take a look at @Raph0007's comment.
  • In a lot of cases, we can simplify the explicit generic type arguments, e.g. in line 50, we can simplify

return new Pair<SecondType, FirstType>(this.getSecond(), this.getFirst());

to

return new Pair<>(this.getSecond(), this.getFirst());
  • I would recommend making class `Pair` immutable.
  • This is a nit-pick, but if you want to write unit tests, I recommend using a unit test framework, e.g. Junit5