• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make MyString implement Comparable<MyString>.
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic