Link to home
Start Free TrialLog in
Avatar of GAUTAM
GAUTAMFlag for United States of America

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...
Avatar of ksivananth
ksivananth
Flag of United States of America image

make the return type as Object, so you can return any type based on your runtime decision
Avatar of GAUTAM

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...
Avatar of Am P
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
Avatar of GAUTAM

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...
Avatar of GAUTAM

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...
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
}
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;
    }
}

Open in new window

Avatar of GAUTAM

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

static <return_type> getParameter(String name)
	{
		if(byteMap.containsKey(name))
		{
			return byteMap.get(name);
		}
		else if(intMap.containsKey(name))
		{
			return intMap.get(name);
		}
		
	}

Open in new window

>>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?
Avatar of GAUTAM

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,Integer> intMap = new LinkedHashMap<String,Integer>();
Please help...
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GAUTAM

ASKER

@CEHJ: Thanks for the reply.

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;
	 }

Open in new window

Looks OK. The cast is unnecessary btw - one benefit of generics
Avatar of GAUTAM

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...
Avatar of GAUTAM

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?
Avatar of GAUTAM

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.
>>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
public static <T> T getParameter(String name) { ... }

Open in new window

Avatar of GAUTAM

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.
Well i'm not saying there's anything sinister likely to happen ;) If you're working now, it'll probably be OK
Avatar of GAUTAM

ASKER

@CEHJ: Thanks a lot for your help.
:)