0

Let's say I have a function which deals which returns and accepts generic type. The function may look like this

public T call(T t) { Log.v(FILE_NAME, "t: " + t.toString()); return null; } 

Let's say if the code detected that t.toString() looks like

{label1=5148.000, label2=8363.000, label3=715.000} 

Is there any way I can convert t to an object which we can easily manipulate it's content? By manipulating, I mean we can easily, for example change label1, label2 and label3 value to something else, and return the modified t.

Just in case, in the example above, the parameter t that passed was of type Map<String,String>.

Update:

  1. Yes, I'm implementing Callable<T>.
  2. In the future, I might supply other type of arguments, and not just Map<String,String>.
8
  • No you can't, because Object doesn't has such fields. Either you know the type of objects you like to pass as t to provide a bound for T or you can't change its field. (You could also do some nasty stuff with reflection, but ... don't think about it) Commented Sep 27, 2015 at 20:28
  • @Tom, what if I know the type of object that I pass as t? Is there any way I can convert t to the object? In this case, the object is Map<String,String> Commented Sep 27, 2015 at 20:34
  • @imin if it's always a Map, why are you using generics? If it's not always a Map then your argument falls down. Commented Sep 27, 2015 at 20:36
  • If you know the type and you want to "convert" it in that method, then why do you use generics, instead of public void call(Map<String,String> t) {? Commented Sep 27, 2015 at 20:36
  • @Tom: judging from the message signature, my guess is that OP is implementing Callable<T>. Commented Sep 27, 2015 at 20:37

2 Answers 2

1

I'm note entirely sure what you're asking for. If you know that t will always be of type Map<String, String>, you can simply cast it:

Map<String, String> myMap = (Map<String, String>) t; 
Sign up to request clarification or add additional context in comments.

6 Comments

This seems like a XY problem to me. An unsafe cast it rarely the solution to anything - especially a the beginner level.
@imin Easy to risk a ClassCastException, yes.
anyway in the future since I might pass different type, I might need to implement a function which tries to determine the data type, based on the pattern of t.toString()
You should NOT determine the type of an object based on what it returns from toString. See hotzst's answer for determining an object's type during runtime.
@imin that's a terrible idea on top of a terrible idea. You need to describe the problem you are trying to solve, not some subset of the solution.
|
1

If you need to have the method really generic and have some special handling for some well known parameters you can have something like this:

if (t instanceof Map) { // do something here } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.