It's also about communicating your intent of how the object should be used. For example, if your method expects a Map
object with a predictable iteration order:
private Map<String, String> processOrderedMap(LinkedHashMap<String, String> input) {
// ...
}
And if you absolutely need to tell callers of the above method that it too returns a Map
object with a predictable iteration order, because there is such an expectation for some reason:
private LinkedHashMap<String,String> processOrderedMap(LinkedHashMap<String,String> input) {
// ...
}
Of course, callers may still treat the return object as a Map
as such, but that's beyond the scope of your method:
private Map<String, String> output = processOrderedMap(input);
Personally, I find that this allows me to write cleaner code at times too, especially when it comes to unit testing. Creating a HashMap
for a single test read-only entry takes more than a line, when I can easily replace that with Collections.singletonMap()
.