3

I am wondering if there's an elegant solution for doing this in Java (besides the obvious one - of declaring a different/explicit function. Here is the code:

private static HashMap<String, Integer> nameStringIndexMap 
        = new HashMap<String, Integer>();
private static HashMap<Buffer, Integer> nameBufferIndexMap 
        = new HashMap<Buffer, Integer>();

// and a function
private static String newName(Object object, 
        HashMap<Object, Integer> nameIndexMap){
    ....
}

The problem is that I cannot pass nameStringIndexMap or nameBufferIndexMap parameters to the function. I don't have an idea about a more elegant solution beside doing another function which explicitly wants a HashMap<String, Integer> or HashMap<Buffer, Integer> parameter.

My question is: Can this be made in a more elegant solution/using generics or something similar?

Thank you,

Iulian

2
  • I don't understand what your goal is here. Do you want to replicate your newName method for Buffers ? Commented Mar 26, 2010 at 9:37
  • I want to use the same function for both types. This is what I'm trying to achieve. There are some answer below helping me doing that Commented Mar 26, 2010 at 9:42

2 Answers 2

4

You could make your function generic too:

private static <E extends Object> String newName(E object, 
        HashMap<E, Integer> nameIndexMap){
    ....
}

This bounds the two parameters of the function together, so for a HashMap<String, Integer> you can only pass String instances as first parameter. This may or may not be what you exactly want: if you only want to get elements from the map, Jon's solution is simpler, but if you want to add this object to the map, this one is the only choice.

Sign up to request clarification or add additional context in comments.

Comments

3

You want something like this:

private static String newName(Object object, 
        HashMap<? extends Object, Integer> nameIndexMap) {
    ....
}

or (as pointed out in the comments)

private static String newName(Object object, 
        HashMap<?, Integer> nameIndexMap) {
    ....
}

That will stop you from putting anything into the map, because you couldn't guarantee to get the key right - but you can get things out of the map and guarantee they'll be integers.

Note that this version doesn't make the method generic - which means it's simpler, but it doesn't provide the same type safety that Peter's version does, in that you can't guarantee that object is of the right type. Each approach has its pros and cons - use whatever is most appropriate based on the body of the method. (If you need to put an entry into the map, Peter's approach is definitely better.)

2 Comments

@newacct: True. I'll add that.
It is a good answer but the answer that serves my purpose is not this one. Thank you anyway for the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.