• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to find particular objects in hashMap  RSS feed

 
Nicholas Schembri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose i have a hashMap which includes instances of class Employees and instances of class Customers.

How can i get the employees objects only?

And would it be possible to find the oldest staff by comparing the ages stored in the age fields of the staff objects

Thanks!!!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use instanceof to check if an object is of type Employee or type Customer:

However, if you are using instanceof a lot then it's a sign you are not programming in an object oriented style. Much better OO is to use dynamic dispath, for example:

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Schembri wrote:And would it be possible to find the oldest staff by comparing the ages stored in the age fields of the staff objects

Yes, iterate over the collection checking each staff member's age and keeping a record of the one who is the oldest so far as you go. At the end of the iteration you will have the oldest staff member.
 
Nicholas Schembri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your help

so i am using the instanceof to get only the staff objects... however how can get the key of the oldest staff?

Thanks again
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Schembri wrote:
so i am using the instanceof to get only the staff objects... however how can get the key of the oldest staff?


Staff objects are your class types right? So, only you know how to get the age right? ... You will need to iterate through the keys, and if it is an instance of staff, you will need to get and check the age. And while you are iterating and checking, you will need to keep track of the currently oldest staff (and key) until the iteration is complete.

Henry
 
Nicholas Schembri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Nicholas Schembri wrote:
so i am using the instanceof to get only the staff objects... however how can get the key of the oldest staff?


Staff objects are your class types right? So, only you know how to get the age right? ... You will need to iterate through the keys, and if it is an instance of staff, you will need to get and check the age. And while you are iterating and checking, you will need to keep track of the currently oldest staff (and key) until the iteration is complete.

Henry


Thanks Henry... I was thinking that this cannot be done using hashMap

The problem is that i cannot a way to do it :/..
 
Nicholas Schembri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am posting the code of what i have done till now...

I appreciate your help


..........




Now i need to get the key of the lowest of the Employee object with the lowest wage.....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!