aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections and Generics Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections and Generics Help" Watch "Collections and Generics Help" New topic
Author

Collections and Generics Help

Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hello All,
I need help with the following two questions from K&B's book

1)public static <E extends Number> List<? super E> process(List<E> nums)

A programmer wants to use this method like this:

// INSERT DECLARATIONS HERE

output = process(input);


Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile? (Choose all that apply.)

A.ArrayList<Integer> input = null;

ArrayList<Integer> output = null;

B. ArrayList<Integer> input = null;

List<Integer> output = null;

C. ArrayList<Integer> input = null;

List<Number> output = null;

D. List<Number> input = null;

ArrayList<Integer> output = null;

E. List<Number> input = null;

List<Number> output = null;

F. List<Integer> input = null;

List<Integer> output = null;

G. None of the above.

The correct answers are B,E,F and I chose B,C,E,F. Why C is not correct?I've been studying all day and My head hurts and I cant figure it out.

2)Given that String implements java.lang.CharSequcnce, and:

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);
}
// INSERT DECLARATION HERE
{
Collection<E> longWords = new ArrayList<E>();
for (E word : coll)
if (word.length() > 6) longWords.add(word);
return longWords;
}
}

Which declarations could be inserted at // INSERT DECLARATION HERE so that the program will compile and run? (Choose all that apply.)

A. public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)

B. public static <E extends CharSequence> List<E> getLongWords(Collection<E> coll)

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

D. public static List<CharSequence> getLongWords(Collection<CharSequence> coll)

E. public static List<? extends CharSequence> getLongWords(Collection<? extends CharSequence> coll)

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

G. static public <E super CharSequence> Collection<E> getLongWords(Collection<E> coll)

G is wrong but I cant justify it. Could anyone kindly explain why G is wrong?

The answer to this one is F and I chose A,B.

The question here is that do we need to be specific about the return type i.e Collection<? extends CharSequence> is too vague. Do we have to specify the exact type to be returned incase of generics as in normal method declaration i.e int,String etc ?If this is the case, The method signature in question 1

public static <E extends Number> List<? super E> process(List<E> nums)

where the return type is List<? super E> is also vague. Then why is it correct?

I have a bad headache and I hope my questions are clear and not confusing.Waiting for a favorable reply.Thanks in advance.
Kind Regards,
Hasnain Javed Khan.
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
Hello Hasnain,

about the first question there is an error in the book. The signature of the method should be
public static <E extends Number> List<E> process(List<E> nums)
please look inth the K&B errata, there is all mistakes listed.

The answer C
ArrayList<Integer> input = null;
List<Number> output = null;

is not correct because the reference List<Number> output would be assigned to the return type List<Integer> of the method process and this isn't allowed.

about the question 2.
a) is not correct because on the left side of the assignment operator we have Collection<String> resultList and on the another side Collection<? extends CharSequence> this assignment is not allowed. The reference of type Collection<String> can only be assigned to the Collection and it's subtypes (List, Set, ect. ) with generic type <String> or without generic type at all and nothing else.

b) is not correct because in the method line
return longWords
an assigmnet List<E> = Collection<E> would happen. This assigment can be made only with explicit cast, otherwise compiler error.


SCJP 6.0 [95%] OCP EJBD 6.0 [93%]
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hello Serg Masow,
Thank you so so much for your kind reply and for the solid explanations .I knew something was wrong with the second question and thanks again for telling me about it. K&B should give me a nice smooch on those places on my head that were in contact with the wall (just kidding).Slept well and clearly understood your explanation.

I would really appreciate if you could clarify one more question. Consider The following method signature

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

The return type Collection<? extends CharSequence> means that the return type will be a collection of any type that implements CharSequence including CharSequence itself right (and in case of classes, The Class and All its subclasses) ???

List<String> list = new ArrayList<String>();
or
List<CharSequence> list = new ArrayList<CharSequence>();


Collection<String> = getLongWords(list);
or
Collection<CharSequence> = getLongWords(list);

gives the following compiler error(s)

Type mismatch: cannot convert from Collection<capture-of ? super CharSequence> to Collection<String> (in case of String)
or
Type mismatch: cannot convert from Collection<capture-of ? super CharSequence> to Collection<CharSequence> (in case of CharSequence)

Polymorphism does not apply to the <Generic Type> and the Generic signature <E> on left and right side of the = sign MUST be the same right??

if the above assignment is changed to

Collection<? extends CharSequence> resultList = getLongWords(list);

it compiles but the following assignment

Collection<? super CharSequence> resultList = getLongWords(list);

makes the compiler

Hope my question(s) make(s) sense
Waiting for a favorable reply.Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hello Serg Masow,
Thank you so so much for your kind reply and for the solid explanations .I knew something was wrong with the second question and thanks again for telling me about it. K&B should give me a nice smooch on those places on my head that were in contact with the wall (just kidding).Slept well and clearly understood your explanation.

I would really appreciate if you could clarify one more question. Consider The following method signature

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

The return type Collection<? extends CharSequence> means that the return type will be a collection of any type that implements CharSequence including CharSequence itself right (and in case of classes, The Class and All its subclasses) ???

List<String> list = new ArrayList<String>();
or
List<CharSequence> list = new ArrayList<CharSequence>();


Collection<String> = getLongWords(list);
or
Collection<CharSequence> = getLongWords(list);

gives the following compiler error(s)

Type mismatch: cannot convert from Collection<capture-of ? super CharSequence> to Collection<String> (in case of String)
or
Type mismatch: cannot convert from Collection<capture-of ? super CharSequence> to Collection<CharSequence> (in case of CharSequence)

Polymorphism does not apply to the <Generic Type> and the Generic signature <E> on left and right side of the = sign MUST be the same right??

if the above assignment is changed to

Collection<? extends CharSequence> resultList = getLongWords(list);

it compiles but the following assignment

Collection<? super CharSequence> resultList = getLongWords(list);

makes the compiler

Hope my question(s) make(s) sense
Waiting for a favorable reply.Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Sorry for the double post :roll:
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Sorry for the double post :roll:
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Sorry for the double post :roll:
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Damn
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
Hello Hasnain,

you got it
Polymorphism does not apply to the <Generic Type> and the Generic signature <E> on left and right side of the = sign MUST be the same right??

that is correct, with one exception you can assign a reference with generic type to one reference without type like
Collection = Collection<? extends CharSequence>
or a reference with broad bounds to a reference with small bounds like
Collection<? extends Object> = Collection<? extends CharSequence>
on all other cases the generic typ on the left side must exactly match to the generic type on the right side.
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hello Serg,
Thanks a million for you help and solid explanations
Kind Regards,
Hasnain Javed Khan.
Hamraj Kulshreshtha
Ranch Hand

Joined: Oct 06, 2007
Posts: 31
Hi All,

As Hasnain had asked the doubts regarding the question in K&B, i want to know that can i make the method definition like this:

public static <E extends Number> List<? super E> process(List<E> nums)

As per my understanding we can't put ? instead of type <E or T> while making our own
generic class or method.

SO the above method is not correct.
My understanding is right???

Regards,
Hamraj


SCJP 1.5 91%
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
As per my understanding we can't put ? instead of type <E or T> while making our own generic class or method.

Yes this is true, but for methods its not allowed in the declaration of the type before return type, but allowed in return type and parameter as well.
Hamraj Kulshreshtha
Ranch Hand

Joined: Oct 06, 2007
Posts: 31
Hi Yehia,

Thanks for your reply.

Regards,
Hamraj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections and Generics Help