Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic and collection SCJP 5.0

 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output : [12345678, 1234567890]

Hi all, above is one of my revision question. Is anybody know about the method signature below:??

static public<E extends CharSequence>Collection<E> getLongWords(Collection<E> coll){
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tay edmen:
Hi all, above is one of my revision question.


Howdy Tay. There is a rule at javaranch that you have to tell the source of the question i.e. from where you copied the question. So please tell from where you copied this code. Then only we will be able to solve your problem. I hope you will understand this..
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. As Ankit already said, we have a rule here: when you copy a question from a book, mock exam or other source, we require that you quote your sources. So, please tell us where you copied the question from.
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i got it from "SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) by Kathy SierraandBert Bates" Chapter 7: Generics and Collections, Self Test : Question 14.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tay edmen:
does anybody know about the method signature below:??

static public <E extends CharSequence> Collection<E> getLongWords(Collection<E> coll){


Well tay <E extends CharSequence> defines that this method uses a type that is a sub-class of CharSequence (String is also a sub-class of CharSequence).

the return type of this method is method is Collection<E> this means that it will return a collection of type which is a sub-type of CharSequence.

the parameter passed to this method is Collection<E> this means that it will return a collection of type which is a sub-type of CharSequence.

Now both the returned and passed types are of type E. So the type of the collection passed as parameter and the type of the collection returned will be the same and will be a sub-type of CharSequence.

So when you pass a Collection<String> to this method, it will return a Collection<String> and if you pass a Collection<StringBuffer> a Collection<StringBuffer> will be returned....
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic