GAUTAM
asked on
How to have dynamic return type for a function in java
Hi Experts...
I have a function which could return values depending on the request and the return value needs to be dynamic as it could be any of the eight major return types.
How do i achieve the same.
Please help...
I have a function which could return values depending on the request and the return value needs to be dynamic as it could be any of the eight major return types.
How do i achieve the same.
Please help...
make the return type as Object, so you can return any type based on your runtime decision
ASKER
@ksivananth: Thanks for the reply.
I thought of the same thing but keeping the return type as Object does not satisfy my requirement as in the first place i am trying not to have a casting back at the required end.
This was the reason for the dynamic return type.
Please help...
I thought of the same thing but keeping the return type as Object does not satisfy my requirement as in the first place i am trying not to have a casting back at the required end.
This was the reason for the dynamic return type.
Please help...
share the code snippet of your function.
then parameterization is the way, can you show me the method and how it be called, will be able to suggest you the exact parameterization
ASKER
@amit_n_panchal: Basically based on the name passed into the fuction i will be returning data which can be either of the major eight return type containing in eight arraylists.
So i need to decide the return type depending on the name passed at the runtime.
Please help...
So i need to decide the return type depending on the name passed at the runtime.
Please help...
ASKER
@all:
The function syntax is
static <return type> retunValue(String name)
{
return either one of the return typed valued based on a logic;
}
Please help...
The function syntax is
static <return type> retunValue(String name)
{
return either one of the return typed valued based on a logic;
}
Please help...
you may try something like this, you don't need to pass a name
public <T> T myMethod( T t, ..... ){
//return an instance of T which is of type based on your decision at runtime
}
public <T> T myMethod( T t, ..... ){
//return an instance of T which is of type based on your decision at runtime
}
Generics is probably what you want. Here is an example of calling a method that operates on type T contained in ArrayList<T>
import java.util.*;
public class Library<T> {
public static void main(String[] args) {
Library<String> lS = new Library<String>();
System.out.println(lS.doIt(new ArrayList<String>(Arrays.asList(new String[] { "alpha", "beta", "gamma"}))));
}
public T doIt(List<T> items) {
T item = items.get(0);
return item;
}
}
ASKER
@ksivananth:Thanks for the reply.
Here i need to pass a name as this name would be the key value in one of my maps.
Based in this String name i have to search two maps either of type Byte or Integer and return either one of the map's value if the key is present.
Below i have the sample code .
How do i achieve the same.
Please help...
Here i need to pass a name as this name would be the key value in one of my maps.
Based in this String name i have to search two maps either of type Byte or Integer and return either one of the map's value if the key is present.
Below i have the sample code .
How do i achieve the same.
Please help...
static <return_type> getParameter(String name)
{
if(byteMap.containsKey(name))
{
return byteMap.get(name);
}
else if(intMap.containsKey(name))
{
return intMap.get(name);
}
}
>>Based in this String name i have to search two maps either of type Byte or Integer
There are two types involved in a Map. What are they in your case?
There are two types involved in a Map. What are they in your case?
ASKER
@CEHJ: Thanks for the reply.
If you are asking about the map declaration syntax these are are as follows:
static LinkedHashMap<String,Byte> byteMap = new LinkedHashMap<String,Byte> ();
and
static LinkedHashMap<String,Integ er> intMap = new LinkedHashMap<String,Integ er>();
Please help...
If you are asking about the map declaration syntax these are are as follows:
static LinkedHashMap<String,Byte>
and
static LinkedHashMap<String,Integ
Please help...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@CEHJ: Thanks for the reply.
Will this work out?
My class name is Request<T>.
It does not generate any errors.
Please help.
Will this work out?
My class name is Request<T>.
It does not generate any errors.
Please help.
public T getParameter(String name)
{
if(byteMap.containsKey(name))
{
Request<Byte> req = new Request<Byte>();
return (T)req.returnValue(byteMap,name);
}
else if(intMap.containsKey(name))
{
Request<Integer> req = new Request<Integer>();
return (T)req.returnValue(intMap,name);
}
return null;
}
public T returnValue(Map<String, T> items, String key)
{
T item = items.get(key);
return item;
}
Looks OK. The cast is unnecessary btw - one benefit of generics
ASKER
@CEHJ: Thanks for the reply.
Without the cast it gives compiler errors.
Is there any chances of this cast causing trouble later.
Coz the implementation is not finished and if i assume this works fine and continue with this a lot of time can get wasted.
Please help...
Without the cast it gives compiler errors.
Is there any chances of this cast causing trouble later.
Coz the implementation is not finished and if i assume this works fine and continue with this a lot of time can get wasted.
Please help...
ASKER
@CEHJ: And how can i make the getParameter() method static.
I can't guarantee there won't be problems. What's wrong with the earlier and simpler suggestions about using Object and casting?
ASKER
@CEHJ : Thanks for they reply.
I tried out a sample and it workd fine for the moment.
To avoid casting this codeing is being done.
How can i make the getParameter() method static.
I tried out a sample and it workd fine for the moment.
To avoid casting this codeing is being done.
How can i make the getParameter() method static.
>>How can i make the getParameter() method static.
That will raise the difficulty bar, possibly leading to problems, but the general pattern would be
You could probably more easily use
Map<String, Number> btw
That will raise the difficulty bar, possibly leading to problems, but the general pattern would be
You could probably more easily use
Map<String, Number> btw
public static <T> T getParameter(String name) { ... }
ASKER
@CEHJ: Thanks for the reply.
I tried it out and it works perfectly.
How can this raise the difficulty bar, possibly leading to problems.
I tried it out and it works perfectly.
How can this raise the difficulty bar, possibly leading to problems.
Well i'm not saying there's anything sinister likely to happen ;) If you're working now, it'll probably be OK
ASKER
@CEHJ: Thanks a lot for your help.
:)