1

I'm following the indications on this post to create a a parametrized URI for an Http GetMethod (http://some.domain/path?param1=value1&param2=value2) and I ran into a new issue.

I have this code:

List<NameValuePair> params = new ArrayList<NameValuePair>();
...
some code which does: params.add(new BasicNameValuePair(paramName, paramValue));
...
String paramString = URLEncodedUtils.format(params, "utf-8");

However, my Eclipse says that URLEncodeUtils.format does not accept type List<NameValuePair> but only List<? extends NameValuePair>.

I thought this may mean that only a subclass would be accepted (I see no sense in it, though, since NameValuePair is not abstract) but I also tried doing this with no luck:

class NameValuePairExtension extends NameValuePair{}
List<NameValuePairExtension> params = new ArrayList<NameValuePairExtension>();

What does this exactly mean?

EDIT:

Thanks for your quick replies.

A part from the theorical repsonses, I just found the "practical solution": NameValuePair is an Interface, so it format connot work with it, and requires an implementation.

Therefore, the solution is to replace NameValuePair by a BasicNameValuePair, which implements the first one.

However, I still find this "extends" a bit confusing.

Community
  • 1
  • 1
Carles Sala
  • 1,989
  • 1
  • 16
  • 34
  • I think we've answered many of these Generics questions here on SO, no? :p – Buhake Sindi Aug 31 '11 at 10:41
  • My appologises if this is the case. I tried searching through the posts, but I found nothing. Maybe the "?" made the search fail, and serching only for "extends" lead to lots of other issues – Carles Sala Aug 31 '11 at 11:28

2 Answers2

2

Xxx<? extends Yyy> means that Xxx has a generic type parameter that can be whatever you want while it implements interface Yyy (if Yyy is an interface) or extends class Yyy (if Yyy is a class).

Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
  • 1
    Thanks for your Response! I just posted a comment in Jon's Answer: I just realized that NameValuePair is an Interface, so the reason may be the lack of implementation, whether it is an extension or not. – Carles Sala Aug 31 '11 at 11:27
2

It means it's a list of some type which extends NameValuePair (or is NameValuePair) but we don't know what type, and we don't care.

This is a wildcard in Java generics. See the Java Generics FAQ for more details.

EDIT: My guess is that you're using the wrong NameValuePair type. Please post the exact compilation error and it may become clearer...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for your comment. However, in this case none of these things worked: neither NameValuePair nor an extension. However, I just realized that NameValuePair is an Interface, so this may be the reason on the failure – Carles Sala Aug 31 '11 at 11:25
  • @Carles: Are you sure you've got the right `NameValuePair` type? – Jon Skeet Aug 31 '11 at 11:26
  • Thanks for the tip. However, I already found the exact problem and edited the initial question to include it – Carles Sala Aug 31 '11 at 11:34
  • @Carles: `format` should be absolutely fine with a `List`. I don't think the problem is where you think it is... – Jon Skeet Aug 31 '11 at 11:48