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

H

hyena

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!
 
A

Andrew McDonagh

hyena said:
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!

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
 
H

hyena

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.
 
A

Andrew McDonagh

hyena said:
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.



"Andrew McDonagh" <[email protected]> wrote in message


no prob =- glad to help
 
I

isamura

: 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.
:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top