Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Storing objects in hashmap

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code used to store and retrieve values in hashmap
HashMap<Integer,String>hmap = new HashMap<Integer,String>();
hmap.put(5,"prak");
hmap.put(1," sachin");

hmap.put(4,"raja");
hmap.put(4,"raja");
Iterator itr= hmap.entrySet().iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
Give me idea for this situation. I need to store objects(long empid,String name, int salary, String Designation)in
hashmap , use empid as key for all the objects.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

You can also store the Model Class/Form Bean object to the HashMap, and as use that object's property as a key in HashMap.

Please let me know, if you have any issue.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So just create a HashMap and make the key the empid field and the value the entire object.

Hunter
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you store User defined objects in a hashmap (or any collection for that matter), you need to implement the equals() and hashMap() methods. the default implementations of these methods (from java.lang.Object) is generally not sufficient

-Fred.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Muhlenberg wrote:When you store User defined objects in a hashmap (or any collection for that matter), you need to implement the equals() and hashMap() methods. the default implementations of these methods (from java.lang.Object) is generally not sufficient



This is absolutely true of any objects used as keys in a HashMap; the "value" half of the key/value pair doesn't actually benefit from having these methods. They're important, though, if you want to store the values in a List or Set and be free to use all of the methods of those interfaces.
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic