This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
OK, this is one of those things that sounds like it should be simple, but I'm just being blind. Feel free to start any (helpful) answer with "as should be obvious to any intelligent lifeform..." I have a HashMap of Address objects, with Strings as the keys. I want to extract an array of Address objects from it. So I tried
but that throws an ArrayStoreException. I think that this is because the Set that is returned by the entrySet() method contains Map.Entry objects, not Address objects but that hasn't helped me work out how to get the contents as an Address array. Any help gratefully accepted Dave
Joined: Mar 14, 2003
Oh dear, now I'm embarrassed. Try values() intead of entrySet() :roll: Should have been obvious to any intelligent lifeform with a reference book. Dave
I think the problem is that HashMap.values() returns a Collection, which is an interface. In this code, I'm using an ArrayList. I don't have a java compiler on hand to test this, but try this: Address myAddressArray = (Address) ArrayList(addresses.values()).toArray() Angel