Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Generics, damned if you do, damned if you don't (http://www.velocityreviews.com/forums/t519184-generics-damned-if-you-do-damned-if-you-dont.html)

Roedy Green 07-02-2007 09:01 PM

Generics, damned if you do, damned if you don't
 

Consider this code snippet:

// create a new HashMap
HashMap<String, Integer> h = new HashMap<String, Integer>(149, .75f);
....

// extract key/value pair entries into an array
Set<Map.Entry<String, String>> justEntries = h.entrySet();
Map.Entry<String, String>[] keyValuePairs =
justEntries.toArray ( new Map.Entry[justEntries.size()] );

This generates an "unchecked conversion" warning.

If put in the type like this:

// extract key/value pair entries into an array
Set<Map.Entry<String, String>> justEntries = h.entrySet();
Map.Entry<String, String>[] keyValuePairs =
justEntries.toArray ( new
Map.Entry<String,String>[justEntries.size()] );

I get an "generic array conversion error"

How are you supposed to code that.

Map.Entry is an interface and HashMap.Entry is not public. However
the problem appears to be general -- even creating arrays af HashMaps.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Joshua Cranmer 07-02-2007 09:17 PM

Re: Generics, damned if you do, damned if you don't
 
On Mon, 02 Jul 2007 21:01:05 +0000, Roedy Green wrote:

> Consider this code snippet:
>
> // create a new HashMap
> HashMap<String, Integer> h = new HashMap<String, Integer>(149, .75f);
> ...
>
> // extract key/value pair entries into an array
> Set<Map.Entry<String, String>> justEntries = h.entrySet();

Don't you mean Map.Entry<String, Integer> ?
> Map.Entry<String, String>[] keyValuePairs = justEntries.toArray
> ( new Map.Entry[justEntries.size()] );
>
> This generates an "unchecked conversion" warning.
> How are you supposed to code that.


Go with the first one, bite the bullet, and add a @SuppressWarnings
("unchecked") in. Arrays + generics don't mix. In the Sun generics FAQ,
there's a part about why there are such problems with generic arrays
(simple answer: type erasure).

Eric Sosman 07-02-2007 09:34 PM

Re: Generics, damned if you do, damned if you don't
 
Roedy Green wrote On 07/02/07 17:01,:
> Consider this code snippet:
>
> // create a new HashMap
> HashMap<String, Integer> h = new HashMap<String, Integer>(149, .75f);
> ...
>
> // extract key/value pair entries into an array
> Set<Map.Entry<String, String>> justEntries = h.entrySet();
> Map.Entry<String, String>[] keyValuePairs =
> justEntries.toArray ( new Map.Entry[justEntries.size()] );
>
> This generates an "unchecked conversion" warning.
>
> If put in the type like this:
>
> // extract key/value pair entries into an array
> Set<Map.Entry<String, String>> justEntries = h.entrySet();
> Map.Entry<String, String>[] keyValuePairs =
> justEntries.toArray ( new
> Map.Entry<String,String>[justEntries.size()] );
>
> I get an "generic array conversion error"
>
> How are you supposed to code that.
>
> Map.Entry is an interface and HashMap.Entry is not public. However
> the problem appears to be general -- even creating arrays af HashMaps.


You may find

http://www.angelikalanger.com/Generi...erized%20type?

and

http://www.angelikalanger.com/Generi...0and%20arrays?

helpful.

--
Eric.Sosman@sun.com

Bart 07-02-2007 09:37 PM

Re: Generics, damned if you do, damned if you don't
 
> ...
> I get an "generic array conversion error"
>
> How are you supposed to code that.
> ...


AFAIK, there's no "generics" way to do this, suppressing it is the
only way unfrtunatelly, I guess.


> ...
> even creating arrays af HashMaps.
> ...


You mean like this?

HashMap<String, Integer> h = new HashMap<String, Integer>();
String[] keys = h.keySet().toArray(new String[]{});
Integer[] values = h.values().toArray(new Integer[]{});

which gives no compiler errors/warnings (with Eclpise's compiler, that
is).

Regards,
Bart.


Tom Hawtin 07-02-2007 09:54 PM

Re: Generics, damned if you do, damned if you don't
 
Roedy Green wrote:
>
> // create a new HashMap
> HashMap<String, Integer> h = new HashMap<String, Integer>(149, .75f);
> ...
>
> // extract key/value pair entries into an array
> Set<Map.Entry<String, String>> justEntries = h.entrySet();
> Map.Entry<String, String>[] keyValuePairs =
> justEntries.toArray ( new Map.Entry[justEntries.size()] );


> Map.Entry is an interface and HashMap.Entry is not public. However
> the problem appears to be general -- even creating arrays af HashMaps.



Avoid arrays of reference types. We now have the much nicer generic List.

So the code becomes:

List<Map.Entry<String,String>> entries =
new java.util.ArrayList<Map.Entry<String,String>>(
map.entrySet();
);

Tom Hawtin

Roedy Green 07-02-2007 10:00 PM

Re: Generics, damned if you do, damned if you don't
 
On Mon, 02 Jul 2007 17:34:37 -0400, Eric Sosman <Eric.Sosman@sun.com>
wrote, quoted or indirectly quoted someone who said :

>
>http://www.angelikalanger.com/Generi...erized%20type?


I have often wondered what this ultra competent and supremely
altruistic woman looked like:
http://angelikalanger.com/
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green 07-03-2007 12:12 AM

Re: Generics, damned if you do, damned if you don't
 
On Mon, 02 Jul 2007 21:37:11 -0000, Bart <bkiers@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>
>You mean like this?
>
> HashMap<String, Integer> h = new HashMap<String, Integer>();
> String[] keys = h.keySet().toArray(new String[]{});
> Integer[] values = h.values().toArray(new Integer[]{});
>
>which gives no compiler errors/warnings (with Eclpise's compiler, that
>is).

no. You don't allocate any arrays of generics there. See
http://mindprod.com/jgloss/hashmap.html
for sample code.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Bart 07-03-2007 04:26 PM

Re: Generics, damned if you do, damned if you don't
 
> > ...
> > HashMap<String, Integer> h = new HashMap<String, Integer>();
> > String[] keys = h.keySet().toArray(new String[]{});
> > Integer[] values = h.values().toArray(new Integer[]{});

>
> > which gives no compiler errors/warnings (with Eclpise's compiler, that
> > is).

>
> no. You don't allocate any arrays of generics there.


I don't get your point. What IS it that I do then?


> See http://mindprod.com/jgloss/hashmap.html for sample code.
> ...


>From you webpage:


// extract the keys into an array
Set<String> justKeys = h.keySet();
// Use toArray that takes an empty array,
// otherwise we end up with a useless Object[] instead of a
String[].
String[] keys = justKeys.toArray( new
String[justKeys.size()] );

How is the above different than the code I posted?

Regards,
Bart.


Bart 07-03-2007 05:54 PM

Re: Generics, damned if you do, damned if you don't
 
> ...
> How is the above different than the code I posted?
> ...


Besides the fact that you explicitly say how many elements the newly
created array should have, whereas I leave that responsibility to
Arrays' toString() method, of course.


Roedy Green 07-03-2007 08:20 PM

Re: Generics, damned if you do, damned if you don't
 
On Tue, 03 Jul 2007 16:26:05 -0000, Bart <bkiers@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>>From you webpage:

>
> // extract the keys into an array
> Set<String> justKeys = h.keySet();
> // Use toArray that takes an empty array,
> // otherwise we end up with a useless Object[] instead of a
>String[].
> String[] keys = justKeys.toArray( new
>String[justKeys.size()] );
>
>How is the above different than the code I posted?


It is not different. It just not the problem we were talking about,
arrays of generics. Look in the code sample about autoboxing.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


All times are GMT. The time now is 04:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.