File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HashMap: keySet() and values order Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "HashMap: keySet() and values order" Watch "HashMap: keySet() and values order" New topic
Author

HashMap: keySet() and values order

Dmitriy Davydenko
Greenhorn

Joined: Feb 28, 2008
Posts: 2
HashMap doesn't guarantee the order of the elements.

But if i call
keySet().toArray( new String[0] )
and then (without changing the map)
values().toArray( new String[0] )

is the order of the elements the same in the arrays? Or no guarantees?
Monika Joshi
Greenhorn

Joined: Apr 19, 2010
Posts: 9
Hi Golem,

The order of the elements would be same in the array.

Just to clarify, if we have a hashmap with values (1, "one"), (2, "two"), (3, "three")
keySet().toArray( new String[0] ) - would generate the keys of the elements in hashmap. - ["1","2","3"]

values().toArray( new String[0] ) - would generate the values of the elements in hashmap.["one", "two", "three"]

Regards,
Minakshi

Gopi Chella
Ranch Hand

Joined: Apr 26, 2010
Posts: 52
I dont think the order of the elements will be same in array. Please check the below example i made



The ouput is :

I hope if you need the elements to be Ordered then go for Linked Hash Map and if you want the element to be in sorted manner then go for TreeMap

SCJP 1.5
Devaka Cooray
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2691
    
    3

"Golem XIV", please check your private messages regarding an important administrative matter.


Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

If you do not change the contents of the map then the order will (most likely) be the same. The iteration order isn't random, it's just unspecified.

Minakshi Sood wrote:Just to clarify, if we have a hashmap with values (1, "one"), (2, "two"), (3, "three")
keySet().toArray( new String[0] ) - would generate the keys of the elements in hashmap. - ["1","2","3"]

Not necessarily. As the API says the order is unspecified. It might be 1, 2, 3 but it might also be 2, 1, 3 or anything else.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Minakshi Sood wrote:The order of the elements would be same in the array.

Maybe, but don't count on it! There's no guarantee that the order of elements in the keySet() and values() always lines up. Don't write code that relies on that, even if it works on your system - it might not work with a different version of Java or on another operating system.

If you need two lists with the keys and the elements in such a way that they line up, do something like this (assuming keys and values are both strings):


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: HashMap: keySet() and values order
 
Similar Threads
First Key of HashMap
Maintaining order in the map
LinkedHashMap and keySet
converting Array Of Objects to Array Of Strings
How get haspmap in inserted order?