Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11347 > unrolled thread
| Started by | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| First post | 2012-01-15 08:30 -0800 |
| Last post | 2012-01-15 21:02 -0800 |
| Articles | 15 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
exporting a HashMap Roedy Green <see_website@mindprod.com.invalid> - 2012-01-15 08:30 -0800
Re: exporting a HashMap Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-15 12:01 -0500
Re: exporting a HashMap Roedy Green <see_website@mindprod.com.invalid> - 2012-01-16 02:06 -0800
Re: exporting a HashMap Roedy Green <see_website@mindprod.com.invalid> - 2012-01-17 09:04 -0800
Re: exporting a HashMap Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-17 20:38 -0500
Re: exporting a HashMap Arne Vajhøj <arne@vajhoej.dk> - 2012-01-15 12:25 -0500
Re: exporting a HashMap Lew <noone@lewscanon.com> - 2012-01-15 10:22 -0800
Re: exporting a HashMap Roedy Green <see_website@mindprod.com.invalid> - 2012-01-16 10:55 -0800
Re: exporting a HashMap Arne Vajhøj <arne@vajhoej.dk> - 2012-01-16 14:55 -0500
Re: exporting a HashMap Lew <noone@lewscanon.com> - 2012-01-16 14:19 -0800
Re: exporting a HashMap David Lamb <dalamb@cs.queensu.ca> - 2012-01-16 18:31 -0500
Re: exporting a HashMap Roedy Green <see_website@mindprod.com.invalid> - 2012-01-17 09:54 -0800
Re: exporting a HashMap Arne Vajhøj <arne@vajhoej.dk> - 2012-01-16 15:07 -0500
Re: exporting a HashMap Steven Simpson <ss@domain.invalid> - 2012-01-15 19:22 +0000
Re: exporting a HashMap Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-15 21:02 -0800
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-01-15 08:30 -0800 |
| Subject | exporting a HashMap |
| Message-ID | <ljv5h7pl5t57ikpoqntvu1iif4tkrl9l7t@4ax.com> |
What is best code to export key-value pairs from a HashMap into some structure in key-value order. One possible way is to extract the keys, sort them, then look up the values. That strikes me as infantile. Surely there is a better way. The best I can think of is to invent a Pair dummy class, extract the fields from an Entry into the two fields, and sort them in an array. Part of the problem is generics and arrays don't mix. -- Roedy Green Canadian Mind Products http://mindprod.com One of the most useful comments you can put in a program is "If you change this, remember to change ?XXX? too".
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-01-15 12:01 -0500 |
| Message-ID | <jev0q9$aoo$1@dont-email.me> |
| In reply to | #11347 |
On 1/15/2012 11:30 AM, Roedy Green wrote:
> What is best code to export key-value pairs from a HashMap into some
> structure in key-value order.
>
> One possible way is to extract the keys, sort them, then look up the
> values. That strikes me as infantile. Surely there is a better way.
>
> The best I can think of is to invent a Pair dummy class, extract the
> fields from an Entry into the two fields, and sort them in an array.
>
> Part of the problem is generics and arrays don't mix.
Why invent a Pair when Map.Entry already holds the data? Just
typed in, unchecked:
Map<K,V> map = ...;
List<Map.Entry<K,V>> entries =
new ArrayList<Map.Entry<K,V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
@Override
public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
return e1.getKey().compareTo(e2.getKey());
}
});
for (Map.Entry<K,V> e : entries) { ... }
There's scary Javadoc about the longevity of Map.Entry, but as long
as the underlying Map isn't changed all should be well.
Another approach is to use a SortedMap, either ab initio or as
a substitute for the List-and-sort above:
Map<K,V> map = ...;
SortedMap<K,V> map2 = new TreeMap<K,V>(map);
for (Map.Entry<K,V> e : map2.entrySet()) { ... }
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-01-16 02:06 -0800 |
| Message-ID | <fr96h7pe96eslbl6o0a8vsvots4p8sn5p2@4ax.com> |
| In reply to | #11351 |
On Sun, 15 Jan 2012 12:01:55 -0500, Eric Sosman <esosman@ieee-dot-org.invalid> wrote, quoted or indirectly quoted someone who said : > Why invent a Pair when Map.Entry already holds the data? very good. I was getting stuck because the only thing Set had for export was asArray. You cleverly looked for something that could import a Set. -- Roedy Green Canadian Mind Products http://mindprod.com One of the most useful comments you can put in a program is "If you change this, remember to change ?XXX? too".
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-01-17 09:04 -0800 |
| Message-ID | <lcabh75qqo9b1qtsat1q8g6sobvf0o8iss@4ax.com> |
| In reply to | #11351 |
On Sun, 15 Jan 2012 12:01:55 -0500, Eric Sosman
<esosman@ieee-dot-org.invalid> wrote, quoted or indirectly quoted
someone who said :
>
> Map<K,V> map = ...;
> List<Map.Entry<K,V>> entries =
> new ArrayList<Map.Entry<K,V>>(map.entrySet());
> Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
> @Override
> public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
> return e1.getKey().compareTo(e2.getKey());
> }
> });
> for (Map.Entry<K,V> e : entries) { ... }
I have added your technique with attribution to
http://mindprod.com/jgloss/hashmap.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
One of the most useful comments you can put in a program is
"If you change this, remember to change ?XXX? too".
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-01-17 20:38 -0500 |
| Message-ID | <jf57qu$upe$1@dont-email.me> |
| In reply to | #11409 |
On 1/17/2012 12:04 PM, Roedy Green wrote:
> On Sun, 15 Jan 2012 12:01:55 -0500, Eric Sosman
> <esosman@ieee-dot-org.invalid> wrote, quoted or indirectly quoted
> someone who said :
>
>>
>> Map<K,V> map = ...;
>> List<Map.Entry<K,V>> entries =
>> new ArrayList<Map.Entry<K,V>>(map.entrySet());
>> Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
>> @Override
>> public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
>> return e1.getKey().compareTo(e2.getKey());
>> }
>> });
>> for (Map.Entry<K,V> e : entries) { ... }
>
> I have added your technique with attribution to
> http://mindprod.com/jgloss/hashmap.html
Thanks. Actually, I thought my other suggestion was slicker,
certainly briefer.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2012-01-15 12:25 -0500 |
| Message-ID | <4f130c08$0$292$14726298@news.sunsite.dk> |
| In reply to | #11347 |
On 1/15/2012 11:30 AM, Roedy Green wrote: > What is best code to export key-value pairs from a HashMap into some > structure in key-value order. > > One possible way is to extract the keys, sort them, then look up the > values. That strikes me as infantile. Surely there is a better way. > > The best I can think of is to invent a Pair dummy class, extract the > fields from an Entry into the two fields, and sort them in an array. > > Part of the problem is generics and arrays don't mix. Generics and ArrayList mix fine. Arne
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-01-15 10:22 -0800 |
| Message-ID | <jev5gh$bum$2@news.albasani.net> |
| In reply to | #11352 |
Arne Vajhøj wrote: > Roedy Green wrote: >> What is best code to export key-value pairs from a HashMap into some >> structure in key-value order. >> >> One possible way is to extract the keys, sort them, then look up the >> values. That strikes me as infantile. Surely there is a better way. >> >> The best I can think of is to invent a Pair dummy class, extract the >> fields from an Entry into the two fields, and sort them in an array. >> >> Part of the problem is generics and arrays don't mix. > > Generics and ArrayList mix fine. And anyway, generics and arrays do mix, just not well. To say merely that they don't mix, without the qualifier "well", isinaccurate and misleading, and certainly no help to newbies. Better would be to explain the limits, why they exist, and what the alternatives are. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-01-16 10:55 -0800 |
| Message-ID | <cis8h757coskrja9mcf1nou7ihgr9g2grf@4ax.com> |
| In reply to | #11355 |
On Sun, 15 Jan 2012 10:22:08 -0800, Lew <noone@lewscanon.com> wrote, quoted or indirectly quoted someone who said : >And anyway, generics and arrays do mix, just not well. To say merely that they >don't mix, without the qualifier "well", isinaccurate and misleading, and >certainly no help to newbies. Better would be to explain the limits, why they >exist, and what the alternatives are. If you were charged with writing a paragraph or to for newbies to explain the impedance mismatch of arrays and generics what would you say? -- Roedy Green Canadian Mind Products http://mindprod.com One of the most useful comments you can put in a program is "If you change this, remember to change ?XXX? too".
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2012-01-16 14:55 -0500 |
| Message-ID | <4f1480ce$0$292$14726298@news.sunsite.dk> |
| In reply to | #11384 |
On 1/16/2012 1:55 PM, Roedy Green wrote: > On Sun, 15 Jan 2012 10:22:08 -0800, Lew<noone@lewscanon.com> wrote, > quoted or indirectly quoted someone who said : >> And anyway, generics and arrays do mix, just not well. To say merely that they >> don't mix, without the qualifier "well", isinaccurate and misleading, and >> certainly no help to newbies. Better would be to explain the limits, why they >> exist, and what the alternatives are. > > If you were charged with writing a paragraph or to for newbies to > explain the impedance mismatch of arrays and generics what would you > say? "generics and arrays do mix, just not well" perhaps? Arne
[toc] | [prev] | [next] | [standalone]
| From | Lew <noone@lewscanon.com> |
|---|---|
| Date | 2012-01-16 14:19 -0800 |
| Message-ID | <jf27p8$3qt$1@news.albasani.net> |
| In reply to | #11386 |
Arne Vajhøj wrote:
> Roedy Green wrote:
>> Lew wrote:
>>> And anyway, generics and arrays do mix, just not well. To say merely that they
>>> don't mix, without the qualifier "well", is inaccurate and misleading, and
>>> certainly no help to newbies. Better would be to explain the limits, why they
>>> exist, and what the alternatives are.
>>
>> If you were charged with writing a paragraph or to for newbies to
>> explain the impedance mismatch of arrays and generics what would you
>> say?
>
> "generics and arrays do mix, just not well" perhaps?
Along with some specifics. As stated in the cited post, you should explain
the limits and what to do about them. It isn't especially complicated
material. For example, and this is just off the top of my head to stimulate
discussion, not presented as a paragon of prose:
You should not mix arrays and generics.
Generics and arrays do not mix well. That's because arrays "remember" their
underlying type at runtime, but generics just become 'Object' at runtime
through the process of "type erasure". [1] The compiler will not let you
create an array of generic types, unless the generic parameter comprises
entirely unadorned wildcard ('?') characters. So
Foo<?> [] bunchaFoos = new Foo<?> [NUMFOOS];
is legal, but
Foo<Bar> [] bunchaFoos = new Foo<Bar> [NUMFOOS];
is not. Other things like casting and reflection get really difficult, too.
For almost everything you want to do mixing arrays and generics you can use
'ArrayList' instead of an array. The syntax is a little more verbose but the
type safety and expressiveness compensate.
[1] In technical terms, an array is a "reifiable" type - it can be made "real"
in the JVM. Consequently its base type must also be reifiable. A generic
type, except for the pure wildcard '?' generics, is not reifiable because of
erasure. So the compiler won't let you make an array of a generic type.
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
[toc] | [prev] | [next] | [standalone]
| From | David Lamb <dalamb@cs.queensu.ca> |
|---|---|
| Date | 2012-01-16 18:31 -0500 |
| Message-ID | <jf2c07$l79$1@dont-email.me> |
| In reply to | #11395 |
On 16/01/2012 5:19 PM, Lew wrote: > nice stuff... That's a really nice explanation -- simple statement first, slightly expanded, then details with footnote. I might quibble with some of the wording, but I'm the sort who might quibble with anybody's wording.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-01-17 09:54 -0800 |
| Message-ID | <fadbh7l8f0j34ma3mkjv78h7fvevbvcm9v@4ax.com> |
| In reply to | #11395 |
On Mon, 16 Jan 2012 14:19:20 -0800, Lew <noone@lewscanon.com> wrote,
quoted or indirectly quoted someone who said :
>
>Generics and arrays do not mix well. That's because arrays "remember" their
>underlying type at runtime, but generics just become 'Object' at runtime
>through the process of "type erasure". [1] The compiler will not let you
>create an array of generic types, unless the generic parameter comprises
>entirely unadorned wildcard ('?') characters. So
>
> Foo<?> [] bunchaFoos = new Foo<?> [NUMFOOS];
>
>is legal, but
>
> Foo<Bar> [] bunchaFoos = new Foo<Bar> [NUMFOOS];
>
>is not. Other things like casting and reflection get really difficult, too.
>
>For almost everything you want to do mixing arrays and generics you can use
>'ArrayList' instead of an array. The syntax is a little more verbose but the
>type safety and expressiveness compensate.
>
>[1] In technical terms, an array is a "reifiable" type - it can be made "real"
>in the JVM. Consequently its base type must also be reifiable. A generic
>type, except for the pure wildcard '?' generics, is not reifiable because of
>erasure. So the compiler won't let you make an array of a generic type.
I have added this to http://mindprod.com/jgloss/generics.html with
attribution.
There is an outstanding question your entry triggered:
/*
* @(#)Alphabetically.java
*
* Summary: Describe/summarise the comparison here..
*
* Requires: JDK 1.5+
*
* Created with: Canadian Mind Products ComparatorCutter.
*
* Version History:
* 1.0 2012-01-17 - initial release
*/
// <> package ...
import java.util.Comparator;
/**
* Describe/summarise the comparison here..
* <p/>
* Defines an alternate sort order for Thing.
*
* @author ...
* @version 1.0 2012-01-17 - initial release
* @since 2012-01-17
*/
class Alphabetically implements Comparator<Thing>
{
/**
* Describe/summarise the comparison here..
* Defines an alternate sort order for Thing with JDK 1.5+
generics.
* Compare two Thing Objects.
* Compares name case sensitively.
* Informally, returns (a-b), or +ve if a sorts after b.
* The Java source code for this Comparator was generated by the
* Canadian Mind Products ComparatorCutter Applet at
http://mindprod.com/applet/comparatorcutter.html
* For non-military purposes only.
*
* @param a first Thing to compare
* @param b second Thing to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( @NotNull Thing a, @NotNull Thing b )
{
return a.name.compareTo( b.name );
}
}
at run time, what type are the parameters to compare, Thing or Object?
--
Roedy Green Canadian Mind Products
http://mindprod.com
One of the most useful comments you can put in a program is
"If you change this, remember to change ?XXX? too".
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2012-01-16 15:07 -0500 |
| Message-ID | <4f14839c$0$287$14726298@news.sunsite.dk> |
| In reply to | #11355 |
On 1/15/2012 1:22 PM, Lew wrote: > Arne Vajhøj wrote: >> Roedy Green wrote: >>> What is best code to export key-value pairs from a HashMap into some >>> structure in key-value order. >>> >>> One possible way is to extract the keys, sort them, then look up the >>> values. That strikes me as infantile. Surely there is a better way. >>> >>> The best I can think of is to invent a Pair dummy class, extract the >>> fields from an Entry into the two fields, and sort them in an array. >>> >>> Part of the problem is generics and arrays don't mix. >> >> Generics and ArrayList mix fine. > > And anyway, generics and arrays do mix, just not well. To say merely > that they don't mix, without the qualifier "well", isinaccurate and > misleading, and certainly no help to newbies. Better would be to explain > the limits, why they exist, and what the alternatives are. Or maybe just use the "well" modifier to keep focus on the original problem. Arne
[toc] | [prev] | [next] | [standalone]
| From | Steven Simpson <ss@domain.invalid> |
|---|---|
| Date | 2012-01-15 19:22 +0000 |
| Message-ID | <jllbu8-vjj.ln1@news.simpsonst.f2s.com> |
| In reply to | #11347 |
On 15/01/12 16:30, Roedy Green wrote: > What is best code to export key-value pairs from a HashMap into some > structure in key-value order. What is meant by key-value order? -- ss at comp dot lancs dot ac dot uk
[toc] | [prev] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2012-01-15 21:02 -0800 |
| Message-ID | <SbOQq.445$DC7.289@newsfe12.iad> |
| In reply to | #11347 |
On 1/15/12 8:30 AM, Roedy Green wrote: > What is best code to export key-value pairs from a HashMap into some > structure in key-value order. > > One possible way is to extract the keys, sort them, then look up the > values. That strikes me as infantile. Surely there is a better way. > > The best I can think of is to invent a Pair dummy class, extract the > fields from an Entry into the two fields, and sort them in an array. > > Part of the problem is generics and arrays don't mix. Uh, TreeMap is in key order. Why would you use HashMap if you want key order?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web