• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How get haspmap in inserted order?

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi every one,

i am proud to say to be part of code ranch because when i pass question i am getting positive responses.




if i want to retrieve the values from hashmap ,it may not follow order as i inserted.for that i need to go for

so,in my interview he asked me that as any predefined method is there to retrieve the data from hashmap as we inserted?
please give me solution.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing in a hashMap you can use to get it in the inserted order, however you can use a Tree Map. Tree Map retrieves values in the inserted order
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajesh Nagaraju wrote:Tree Map retrieves values in the inserted order


No, it doesn't. TreeMap sorts the elements by the "natural" order of the keys.

LinkedHashMap will return the elements in the order they were inserted into the map when you iterate over the keySet(), entrySet() or values() of the map.


This will print the elements in the order they were put into the map:

id = 3
name = shyam
age = 26

 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies, I got it all mixed up

Thanks Jesper
 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankyou jasper,


can you explain it.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The enhanced for statement
The for Statement Tutorial
 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my condition is you should not use LinkedHashMap ok.

i want method to retrieve in order as i inserted.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shyam sunder prasad wrote:my condition is you should not use LinkedHashMap ok.

i want method to retrieve in order as i inserted.



Then that's exactly what a LinkedHashMap does, as has been pointed out. Why don't you want to use that?

In a plain HashMap, the information simply doesn't exist any more once they've been added. So your only options would be to extend and modify HashMap (in other words, write your own LinkedHashMap), or keep a separate list of the items you've added so you can control the order of iteration.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic