0

I am trying to create a function call using hashmap.
For example,

Hashmap-- "a" -> "b" "c" -> "d"

Now I should parse this Hashmap and create a function like this-

someFun("{a:#,c:#}",new SomeClass(b),new SomeClass(d));

As you can see, a and c are keys of hashmap(first argument) ;
b and d are used to create objects (second argument and so on..)
someFun parameters depend on HashMap size..

I am confused!
Because I can loop through the map to get Keys and easily create the first argument.
For the second argument, I can use the value to create Objects.
But now how do I add these together to make the function call as specified?
Any help would be very much appreciated :)

P.S: My question is not about getting values/keys from hashmap,but using them to create a function call something as specified.I am not allowed to change someFun consider it as API call.

Yash
  • 177
  • 2
  • 14
  • Your question is not clear.!! You want to call a function instead of retrieving the value from the `Hashmap` for a particular key. Is that it..?? – Dileep Mar 12 '14 at 04:53
  • To make the function call, you will have to use the java reflection api. – Hirak Mar 12 '14 at 04:55
  • http://stackoverflow.com/questions/10462819/get-keys-from-hashmap-in-java You dodnt need to parse a hasmap. Just use the keyset() function to get all keys and make a string as you wanted. – ray Mar 12 '14 at 05:03
  • Not instead of, I need use hash maps keys and values to create a function call @Dileep – Yash Mar 12 '14 at 05:13
  • @Hirak Thanks, I am aware of java reflection API,but really not sure how would I use that to implement this – Yash Mar 12 '14 at 05:13
  • @ray Yes,true ..Thanks, my question is about using hashmap to create function call. – Yash Mar 12 '14 at 05:14
  • someFun() function takes how many arguments? is that fixed or varying as per the hashmap size? – ray Mar 12 '14 at 05:30
  • @ray varies as per HashMap – Yash Mar 12 '14 at 05:32
  • well you can consider changing the someFun() function to take an array of SomeClass as the second argument. – ray Mar 12 '14 at 05:34
  • Well,`someFun` is part of some library I cannot change that! – Yash Mar 12 '14 at 05:36
  • If the function you are calling, has argument like SomeClass ... somes, you can just construct an array of SomeClass values, based on value set in your Map, and use it as somes call – mavarazy Mar 12 '14 at 05:42

4 Answers4

2

Is it something like this, that you need:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;

public class Draft {

public static class SomeClass{

    final private String val;

    public SomeClass(String val) {
        this.val = val;
    }
}

public void someFun(String str, SomeClass ... classes) {
    System.out.println("someFun" + str + Arrays.toString(classes));
}

public static void main(String[] args) {
    HashMap<String, String> keyToParam = new HashMap<>();
    keyToParam.put("a", "b");
    keyToParam.put("c", "d");
    String strArg = null;
    SomeClass[] classes = new SomeClass[keyToParam.size()];
    int pointer = 0;
    for(Entry<String, String> entry: keyToParam.entrySet()) {
        strArg += entry.getKey() + ":#";
        classes[pointer++] = new SomeClass(entry.getValue());
    }
    new Draft().someFun(strArg, classes);
}

}
mavarazy
  • 7,562
  • 1
  • 34
  • 60
1

First, you should define function like this:

public void someFun(String str, SomeClass[] someClasses) {
    // do something
}

Or this:

public void someFun(String str, SomeClass... someClass) {
    // do something
}

The former would be better because it's easy to call it by Java Reflection.

Then go through the hashmap and concentrate all keys to a string someString as the first parameter. While doing the iteration, you put all the values into an array someClasses as the second parameter.

Finally get the method and invoke it by (assume that we use SomeObject someObject to call the function):

Method method = SomeObject.getClass().getMethod("someFun");
method.invoke(someObject, new Object[] {someString, someClasses});
Jiang
  • 590
  • 2
  • 10
0

To get the HapsMap Key values

    HashMap<Object, Object> hashMap= new HashMap<Object,Object>();
    ...
    Set<Object> keyValueSet=hashMap.keySet();
    for ( Object keyValue : keyValueSet) {
      //you get the key from keyValue and the corresponding value from hashMap using this 
      hashMap.get(keyValue);
    }

The rest is your logic, you can use the values in any place you want.

Dileep
  • 5,362
  • 3
  • 22
  • 38
  • Thanks,I know how to get values/keys.I need to make the function call with dynamic arguments – Yash Mar 12 '14 at 05:38
  • you said you need HashMap key and values to create function call. – Dileep Mar 12 '14 at 05:39
  • Please be specific on your requirement.?? – Dileep Mar 12 '14 at 05:40
  • Please read question again,I know how to get those,but I need a make that function call using them as mentioned, as in your example, we get keys and values and then? keys can be used to create first argument - string,how about the rest? – Yash Mar 12 '14 at 05:41
  • Pass the hashMap as argument, problem solved right..? – Dileep Mar 12 '14 at 05:45
  • No,please read the question.I can't change the function definition and I need to send new objects from hash values,not whole hash map.. – Yash Mar 12 '14 at 05:46
  • The only way of doing that is by passing the HashMap, reason is that. if the hashMap contains n number of values then you must create a function with n number of arguments.. How could that be possible..?? If your hashMap only contains only 2 element, they you can do this and i don't find a use of HashMap there, you can use some arrays. – Dileep Mar 12 '14 at 05:50
  • Thanks! arrays could be used but task remains same! Thanks for the help,I just missed generic arguments take arrays..too bad :/ – Yash Mar 12 '14 at 06:01
0

Try this way...

try {
Object obj = new Object();// Create a new instance of the class that contain your method.
Method m = obj.getClass().getMethod("methodName", param1.class, param2.class, ..);
// In your case Method m = obj.getClass().getMethod("someFun", String.class,SomeClass.class,SomeClass.class);
m.invoke(obj, "{a:#,c:#}", new SomeClass(b),new SomeClass(d));
}catch (SecurityException e) {
  // ...
} catch (NoSuchMethodException e) {
  // ...
}catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}

Method implementation :

public static void someFun(String name, SomeClass classes ...) {

    for (SomeClass cls : classes) {
        //Logic here
    }
}
Hirak
  • 3,601
  • 1
  • 22
  • 33
  • This is a notepad typed code. Please expect few compile errors and modifications... – Hirak Mar 12 '14 at 05:42
  • Thanks, but the number of arguments for the method are not fixed and I need to decide them based on hashmap size – Yash Mar 12 '14 at 05:45
  • That shouldnot be a problem. Have your someFun method to accept varargs. And both getMethod and invoke of the reflection apis accept variable arguments. – Hirak Mar 12 '14 at 06:22
  • Thanks! Actually I was confused on how to send these multiple arguments,Now I understand I could just use an array to do that! Thanks for you help! :) – Yash Mar 12 '14 at 06:23
  • You can use variable arguments. Java will handle that as an array. I have edited my answer to show you the example. – Hirak Mar 12 '14 at 06:31