• Post Reply Bookmark Topic Watch Topic
  • New Topic

hashMap keys to String Array  RSS feed

 
Nicholas Schembri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have the following hashMap declared:


How can i get an array of String filled with hashMap keys?

Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Schembri wrote:
How can i get an array of String filled with hashMap keys?


What have you tried so far?

Henry
 
Nicholas Schembri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Nicholas Schembri wrote:
How can i get an array of String filled with hashMap keys?


What have you tried so far?

Henry


Hi Henry I have tried the following method:

private HashMap<String, Car> cars = new HashMap<String, Car>();

.......

public String[] getCarIds(){
cars.keySet().toArray();

}
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

If you want an array of type String you need to use the other toArray() Method (ie the one that takes an array of the type you want to be returned)
 
Arun Peter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Nicholas Schembri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your help.

I managed to solve my problem with your help

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!