• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

checking whether a generic class implements Comparable

 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a topic about implementing a DoublyLinkedList, where the generic type T impements Comparable<T>. Now, I made my own implementation that I found a bit neater, althoud required some more code, but I found it a pity to have that generic having to implement Comparable. So I thought about a TreeSet, where you either use the natural comparing or you specifu a Comparator. I tried this, but I could not get it to work. I tried:

But that does not work. How can I get this to work?
 
Saloon Keeper
Posts: 13280
292
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make all instances take only Comparables by declaring it as class XYZList<E extends Comparable<? super E>>. Stephan's suggestion is a lot better.
 
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Limiting the generic type will disallow creating an instance with a custom comparator for non-comparable types. I also would go for Stephan's solution.
 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Stephan, works a treat!

Here are two usages:

I shall try to look up how a TreeSet manages.
 
Stephan van Hulst
Saloon Keeper
Posts: 13280
292
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, for the first use case you can use a type witness, but personally I prefer to type my variable explicitly and let type inference work its magic on the factory method:


TreeSet<E> is really just a thin wrapper around a TreeMap<E, Object>, where the values are always the same instance and ignored by the wrapper set. For instance, when you call treeSet.add(value), the call is delegated to treeMap.put(value, DUMMY).

TreeMap provides no fail-fast behavior at all. TreeMap uses a null comparator when you use the parameterless constructor. When you call TreeMap.put() it performs an explicit null check on the comparator field, and if it is null, it casts the key to Comparable<? super K>. This will cause a ClassCastException if the key is not Comparable.

When there's already another entry in the map, it calls compareTo() on the key and the existing entry's key, which will also throw a ClassCastException if the two are not actually comparable to each other.

Don't blame the designers of the collection framework though. TreeMap has been around since Java 1.2, whereas generics were added in Java 5.0.
 
Saloon Keeper
Posts: 1313
40
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For reference, "type witness versus inference" was the topic my weird post on LVTI in a Stream to get a histogram of letter frequencies morphed into.

I was reminded that "type inference when using a type witness isn't really an inference at all!"
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic