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?
 
CEHJConnect With a Mentor Commented:
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
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Amitkumar PSr. ConsultantCommented:
share the code snippet of your function.
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.