JavaRanch » Java Forums »
Java »
Java in General
| Author |
determine the input type of the value of the function parameter map
|
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
Given the following programs, how to determine the input type of the value of the java.util.Map?
If that is not possible, will changing the class definition to something else such as the following achieve my goal?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
You can use instanceof to check what runtime type an object has.
Note that all objects have a toString() method, because toString() is in class Object.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Why not add generics to the combo box?
If you need to compare on V without wanting to use a java.util.Comparator you can add bounds to it:
Note that the ? super V part is there to allow classes like java.sql.Timestamp - classes that extends another Comparable class.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
Rob Spoor wrote:Why not add generics to the combo box?
If you need to compare on V without wanting to use a java.util.Comparator you can add bounds to it:
Note that the ? super V part is there to allow classes like java.sql.Timestamp - classes that extends another Comparable class.
Thank you for your advice!
Please help to solve the following compile error of my test program:
Bound mismatch: The generic method sortByValue(Map<K,V>) of type MapUtil is not applicable for the arguments (Map<String,MyString>). The inferred type MyString is not a valid substitute for the bounded parameter <V extends Comparable<? super V>> MapTest/implementation/src MyComboBox.java line 34
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Make MyString implement Comparable<MyString>.
|
 |
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
Rob Spoor wrote:Make MyString implement Comparable<MyString>.
The MyString class is actually a third party class which I don't have the source code so I cannot change it to implement Comparable.
Is there another solution?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
You'll have to drop the extends Comparable<? super V> part and use a Comparator<V>. However, this means that you either have to provide a Comparator<String> if you want a MyComboBox<String>, or you need to sacrifice a bit of type safety - in a similar way to how TreeMap does it. TreeMap allows you to use a non-comparable class as the key type without needing to provide a Comparator, which will cause a ClassCastException during runtime.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
If it isn’t a final class you can extend it and make it implement Comparable.
I think combo boxes with generics only work in Java7.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4753
|
|
Campbell Ritchie wrote:If it isn’t a final class you can extend it and make it implement Comparable...
And if it is, you might still be able to wrap it in a MyStringWrapper class of your own, and have it implement Comparable.
It all depends on what the class exposes for you, but you might even be able to have your wrapper class implement CharSequence, which would then make it pretty much interchangeable with a String as far as any receiving object is concerned.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
Rob Spoor wrote:You'll have to drop the extends Comparable<? super V> part and use a Comparator<V>. However, this means that you either have to provide a Comparator<String> if you want a MyComboBox<String>, or you need to sacrifice a bit of type safety - in a similar way to how TreeMap does it. TreeMap allows you to use a non-comparable class as the key type without needing to provide a Comparator, which will cause a ClassCastException during runtime.
Thank you for your advice!
It compiles and runs ok as follows.
However, the commented out class StrComparator has compile error if it is uncommented.
Please help to fix the compile error.
Feel free to suggest any improvement.
Thanks in advance.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
Winston Gutkowski wrote: . . . wrap it in a MyStringWrapper class . . . have your wrapper class implement CharSequence, . . .
What good ideas
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
albert kao wrote:However, the commented out class StrComparator has compile error if it is uncommented.
Please help to fix the compile error.
V doesn't have a compareTo method, because V is not bound. Change it into this:
Note that I also made it implement Comparator<V> which removes the need of casting to V.
|
 |
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
Rob Spoor wrote:
albert kao wrote:However, the commented out class StrComparator has compile error if it is uncommented.
Please help to fix the compile error.
V doesn't have a compareTo method, because V is not bound. Change it into this:
Note that I also made it implement Comparator<V> which removes the need of casting to V.
Currently one Comparator is for String and another Comparator is for MyString.
Is it possible to have a Comparator which allow both String & MyString?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Sure, but because the two classes have no common super type you'd have to use a Comparator<Object>:
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4753
|
|
If it doesn't implement Comprarable, it seems odd that it would implement CharSequence; the authors don't appear to have had ease-of-use high on their list when they wrote it. Or maybe they expect everybody to use toString() if they need one.
OP could help us out by providing its public API...or at least the relevant bits.
Winston
|
 |
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
I simplify the MyComboBox class as follows.
However, please help to fix the compile error.
Bound mismatch: The generic method sortByValue(Map<K,V>) of type MapUtil is not applicable for the arguments (Map<String,V>). The inferred type V is not a valid substitute for the bounded parameter <V extends Comparable<? super V>>
|
 |
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
Winston Gutkowski wrote:
If it doesn't implement Comprarable, it seems odd that it would implement CharSequence; the authors don't appear to have had ease-of-use high on their list when they wrote it. Or maybe they expect everybody to use toString() if they need one.
OP could help us out by providing its public API...or at least the relevant bits.
Winston
MyString is a third party class which I don't have the source code.
From its original API, I infer that it does not implement CharSequence.
It implements:
In the posted code yesterday for the MyString class I implement instead
int compareTo(MyString anotherString);
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
albert kao wrote:
You've instantiated a comparator, now use it.
I suggest you go through this before continuing.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4753
|
|
For the class definition: yes.
If you're using it in a parameterized method where T has already been determined, Comparator<? super T> is more normal.
But we're getting off-topic again. The rozzers'll be on our backs .
Winston
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4753
|
|
albert kao wrote:It implements:
...
In which case, it looks like the answer has been staring at you all along: use its getText() method (whichever seems appropriate).
Or does it do some nefarious reformatting that prevents you?
Winston
|
 |
 |
|
|
subject: determine the input type of the value of the function parameter map
|
|
|
|