wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Getting array back from map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting array back from map" Watch "Getting array back from map" New topic
Author

Getting array back from map

Jukka Kuusamo
Greenhorn

Joined: Jan 19, 2010
Posts: 11
Hey

I'm wondering how to get array back that I placed in map as


When I try to print map.keySet() I get


It's kinda weird that when I try to StringTokenizer it then I get only object that object gibberish


I would like to get my array back as array or in some kind of sequence (like I would get on tokenizer)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What about looping through the set returned by map.keySet ?


[My Blog]
All roads lead to JavaRanch
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your keys are arrays (which may or may not be what you really want), and the key set will be a set of arrays--that appears to be what you're getting.

Things like "[Ljava.lang.String..." aren't "gibberish"--that's a default toString() method, and is an array of Strings.
Jukka Kuusamo
Greenhorn

Joined: Jan 19, 2010
Posts: 11
Yes it's set of arrays. Now how could I read this array

I was just wondering that when I print it as toString it prints me an array, when I try to tokenizer it, which could be one way to use this non-array array, it thing just messes thing up.


Oh and yes it is gibberish if it isn't meant for human to understand
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's no "non-array array"--it's an array. You have a set of arrays. Did you see Christophe's answer? Get the array from the key set. Then do whatever you want with it.

(And it *is* designed for a human to read; see these examples--sorry I can't find the correct reference off the top of my head.)
Jukka Kuusamo
Greenhorn

Joined: Jan 19, 2010
Posts: 11
Sounds good. How can I get it from set?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

Jukka Kuusamo wrote:Sounds good. How can I get it from set?


Take a look at the java.util.Set API, which will likely lead you to the java.util.Iterator API.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

On a side note, it is a bad idea to use any highly mutable object (like Lists, Sets, Collections) as keys of a Map. The return values of the methods used to look them up again (hashCode + equals for (Linked)HashMap, compareTo / Comparator.compare for TreeMap) are very likely to change, therefore making it impossible to use methods like get, containsKey and other similar methods. It even allows you to use the very same object as two different keys. For instance:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jukka Kuusamo
Greenhorn

Joined: Jan 19, 2010
Posts: 11
Well now I got it. I didn't really know how to walk though ArrayList.
 
Consider Paul's rocket mass heater.
 
subject: Getting array back from map