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...
gaugetaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ksivananthCommented:
make the return type as Object, so you can return any type based on your runtime decision
0
gaugetaAuthor Commented:
@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...
0
Amitkumar PSr. ConsultantCommented:
share the code snippet of your function.
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

ksivananthCommented:
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
0
gaugetaAuthor Commented:
@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...
0
gaugetaAuthor Commented:
@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...
0
ksivananthCommented:
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
}
0
CEHJCommented:
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

0
gaugetaAuthor Commented:
@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

0
CEHJCommented:
>>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?
0
gaugetaAuthor Commented:
@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...
0
CEHJCommented:
Check the following adaptation for your Map
import java.util.*;

public class Library<T> {
    public static void main(String[] args) {
	Library<Byte> lS = new Library<Byte>();
	Map<String, Byte> m = new HashMap<String, Byte>();
	m.put("A", Byte.valueOf((byte)'A'));
	System.out.println(lS.doIt(m, "A"));
    }

    public T doIt(Map<String, T> items, String key) {
	T item = items.get(key);
	return item;
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gaugetaAuthor Commented:
@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

0
CEHJCommented:
Looks OK. The cast is unnecessary btw - one benefit of generics
0
gaugetaAuthor Commented:
@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...
0
gaugetaAuthor Commented:
@CEHJ: And how can i make the getParameter() method static.
0
CEHJCommented:
I can't guarantee there won't be problems. What's wrong with the earlier and simpler suggestions about using Object and casting?
0
gaugetaAuthor Commented:
@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.
0
CEHJCommented:
>>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

0
gaugetaAuthor Commented:
@CEHJ: Thanks for the reply.
I tried it out and it works perfectly.
How can this raise the difficulty bar, possibly leading to problems.
0
CEHJCommented:
Well i'm not saying there's anything sinister likely to happen ;) If you're working now, it'll probably be OK
0
gaugetaAuthor Commented:
@CEHJ: Thanks a lot for your help.
0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.