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 - 02 Jul 2007 22:17 GMT
> Consider this code snippet:
>
[quoted text clipped - 4 lines]
> // 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 - 02 Jul 2007 22:34 GMT
Roedy Green wrote On 07/02/07 17:01,:
> Consider this code snippet:
>
[quoted text clipped - 23 lines]
> 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/GenericsFAQ/FAQSections/ParameterizedTypes.html#Ca
n%20I%20create%20an%20array%20whose%20component%20type%20is%20a%20concrete%20ins
tantiation%20of%20a%20parameterized%20type?
and
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#How
%20do%20I%20generically%20create%20objects%20and%20arrays?
helpful.

Signature
Eric.Sosman@sun.com
Roedy Green - 02 Jul 2007 23:00 GMT
>http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#Ca
n%20I%20create%20an%20array%20whose%20component%20type%20is%20a%20concrete%20ins
tantiation%20of%20a%20parameterized%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
Bart - 02 Jul 2007 22:37 GMT
> ...
> 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.
Roedy Green - 03 Jul 2007 01:12 GMT
>You mean like this?
>
[quoted text clipped - 4 lines]
>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 - 03 Jul 2007 17:26 GMT
> > ...
> > HashMap<String, Integer> h = new HashMap<String, Integer>();
[quoted text clipped - 5 lines]
>
> 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 - 03 Jul 2007 18:54 GMT
> ...
> 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 - 03 Jul 2007 21:20 GMT
>>From you webpage:
>
[quoted text clipped - 7 lines]
>
>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
Roedy Green - 03 Jul 2007 21:22 GMT
On Tue, 03 Jul 2007 20:20:03 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :
>It is not different. It just not the problem we were talking about,
>arrays of generics. Look in the code sample about autoboxing.
The code you quoted is an array of ordinary Strings, not an array of
generics. The problem comes here when you try to allocate an array of
generics:
// extract key/value pair entries into an array
Set<Map.Entry<String, Integer>> justEntries =
h.entrySet();
// Infuriatingly, this generates an unchecked conversion
warning message.
Map.Entry<String, Integer>[] keyValuePairs =
justEntries.toArray( new
Map.Entry[justEntries.size()] );
// Type erasure won't let us say:
// Map.Entry<String, Integer>[] keyValuePairs =
// justEntries.toArray ( new
// Map.Entry<String,Integer>[justEntries.size()] );
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Tom Hawtin - 02 Jul 2007 22:51 GMT
> // create a new HashMap
> HashMap<String, Integer> h = new HashMap<String, Integer>(149, .75f);
[quoted text clipped - 4 lines]
> 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