• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

determine the input type of the value of the function parameter map

 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make MyString implement Comparable<MyString>.
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49805
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . wrap it in a MyStringWrapper class . . . have your wrapper class implement CharSequence, . . .
What good ideas
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, but because the two classes have no common super type you'd have to use a Comparator<Object>:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49805
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
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
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Campbell Ritchie wrote:If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
albert kao wrote:

You've instantiated a comparator, now use it.

I suggest you go through this before continuing.
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
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
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic