• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Generics in return type

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

This my first post on Java ranch forum

public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collection<E> coll)
{
Collection<E> longWords = new ArrayList<E>();
for (E word : coll)
if (word.length() > 6) longWords.add(word);
return longWords;
} }


When i invoked this method using Collection<String> argument for coll this piece of code gives compile error because return type mentioned as []bCollection<? extends CharSequence> [/b] does not match return value which is Collection<String>....

So my question is why compiler thinks String does not fit in <? extends CharSequence> since String class implements CharSequence interface ??
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post more of your code? Like the class definition? Or the calling code? ... Quite frankly, what you posted so far compiles fine.

Henry
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in your return type. It only specifies that it is a collection of some implementation of CharSequence - but you don't know which.

If you want to pass it Collection<String> and receive the return value as Collection<String>, you should change the signature:

This will work with your current implementation as well, since longwords is exactly that: Collection<String>
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Fahimuddin Patel:

When i invoked this method using Collection<String> argument for coll this piece of code gives compile error because return type mentioned as []bCollection<? extends CharSequence> [/b] does not match return value which is Collection<String>....

So my question is why compiler thinks String does not fit in <? extends CharSequence> since String class implements CharSequence interface ??



Well, to help you understand what the compiler "thinks", it would help us very much to know the exact error message, and to see the line of code it refers to.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!