Why aren't Collections.checkedMap and friends used more often?

I recently stumbled upon Javadoc for the Collection.checkedMap family of functions to create dynamically typed representations of standard collection types. Given that they add another layer of security on top of collections that diagnose a relatively common programmer error, I would suggest that they would be more popular. For some reason, however, in all the major Java projects that I worked on, I did not see them once.

My question is this: is there a specific reason Java programmers don't use these proven wrappers more often? Or is it just a lack of benefit / lack of knowledge about their existence?

EDIT . To clarify my question, generic versions of collections still contain unsafe type functions. Map containsKey , containsValue , remove and get work, for example, Object . My main question is that, given this type, it is insecurity, why more people do not use proven implementations to diagnose runtime type violations.

+7
source share
2 answers

These classes are really necessary if you are abusing (or not using) generics. It duplicates when checking the runtime, which should be sufficient to run at compile time with security guarantees like the generic mechanism.

As a result, the use of these methods is really interesting in the context of API designers who do not trust library users and want to protect themselves or users from users who use the card improperly.

In this case: if your code compiles without warnings about the raw type or any unverified warnings about the conversion / type, you are guaranteed that the checked* methods do not do you any good; they only crash at runtime.

Edit

You seem to conclude that since remove , get , etc. work on Object , they are somehow unsafe. They are not. None of these methods will save their operand on the card, so you certainly will not compromise security like the card itself. These methods accept an object for one main reason: for backward compatibility. There have never been any stringent requirements that would be required to adhere to the same class. For example, if an instance of class A and an instance of class B can be somehow equal to each other, put a on the card, and then calling remove(b) should remove the mapping a .

This behavior needed to be maintained after adding generics to maintain backward compatibility. Thus, they were not able to upgrade the interface to something like remove(K) , because existing code can rely on the ability to remove a mapping based on an instance of a completely different class. However, it is not safe, and using a verified version of the card does not alter this behavior in the least.

+13
source

RETRACT: The following opinion is incorrect. There are several good reasons, besides backward compatibility, why it is better that the search index be of any type.

There is no divine reason why these methods should accept Object instead of E In real-world programs, if a non-E object is transmitted, this is almost certainly an application error. In most cases, the version E API will be better than the Object version. And I say that the Sun made a mistake here. There is no backward compatibility because the common API is the new API.

Fortunately, my IDE (IntelliJ) would warn me if I pass these non-E methods. Static validation has been pushed beyond what the language specification says and what the compiler does.

0
source

All Articles