Java KeyValue pair returning method
There might be cases/scenarios in your programming life where in you are need a method that just returns a key-value pair rather than a single value. I recently into this situation in my class where what I actually wanted to return was a key-value pair from a method.
Now any programmer would say that this is pretty simple; it indeed is! The problem is how to make it elegant by default. Well we will go over what I mean shortly but lets view some of the quick and dirty solutions to this problem:
1. Array based:
return a single dimensional array of type Object of length 2. Simple right?
example:
Object obj=new Object[2];
obj[0]=key;
obj[1]=value;
Now return obj
2. Simple-Class based:
Simple class based solution to this problem is:
class Container{
Object key;
Object value;
//constructor
//getters
//setter
}
Now if you are a serious programmer, you will realize that there is a problem with this approach!
What? Well:
1. you have to manually perform type casting right? which is fine; but you will have to check types in your code there there will be casting exception thrown during runtime.
2. second issue is, with the array based approach you will have to check for array length right?
3. code is not self-explanatory!!! (you don't know what is the return type of this function)
If you are lazy you probably won't any of the above.
My point here is that you have to manually do error checking during runtime.
For speed purposes array is the best approach if and only if your key-value pair are both primitives as then you can specify a primitive array return type. But if it's anything else than a primitive i.e. an object then you will end up having more or less the same over head.
My approach:
I thought about the problem for a while and realized that if I used generics it will make sure that any issue will get addressed during compile time not runtime. This is what I mean:
Container class:
public class Temp<T,S> {
T key;
S value;
public Temp() {
}
private Temp(Object k,Object v){
key=(T) k;
value=(S) v;
}
public Temp<? extends T,? extends S> returnPair(T key,S value){
return new Temp<T, S>(key, value) ;
}
}
My approach is what most classes like HashMap/Hash etc. in Java use except for it doesn't have the overhead of the Hash class methods.
Now any programmer would say that this is pretty simple; it indeed is! The problem is how to make it elegant by default. Well we will go over what I mean shortly but lets view some of the quick and dirty solutions to this problem:
1. Array based:
return a single dimensional array of type Object of length 2. Simple right?
example:
Object obj=new Object[2];
obj[0]=key;
obj[1]=value;
Now return obj
2. Simple-Class based:
Simple class based solution to this problem is:
class Container{
Object key;
Object value;
//constructor
//getters
//setter
}
Now if you are a serious programmer, you will realize that there is a problem with this approach!
What? Well:
1. you have to manually perform type casting right? which is fine; but you will have to check types in your code there there will be casting exception thrown during runtime.
2. second issue is, with the array based approach you will have to check for array length right?
3. code is not self-explanatory!!! (you don't know what is the return type of this function)
If you are lazy you probably won't any of the above.
My point here is that you have to manually do error checking during runtime.
For speed purposes array is the best approach if and only if your key-value pair are both primitives as then you can specify a primitive array return type. But if it's anything else than a primitive i.e. an object then you will end up having more or less the same over head.
My approach:
I thought about the problem for a while and realized that if I used generics it will make sure that any issue will get addressed during compile time not runtime. This is what I mean:
Container class:
public class Temp<T,S> {
T key;
S value;
public Temp() {
}
private Temp(Object k,Object v){
key=(T) k;
value=(S) v;
}
public Temp<? extends T,? extends S> returnPair(T key,S value){
return new Temp<T, S>(key, value) ;
}
}
Main class:
public class Test {
public static void main(String[] args) {
System.out.println(retTest().key+"-"+retTest().value);
}
static Temp<Integer,String> type=new Temp<Integer, String>();
public static Temp<Integer,String> retTest(){
return (Temp<Integer, String>) type.returnPair(2,"asdasd");
}
}
What I just did was created a container based on generics, what anyone who wants to return key-value pair using this Temp class has to do is to specify the type of Key and Value what will happen now is that if you tried to pass an argument to the returnPair method/function other than the one specified by you in generics, you will not be able to compile the program unless:
1. you used Object type instead of peculiar type
2. you did a manual cast
The second act will require you to manually do it. Why I like my approach is because:
1. It prevents you as a programmer to unknowingly cast some type to other
2. The biggest reason for this approach is the fact that it makes the reader understand that the method will return a key-vlaue pair whose type has been specified by the programmer upfront.
Since this class uses generics you could just use it wherever you want to without a problem.
My approach is what most classes like HashMap/Hash etc. in Java use except for it doesn't have the overhead of the Hash class methods.
Comments
Post a Comment