I understand Java best practice suggests that while declaring a variable the generic set interface is used to declare on the left and the specific implementation on the right.
Thus if I have to declare the Set interfaces, the right way is,
Set<String> set = new TreeSet<>();
However with this declaration I'm not able to access the set.last()
method.
However when I declare it this way,
TreeSet<String> set = new TreeSet<>();
I can access, last()
and first()
.
Can someone help me understand why?