Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

How to determine the data type of the return object of Map.get() ?

Thread view: 
hyena - 15 Dec 2005 11:03 GMT
hi ,

 I am trying to determine the data type of the return value of
HashMap.get(i). Is there some kind of operators or functions to do this?

 The keys of the hashmap is always Integer and the value object could be
either Integer or String.

 Thanks!
hyena - 15 Dec 2005 11:08 GMT
found the solution. Sorry for disturbing:)

> hi ,
>
[quoted text clipped - 5 lines]
>
>  Thanks!
Andrew McDonagh - 15 Dec 2005 11:12 GMT
> hi ,
>
[quoted text clipped - 5 lines]
>
>   Thanks!

whilst its certainly possible (google  'instanceof') its not usually a
good design choice to mix the value types within a collection.

Preferably, have two collections, one for the Integers one for the Strings.

If this is not possible, write a simple data holder class which you can
store both Integer and String inside it, and add an instance of this
dataholder into the collection.

class DataHolder {

public Integer intValue;
public String stringValue;
}

....

map.put(key, dataHolder);

Doing this will be a good start along a design path where very quickly
you find that the dataHolder class needs to have some instance methods
too - it becomes a proper class with its own responsibilities.

A contrived example...

renaming DataHolder to 'Address' and the member vars to houseNumber &
streetName, starts to show that those vars mean something.

class Address {

public Integer houseNumber;
public String streetName;
}

Then we might find that Address should be able to save/update its own
values by setting/getting them from a db...

class Address {

 public Integer houseNumber;
 public String streetName;

 public void refresh() {
   houseNumber = db.getHouseNumber();
   streetName = db.getStreetName;
 }

}

HTH

Andrew
hyena - 15 Dec 2005 13:14 GMT
Thanks, Andrew,

  Actually I was just doing sonthing in a fast and dirty way:). Just have
forgotten the instanceof operator when I ask the question.

 Anyway, thanks for your example, it is clearly better then the messy
approach I am using now.

>> hi ,
>>
[quoted text clipped - 59 lines]
>
> Andrew
Andrew McDonagh - 15 Dec 2005 13:48 GMT
> Thanks, Andrew,
>
[quoted text clipped - 5 lines]
>
> "Andrew McDonagh" <news@andrewcdonagh.f2s.com> wrote in message

no prob =- glad to help
isamura - 15 Dec 2005 16:22 GMT
: Thanks, Andrew,
:
[quoted text clipped - 3 lines]
:   Anyway, thanks for your example, it is clearly better then the messy
: approach I am using now.

Andrew's example demonstrates good OOP.

It goes to show one does not necessary do OO just because the underlying programming language is
one. The concepts need to be learned and applied consistently.

.k


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.