Best collections questions in March 2011

Should mutable collections override equals and hashCode?

19 votes

I was just wondering if it was a good idea to override equals and hashCode for mutable collections. This would imply that if I insert such a collection into a HashSet and then modify the collection, the HashSet would no longer be able to find the collection. Does this imply that only immutable collections should override equals and hashCode, or is this a nuisance Java programmers simply live with?

You should override equals and hashCode if your class should act like it were a value type. This usually is not the case for collections.

(I don't really have much Java experience. This answer is based on C#.)

Is there a difference between StringDictionary class and Dictionary<String,String>

4 votes

System.Collections.Specialized contains StringDictionary http://msdn.microsoft.com/en-us/library/system.collections.specialized.stringdictionary.aspx#Y1626

What's difference with Strong Typed Dictionary in Generics?

StringDictionary comes from .Net 1, which predates generics.

Therefore, unlike Dictionary<String, String>, it doesn't implement any generic interfaces, so it cannot be used with LINQ (unless you Cast())

Also, StringDictionary normalizes all keys to lowercase.
(To make a case-insensitive Dictionary<String, String>, pass StringComparer.OrdinalIgnoreCase; this is also more compatible with Turkey)