• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Generics and More Generics...

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
These are codes from K&B.
Can some body tell me how to approach and understand these questions.
They are look sooooo confusing.
And I always get messed up with such questions with so many code.
And half the time I dn't even undersand the question..let alone the answer.

So if any one has any tips on how to handle these questions..please Help!




Q1.

Given that String implements java.lang.CharSequence


Which when inserted would compile and run.

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 Collections<E extends CharSequence> getLongWords(Collection<E> coll)

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

E. public static <E 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)


Answer is F






Q2.

Given a method declared as

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


A prgrammer wants to use this method like this:

// INSER DECLARATION HERE

output = process(input);

Which pairs of the declarations should be placed to for the code to compile.?

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> input=null;

D.List<Number> input=null;
ArrayList<Integer> output=null;

E List<Number> input=null;
List<Number> input=null;

F. List<Integer> input=null;
List<Integer> input=null

G.None of the above


Answer is B,E,F.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would answer to ya, but I'm going to college right now, tomorrow if no one has answered I'll.

try to understand until it :-)
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I' ll try...
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
First of all the code you have given has lot of errors i guess it should look like this


1) You need to see generic parameter definition matches our method call or not.
Here <E extends CharSequence> matches to String as String extends from CharacterSequence.

2)and see method return type, matches to the variable which is taking the result of method call

Here Collection<String> resultList=getLongWords(list);

see resultList type matches with the return type of getLongWords(list)

3)And you need to see method parameter can cause any compile time errors in method body.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A. public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)


What is <E extends CharSequence> here?
Is it the return type?

If it is the return type then what is B]Collection<? extends CharSequence>[/B].I thought it meant the return type is of "Collection" which is of "? extends CharSequence" type.
In that case what doese <E extends CharSequence> before the Collection <............> mean.


They both look like return types to me , so how can they be placed together?

Can some one pleas help me to figure this out
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
A. public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)


What is <E extends CharSequence> here?
Is it the return type?

If it is the return type then what is B]Collection<? extends CharSequence>[/B].I thought it meant the return type is of "Collection" which is of "? extends CharSequence" type.
In that case what doese <E extends CharSequence> before the Collection <............> mean.


They both look like return types to me , so how can they be placed together?

Can some one pleas help me to figure this out



Sorry for not being able to answer the complete question. I'm at work and dont have much time now.
<E extends CharSequence> is creating a type and its being used on the parameters of the method in Collection<E>.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still didn't get it

That's ok..I ll wait
Till you get time...whenever that is.. !
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
A. public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)


What is <E extends CharSequence> here?
Is it the return type?

If it is the return type then what is B]Collection<? extends CharSequence>[/B].I thought it meant the return type is of "Collection" which is of "? extends CharSequence" type.
In that case what doese <E extends CharSequence> before the Collection <............> mean.


They both look like return types to me , so how can they be placed together?

Can some one pleas help me to figure this out



no it is not the return type of collection. It is a TYPE restriction on E. Thats how you define a Generic class.
"access specifier" <Type with its restrictions> "return type of the method" MethodName ( "Argument") {}

<E extends CharSequence> simply states that you can have E such that it extends CharSequence i.e. you can have String in place of E as it extends CharSequence but you cannot have Integer in place of E.

Hope it helps
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Milan,

Thanks for the reply..

I got a part of it but not much.

Do you know any link i can look into which talks about Acess Specifier in Generics...

Thnks for your help..
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as such there are no separate rules for generics.
if its a generic method /class / reference variable then it can have those access specifiers that a non-generic(legacy) method /class / reference variable would have if it were it in its place.

For more help refer to the links posted by me in this thread Here
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Milan.
Thanks for the link..

Looks like you have done a lot of reasearch on Generics..

Do you know any link which helps you to understand Generics better in a simple manner..

Still not clear with it..
So I ll do a little more bit of studying before shooting anymore questions..
 
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)

See, if you are looking for the generic type of the return type, look after the return type. After closely looking the method declaration, you'll see that a Collection object is returned(of the wildcard type that is a/is a subclass of CharSequence(like String).), but before that, the <E extends CharSequence> stands for the 'E type' parameter that is used in the method.

For your query about access specifier, I would recommend that you read the K&B section about them as they are explained in a lucid manner.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sumit Bisht:
public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)

See, if you are looking for the generic type of the return type, look after the return type. After closely looking the method declaration, you'll see that a Collection object is returned(of the wildcard type that is a/is a subclass of CharSequence(like String).), but before that, the <E extends CharSequence> stands for the 'E type' parameter that is used in the method.

For your query about access specifier, I would recommend that you read the K&B section about them as they are explained in a lucid manner.




Okay , that means we are sort of defining the E used in getLongWords(Collection<E> coll)
and this E is being defined as <E extends CharSequence>

Wow ! that makes a whole lot of sense.
Looks like a major break through for me in Generics!
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sumit , you have been a big help!
 
Beauty is in the eye of the tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic