Immutable value object
Java
Easy
4 views
Problem Description
Task: create immutable Pair (a,b) with equals and hashCode.
Output Format
Return value
Constraints
Follow equals contract.
Official Solution
static class Pair{final int a;final int b;Pair(int a,int b){this.a=a;this.b=b;}public boolean equals(Object o){if(!(o instanceof Pair)) return false;Pair p=(Pair)o;return a==p.a && b==p.b;}public int hashCode(){return 31*a+b;}}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!