Accesing a method in the last object in a LinkedHashMap

  • A+

I've got a LinkedHashMap that contains an object at key and one at value.

I've used the code


to return the last object of the keys. However, I am unable to access a method that the object has.

I've used the getClass() method to determine that I do indeed have the right kind of object, but the method cannot be called. I simply get the error that the method cannot be found.

Am I doing something wrong?

toArray give you generic Object type. You have to cast it to your key class before using it.

KeyClass key = (KeyClass) yourShots.keySet().toArray()[yourShots.size()-1]; // Here you can access your desired method 


As @rgettman suggest, you could use the overloaded version toArray(T[]) to avoid the cast. In this case you should provide an initialized array with size of keySet() beforehand.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: