Those changes look as if you were guessing. You can guess a million times and one of those guesses will probably be correct.
Or you can think about the problem and get it right first time.
You are not geting the same error. It might be the same Exception but it is a different error because the two posts show Sets filled with different types. The first Strings, the second Integers. Rather than guessing you should be getting the return value from the lower call and exploring it.
Yes, that code will work. I have tried it.
Now read the documentation for NavigableSet#lower and you find out it returns an E. Since you haven't specified types for the Set, you are returning an java.lang.Object.
Try changing NavigableSet setA to NavigableSet<String> setA (first code only) and you can see it will return a String. Then how could you cast that to a NavigableSet?
posted 4 years ago
Actually you would probably get a compile‑time error if you had used generics.