This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic and collection SCJP 5.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic and collection SCJP 5.0" Watch "Generic and collection SCJP 5.0" New topic
Author

Generic and collection SCJP 5.0

Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
import java.util.*;
public class LongWordFinder {
public static void main(String[] args){
String[] array ={"123","12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
System.out.println(resultList);
}
static public<E extends CharSequence>Collection<E> getLongWords(Collection<E> coll){
Collection<E> longWords = new ArrayList<E>();
for(E word : coll)
if (word.length() > 6) longWords.add(word);

return longWords;
}
}

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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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..


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: Oct 21, 2008
Posts: 39
Thanks all for the information.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generic and collection SCJP 5.0
 
Similar Threads
Generics problem
Generics doubt
E super CharSequence makes no sense??
scjp 1.5 question related to collections
K&B error ? Generics pg 632 Question 4