Hi, i have a problem that i want to replace an value of an key/value pair, but I dont have the approiate key for that, to avoid passing the key throug all methods, I just implemented
a method in the delegate class.
public K getFirstAssociatedKey(BilanzObjekt bo){
Set<K> keys = this.container.keySet();
for(K key: keys){
V value = this.container.get(key);
if (bo == value){
return key;
}
}
return null;
}
It works, but I can't use the retrieved key for a put-statement, cause the satic type comparision seems to fail. Even the key is retrieved from the original, the compilier is unable to validate that the key is in the approiate Enum, so it refuses the operation.
In my special case I can avoid this by returning the value to the calling mehod inside the delegate, but that can't be the solution for all cases. Even If i would pass the key-vaue through all methods, the universial declaraction of
? extends Enum<?>
would raise the same problem, that the compiler can't verify if the given key ist part of the Enum of the current EnumMap.
Cause i still not an expert dfor generics, I might have overseen an solution.
So is there any way to retrieve valid keys for the put-method at runtime without declaring a specilized subtype? Using rawtypes would supress compiler-errors, but that isn't the way what Generics intends. I allready have some lager Enums and some subsuets with EnumSet. But this solution isn't absolutly clean, cause you have to use the EnumSet to Iterate over the map and can't use keySet() anymore (Without special preparations for the values not contained in the EnumSet). Also I don't want to limit the code to only one type of Enum. So I my looking for another solution.
Is there hoepfully any easy and gernerally solution to that problem?
Thanks a lot in advance.
Greetings Michael