Vervatovskis
Vervatovskis

Reputation: 2367

HashMap without getter?

Let suppose that i have a hashmap like this

 Map map = new HashMap();
map.put(key, p.getText());

and then to get a value i should do this:

map.get("key_value");

is there a way to get the value like this:

map.key_value;

to speed up my application?

Upvotes: 0

Views: 100

Answers (3)

Brian Rogers
Brian Rogers

Reputation: 129777

If all of your keys are known beforehand, you could extend HashMap and add custom getter methods for each key, such that it would work like you want. But this is not going to speed up the execution of your program. All this buys you is maybe a little convenience.

For example:

public class MyCustomHashMap extends HashMap
{
    public Object key_value()
    {
        return this.get("key_value");
    }
}

Upvotes: 2

jcxavier
jcxavier

Reputation: 2232

That's not really the way a Map works. Referencing using a member operator (.) in Java means you are accessing a public member of the variable, and the keys aren't stored as public members of a map.

Upvotes: 2

Nanne
Nanne

Reputation: 64419

You might be able to whip something up with the values Collection? You can get a more primitive representation of you objects, but you're still working with the HashMap, etc.

But I doubt it will help you speed up your application, as it sounds like a micro-improvement that won't help too much.

Anyway, if the HashMap is really your bottleneck, maybe you want to use something else?

Upvotes: 1

Related Questions