• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Generics doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Given that string implements java.lang.CharSequence

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;
}
}

One of the option is

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


Can anybody explain me the above mentioned option?

Source : k&b

Thanks All
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
My doubt in the below mentioned statement is <E extends CharSequence> <? extends CharSequence> what are these two statements. What do they mention.

public static<E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nik,

The method getLongWords(...) works fine, problem come only due to assignment in the main method.

resultset reference variable can only refer to a Collection parameterized with String, nothing else.

You can make it working by subtle modification:


[ May 02, 2007: Message edited by: Chandra Bhatt ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
What exactly the below statement states can you explain me please?

public static<E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Hi chandra,
What exactly the below statement states can you explain me please?

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



1- Before return clause, you declare the parameterized type, what you have "E".
"E" has a bound that it must be CharSequence or any class that implements CharSequence interface (here for extending and implementing extends is used only).

2- Then you promise to return a Collection (object of the class that implement the Collection will do here, (remember polymorphism applies to base type)), parameterized with anything that implements CharSequence.

3- Collection<E> the parameter passed to the method, says that the method can accept any Collection parameterized with E (you know what E means here, any type that implements CharSequence).
[ May 02, 2007: Message edited by: Chandra Bhatt ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
I didnt get this statement

1- Before return clause, you declare the parameterized type, what you have "E".
"E" has a bound that it must be CharSequence or any class that implements CharSequence interface (here for extending and implementing extends is used only).
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Hi chandra,
I didnt get this statement

1- Before return clause, you declare the parameterized type, what you have "E".
"E" has a bound that it must be CharSequence or any class that implements CharSequence interface (here for extending and implementing extends is used only).





1- Return clause: Collection<? extends CharSequence>
2- Type declaration : <E extends CharSequence>
3- Method name : getLongWords
3- Argument passed to it : Collection<E> coll
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
I am not getting. I am not getting the type declaration one. Can you brief it please
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nik,

Can you use a variable without declaring it anywhere? No! The same thing applies to generic methods too. Either you use the type declared in the class level or declare it before use in the generic methods.

Instance methods can use the type declared in the class but static method must
declare the type before use.

You must declare the type before the the place where you tell what the method will return.

Some of the type declarations are:

<E> any type
<E extends Number> Number or any type that extends Number

 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
In the below statement type is: <E extends CharSequence> so my doubt the arguments for the method will be only that extends CharSequence. Am i right or wrong.

public static <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Hi Chandra,
In the below statement type is: <E extends CharSequence> so my doubt the arguments for the method will be only that extends CharSequence. Am i right or wrong.

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




RIGHT!
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks chandra for explaining with so much of patience
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question...

Would

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

be same as

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

Also just want to confirm that the following wont be correct becuase we cannot use "?" in declaration.

public static Collection<? extends CharSequence> getLongWords(<? extends CharSequence> coll)
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Megha,

Also just want to confirm that the following wont be correct becuase we cannot use "?" in declaration.

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



I think we cannot use "?" for class declaration but its okay for method declaration.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Megha,



What is returned from this method, must be referenced to Collection<? exnteds CharSequence> ref variable;
Collection<String> = getLongWords(...); //NOT OK



Collection<String> = getLongWords(...); //OK


"?" can't be used in param type declaration. (you are right!)
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

I think you are right...but I was not convinced about the actual difference between wild card and type parameter --both of their usage with extends.

I found Shin's notes online
and understood following from it...
<E extends Number> can be considered as a bounded type parameter.
<? extends Number> can be considered as a Bounded Wildcard

� If you want to bound the unknown type use subtype of another type, use
Bounded Wildcard

code:
--------------------------------------------------------------------------------

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

--------------------------------------------------------------------------------

I am free to return E( in our case it was String) or anything other than E which was passed as a argument to the method.It can be StringBuffer too when I return as long as it implements Charsequence. I think I cannot add anything to this returned collection becuase to add anything I need <? super ..> syntax.
Now ....
code:
--------------------------------------------------------------------------------

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

--------------------------------------------------------------------------------


In this case I have to return String, I cant return StringBuffer, bcoz my E went in as String and what I return is E so it should also be E, as opposed to the Bounded wildcard which would have allowed some flexiblity. In this case also I can't add anything to the returned collection as word "super" is not used here too.

Let me know what you say?


So What you said is right...but just the reasoning behind it is as above I guess

[ May 02, 2007: Message edited by: megha joshi ]
[ May 02, 2007: Message edited by: megha joshi ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all your explanations, it's half an hour I'm forcing these hard concepts to my brain, it 'll take more time.
After all, just one purile question come to my mind: are generics questions at the exam as tough as this one?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Megha,

Thanks for going deep in detail, but at one point my brain disagrees with the fact.


Originally posted by Megha,
public static <E extends CharSequence> Collection<E> getLongWords(Collection<E> coll)
--------------------------------------------------------------------------------
In this case I have to return String, I cant return StringBuffer, bcoz my E went in as String and what I return is E so it should also be E, as opposed to the Bounded wildcard which would have allowed some flexiblity. In this case also I can't add anything to the returned collection as word "super" is not used here too.

Let me know what you say?





I think when you say, you return bounded wildcard, you can't keep the reference of the object in Collection<String> or Collection<StringBuffer> or so, you must have to keep the reference in bounded wildcard parameterized Collection. And the most vital part You can't make modification to the Collection returned to you.

In case of bounded parameterized type, we can think in the way that what is
returned is simply a type known to us when we pass the parameterized Collection, as Collection<String>, Collection<StringBuffer> or so. So therefore we are freely allowed to add to that Collection

If I say in brief, if the returned Collection is parameterized with bounded wildcard, you must hold its reference in either plain old Collection or Collection with bounded wildcard parameter.

If the returned Collection is parameterized with the type "E" that is declared to be bounded to some type, we can hold the reference to the returned Collection in plain old Collection (as always) and Collection with
any parameterized type bounded to CharSequence.

And finally:
Compiler only restricts us to add anything in case of bounded wildcard (with extends) parameterized type.

*These are my personal findings, while working with variety of modifications
to the program. Notions are correct, as I expect.

IMHO, my whole explanation matches to yours (that you early described well) except the modification issue.

Please agree/disagree with this.
[ May 02, 2007: Message edited by: Chandra Bhatt ]
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

I too tried a couple of examples... I completely agree with your findings and explanations.

Thanks,
Megha
 
reply
    Bookmark Topic Watch Topic
  • New Topic