• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Question regarding Generics class of Comparable Type.  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am implementing a binary research tree class which impelments the Map interface:


I am not sure how to procceed though, as i am getting an eroor of  "Bound mismatch".
Declaration:


I tried various alterations, but i am always getting the error:
Any help is appreciated!
 
Marshal
Posts: 64182
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you simply have a K, that can apply to any type. You could add something like a List, which is a bad idea because Lists are mutable and you want immutable objectas as the “K” for a Map. But if you put a List in the Map, it doesn't implement Comparable, so that won't work. You want to assure the compiler that you will only put Comparable objects into the tree, so change its declaration toBy the way, a Comparable object may not implement Comparable to itself. So it might Comparable to its superclass, which you have to allow for. The usual declaration for Comparable is
Foo<T extends Comparable<? super T>>
The second type parameter in a Map is usually V not T.
 
Lampros Marantos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply and the welcoming!
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!