If the library specifies extends then they are explicitly disallowing put You should defensively copy before modifying, since they can quite legitimately change their return type to be immutable in a new version. If copying is expensive, then you can try creating a map type that is of type String, Object that first queries their map, and then queries some map you create that has your local modifications.
If the library specifies extends then they are explicitly disallowing put. You should defensively copy before modifying, since they can quite legitimately change their return type to be immutable in a new version. If copying is expensive, then you can try creating a map type that is of type that first queries their map, and then queries some map you create that has your local modifications.
If you do know that their return type is immutable and that you solely own it, then the @SuppressWarnings("unchecked") annotations is a legitimate way to work around the warning, but I would double check those assumptions and comment extensively. To understand extends vs super, look at it this way. Since the value can be any type that extends Object, the following is valid.
Map strToNum = new HashMap(); strToNum. Put("one", Integer. ValueOf(1)); // OK Map strToStr = new HashMap(); strToStr.
Put("one", "1"); // OK Map strToUnk = randomBoolean()? StrToNum : strToStr; strToUnk. Put("null", null); // OK.
Null is an instance of every reference type. StrToUnk. Put("two", Integer.
ValueOf(2)); // NOT OK. StrToUnk might be a string to string map strToUnk. Put("two", "2"); // NOT OK.
StrToUnk might be a string to number map So put doesn't really work with the extends boundary types. But it works perfectly well with reading operations like get: Object value = strToUnk. Get("one"); // We don't know whether value is Integer or String, but it is an object (or null).
If you want a map to primarily use with "put" instead of "get", then you can use "super" instead of extends as in: Map strToDbl = new HashMap(); Map strToNumBase = randomBoolean()? StrToNum : strToDbl; strToNumBase. Put("two", Double.
ValueOf(2.0d)); // OK. We know that any subclass of Number can be used as values. // But now, gets don't work as well.
Number n = strToNumBase. Get("one"); // NOT OK. Values can be instances of any class.
Thanks, you very clearly explained what extends does, though I am still a little unclear on why get does not work safely with super; I'll have to read up about that again. – AniDev Dec 28 '10 at 19:15 @AniDev, strToNumBase can be a map of strings to any super-type of Number. The only super-types of Number are Number itself, its base class Object, and various interfaces like Comparable, Cloneable, etc.An Integer can be put into any of those since an Integer can be passed to any of put(Number n), put(Object o), put(Comparable c), put(Cloneable c), etc. – Mike Samuel Dec 29 '10 at 6:27 But what if Map strToNumBase = new HashMap();, and we try to add a Long to that Map?
– AniDev Dec 29 '10 at 16:13 @AntiDev, that is illegal since Float is not a super-type of Number. – Mike Samuel Dec 30 '10 at 4:06 Then how is Map strToNumBase = new HashMap(); not illegal? – AniDev Dec 31 '10 at 22:02.
As far as I know, bounded widecards, i.e.? extends Number, is not used for variables or fileds. It is commonly used for arguments of method. Let's first consider a case without generic type.
Public void method(List list) { } Example usages: method(new List()); // Add(new Double()); // this is not allowed } The above list now have type of "unknown subtype of Number" which can be List, List, List, etc. Adding a Double object to the list of unknown type is certainly unsafe. To illustrate this point, a call to method is method(new ArrayList()); ... public void method(List list) { // adding Double to Integer list does not make sense.List. Add(new Double()); // compiler error } For variables and fields, you normally don't use bounded widecards, you can do private Map origValueMap; ... Map valueMapPart1 = origValueMap; valueMapPart1.
Put(nameMappings3, new Double(splitAtQuantity)); Note: there is no need to cast new Double(splitAtQuantity) to its super type, e. G Number or Object.
Thanks, this helped a lot to clear my confusion about why what I was trying to do wasn't working. – AniDev Dec 28 '10 at 19:11.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.