Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11412

Re: exporting a HashMap

From Roedy Green <see_website@mindprod.com.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: exporting a HashMap
Date 2012-01-17 09:54 -0800
Organization Canadian Mind Products
Message-ID <fadbh7l8f0j34ma3mkjv78h7fvevbvcm9v@4ax.com> (permalink)
References (1 earlier) <4f130c08$0$292$14726298@news.sunsite.dk> <jev5gh$bum$2@news.albasani.net> <cis8h757coskrja9mcf1nou7ihgr9g2grf@4ax.com> <4f1480ce$0$292$14726298@news.sunsite.dk> <jf27p8$3qt$1@news.albasani.net>

Show all headers | View raw


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&gt;b, 0 if a==b, -ve if a&lt;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".
 

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web