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


Groups > comp.lang.java.programmer > #34289 > unrolled thread

hashtables syntax

Started byPopping mad <rainbow@colition.gov>
First post2016-05-15 06:31 +0000
Last post2016-05-20 18:00 -0700
Articles 14 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  hashtables syntax Popping mad <rainbow@colition.gov> - 2016-05-15 06:31 +0000
    Re: hashtables syntax Lew <lewbloch@gmail.com> - 2016-05-14 23:52 -0700
      Re: hashtables syntax Popping mad <rainbow@colition.gov> - 2016-05-15 10:23 +0000
        Re: hashtables syntax Lew <lewbloch@gmail.com> - 2016-05-15 09:33 -0700
          Re: hashtables syntax Lew <lewbloch@gmail.com> - 2016-05-15 09:35 -0700
        Re: hashtables syntax Eric Douglas <e.d.programmer@gmail.com> - 2016-05-16 06:10 -0700
    Re: hashtables syntax Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-15 07:44 -0400
      Re: hashtables syntax ruben safir <ruben@mrbrklyn.com> - 2016-05-16 01:26 -0400
        Re: hashtables syntax Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-16 07:48 -0400
          Re: hashtables syntax Steven Simpson <ss@my.domain.invalid> - 2016-05-16 15:51 +0100
            Re: hashtables syntax Steven Simpson <ss@my.domain.invalid> - 2016-05-16 22:35 +0100
        Re: hashtables syntax Lew <lewbloch@gmail.com> - 2016-05-16 16:10 -0700
    Re: hashtables syntax Andreas Leitgeb <avl@logic.at> - 2016-05-15 13:43 +0000
    Re: hashtables syntax Roedy Green <see_website@mindprod.com.invalid> - 2016-05-20 18:00 -0700

#34289 — hashtables syntax

FromPopping mad <rainbow@colition.gov>
Date2016-05-15 06:31 +0000
Subjecthashtables syntax
Message-ID<nh954m$bu8$1@reader1.panix.com>
This whole syntax is new and strange to me, seemingly illogical

   private static HashMap<String, Type[]> functions; // built-in
function types

    static { // initialize function types
        functions = new HashMap<>();
        functions.put("floor",      new Type[]{Type.FLOAT, Type.INT});
        functions.put("sqrt",       new Type[]{Type.FLOAT, Type.FLOAT});
        functions.put("log",        new Type[]{Type.FLOAT, Type.FLOAT,
Type.FLOAT});
        functions.put("random",     new Type[]{Type.FLOAT});
        functions.put("parseInt",   new Type[]{Type.STRING, Type.INT});
        functions.put("parseFloat", new Type[]{Type.STRING, Type.FLOAT});
        functions.put("readLine",   new Type[]{Type.STRING});
        functions.put("showInt",    new Type[]{Type.INT, Type.STRING});
        functions.put("showFloat",  new Type[]{Type.FLOAT, Type.STRING});
    }


If we are creating a private static Hashmap with template elements of a
String and and array of Types, then why do I have to call

functions = new HashMap<>();

and why nothing in the template.  And what does static do in this case?

[toc] | [next] | [standalone]


#34290

FromLew <lewbloch@gmail.com>
Date2016-05-14 23:52 -0700
Message-ID<4dd68788-c9d0-4b18-8084-d68dfb93f489@googlegroups.com>
In reply to#34289
Popping mad wrote:
> This whole syntax is new and strange to me, seemingly illogical

If you don't know the language yet, judgments like "illogical" can only impede your progress.

>    private static HashMap<String, Type[]> functions; // built-in
> function types
> 
>     static { // initialize function types
>         functions = new HashMap<>();
>         functions.put("floor",      new Type[]{Type.FLOAT, Type.INT});
>         functions.put("sqrt",       new Type[]{Type.FLOAT, Type.FLOAT});
>         functions.put("log",        new Type[]{Type.FLOAT, Type.FLOAT,
> Type.FLOAT});
>         functions.put("random",     new Type[]{Type.FLOAT});
>         functions.put("parseInt",   new Type[]{Type.STRING, Type.INT});
>         functions.put("parseFloat", new Type[]{Type.STRING, Type.FLOAT});
>         functions.put("readLine",   new Type[]{Type.STRING});
>         functions.put("showInt",    new Type[]{Type.INT, Type.STRING});
>         functions.put("showFloat",  new Type[]{Type.FLOAT, Type.STRING});
>     }
> 
> 
> If we are creating a private static Hashmap with template elements of a

Java does not have templates.

> String and and array of Types, then why do I have to call
> 
> functions = new HashMap<>();

Because there is a difference between declaration and initialization or assignment.

> and why nothing in the template.

Java does not have templates. The "diamond" operator indicates the presence of a generic type. The syntax for this is explained both in the Java Language Specification and the Oracle Java Tutorials, which I commend to your attention.

>  And what does static do in this case?

Again, the keyword 'static' is documented as indicated above. It signifies the association of a member with the class as a whole rather than any instance thereof.

The keywords in the language are among the first things presented in the tutorials and specification. Both have indexes and tables of contents. It is but the work of a moment to look up the meaning in the documentation, a practice which I also commend to your attention.

[toc] | [prev] | [next] | [standalone]


#34291

FromPopping mad <rainbow@colition.gov>
Date2016-05-15 10:23 +0000
Message-ID<nh9imo$r51$1@reader1.panix.com>
In reply to#34290
On Sat, 14 May 2016 23:52:48 -0700, Lew wrote:

> The syntax for this is explained both in the
> Java Language Specification and the Oracle Java Tutorials, which I commend
> to your attention.

we are blocked from Oracle.  I think the boss had  disagreement with them 

[toc] | [prev] | [next] | [standalone]


#34297

FromLew <lewbloch@gmail.com>
Date2016-05-15 09:33 -0700
Message-ID<d59eec5c-0de0-4732-a384-4c408eb2b8de@googlegroups.com>
In reply to#34291
On Sunday, May 15, 2016 at 3:23:30 AM UTC-7, Popping mad wrote:
> On Sat, 14 May 2016 23:52:48 -0700, Lew wrote:
> 
> > The syntax for this is explained both in the
> > Java Language Specification and the Oracle Java Tutorials, which I commend
> > to your attention.
> 
> we are blocked from Oracle.  I think the boss had  disagreement with them

That is one of the most ridiculous excuses I have heard in my life. Have you no computer of 
your own? A smartphone? A friend with a computer? A nearby library?

If I were a manager of someone who gave boneheaded excuses like that, I'd fire them. Show some freaking initiative!

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#34298

FromLew <lewbloch@gmail.com>
Date2016-05-15 09:35 -0700
Message-ID<5f964dcb-8972-46c0-82b0-0f65e49cae6d@googlegroups.com>
In reply to#34297
On Sunday, May 15, 2016 at 9:34:01 AM UTC-7, Lew wrote:
> On Sunday, May 15, 2016 at 3:23:30 AM UTC-7, Popping mad wrote:
> > On Sat, 14 May 2016 23:52:48 -0700, Lew wrote:
> > 
> > > The syntax for this is explained both in the
> > > Java Language Specification and the Oracle Java Tutorials, which I commend
> > > to your attention.
> > 
> > we are blocked from Oracle.  I think the boss had  disagreement with them
> 
> That is one of the most ridiculous excuses I have heard in my life. Have you no computer of 
> your own? A smartphone? A friend with a computer? A nearby library?
> 
> If I were a manager of someone who gave boneheaded excuses like that, I'd fire them. Show some freaking initiative!

And how the bloody blazes are you even *using* Java if you can't access the oracle.com domain, hm? I think your excuse has more holes than a screen door.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#34302

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-05-16 06:10 -0700
Message-ID<73d90980-9998-4219-8922-075de630201d@googlegroups.com>
In reply to#34291
On Sunday, May 15, 2016 at 6:23:30 AM UTC-4, Popping mad wrote:
> we are blocked from Oracle.  I think the boss had  disagreement with them

You might want to have a chat with your Preventer of IT Services.  It seems like a bad idea for any business to use Java code and not allow access to Oracle.

[toc] | [prev] | [next] | [standalone]


#34292

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-05-15 07:44 -0400
Message-ID<nh9n7r$ira$1@dont-email.me>
In reply to#34289
On 5/15/2016 2:31 AM, Popping mad wrote:
>
> This whole syntax is new and strange to me, seemingly illogical
>
>    private static HashMap<String, Type[]> functions; // built-in
> function types
>
>     static { // initialize function types
>         functions = new HashMap<>();
>         functions.put("floor",      new Type[]{Type.FLOAT, Type.INT});
>         functions.put("sqrt",       new Type[]{Type.FLOAT, Type.FLOAT});
>         functions.put("log",        new Type[]{Type.FLOAT, Type.FLOAT,
> Type.FLOAT});
>         functions.put("random",     new Type[]{Type.FLOAT});
>         functions.put("parseInt",   new Type[]{Type.STRING, Type.INT});
>         functions.put("parseFloat", new Type[]{Type.STRING, Type.FLOAT});
>         functions.put("readLine",   new Type[]{Type.STRING});
>         functions.put("showInt",    new Type[]{Type.INT, Type.STRING});
>         functions.put("showFloat",  new Type[]{Type.FLOAT, Type.STRING});
>     }
>
>
> If we are creating a private static Hashmap with template elements of a
> String and and array of Types, then why do I have to call
>
> functions = new HashMap<>();

     You need to create the HashMap before you can start filling it.
You can't put the animals into the Ark before the Ark is built.

> and why nothing in the template.  And what does static do in this case?

     By "template" I guess you mean the <> angle braces?  That's where
the type parameters would go, just as in the declaration.  In fact, it
would be perfectly legal to repeat the parameters -- but since Java can
figure them out for itself ("Hmmm: They're probably the same types as
I already saw when `functions' was first declared!"), why bother?

     As for `static', I'm not sure which of the two appearances confuses
you.  The first `private static ... functions;' means that `functions'
belongs to the class as a whole, not to any instance of the class.  If
`static' weren't there, each instance of the class would have its very
own `functions' map.  The second appearance `static { // initialize ...'
means that this code belongs to the class as a whole and not to any
particular instance; it will execute when the class itself is being
set up -- and thus, will populate `functions' on behalf of the class.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#34300

Fromruben safir <ruben@mrbrklyn.com>
Date2016-05-16 01:26 -0400
Message-ID<338d0b8c-9a5e-ccc0-9a5c-792c870f84bf@mrbrklyn.com>
In reply to#34292
On 05/15/2016 07:44 AM, Eric Sosman wrote:
> y "template" I guess you mean the <> angle braces?  That's where
> the type parameters would go, just as in the declaration.  In fact, it
> would be perfectly legal to repeat the parameters -- but since Java can
> figure them out for itself ("Hmmm: They're probably the same types as
> I already saw when `functions' was first declared!"), why bother?


Gotchya, but it seems inconsistent.


why not like

HashMap<> function = new HashMap<String, Type[]>

Why declare function as a type at all.


[toc] | [prev] | [next] | [standalone]


#34301

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-05-16 07:48 -0400
Message-ID<nhcbrv$7qk$1@dont-email.me>
In reply to#34300
On 5/16/2016 1:26 AM, ruben safir wrote:
> On 05/15/2016 07:44 AM, Eric Sosman wrote:
>> y "template" I guess you mean the <> angle braces?  That's where
>> the type parameters would go, just as in the declaration.  In fact, it
>> would be perfectly legal to repeat the parameters -- but since Java can
>> figure them out for itself ("Hmmm: They're probably the same types as
>> I already saw when `functions' was first declared!"), why bother?
>
>
> Gotchya, but it seems inconsistent.
>
>
> why not like
>
> HashMap<> function = new HashMap<String, Type[]>

     With what is this "inconsistent?"  I'm not seeing your point.

     The types must be specified on one side or the other; Java
decided to insist that they appear on the left.  Java *could*
have chosen to have them on the right (I suppose, but there may
be considerations I'm unaware of), but it chose the left.  Shrug.

     Bear in mind that when type arguments first arrived in Java
they had to appear on *both* sides!  Count your blessings.

> Why declare function as a type at all.

     Not sure what you mean here.  Every identifier in Java has a
type, and when you introduce a new identifier you must tell the
compiler what type it has.  Why should `function' be unlike all
other identifiers?

     Or maybe you think Java shouldn't be a typed language?  Sorry;
that ship sailed long, long ago.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#34304

FromSteven Simpson <ss@my.domain.invalid>
Date2016-05-16 15:51 +0100
Message-ID<fd3p0d-s27.ln1@s.simpson148.btinternet.com>
In reply to#34301
On 16/05/16 12:48, Eric Sosman wrote:
> On 5/16/2016 1:26 AM, ruben safir wrote:
>> Gotchya, but it seems inconsistent.
>>
>>
>> why not like
>>
>> HashMap<> function = new HashMap<String, Type[]>
>
>     With what is this "inconsistent?"  I'm not seeing your point.
>
>     The types must be specified on one side or the other; Java
> decided to insist that they appear on the left.  Java *could*
> have chosen to have them on the right (I suppose, but there may
> be considerations I'm unaware of), but it chose the left.  Shrug.

It would arguably be inappropriate to put the type information 'on the 
right' in the the OP's case, where the declaration of the reference is 
separate from its assignment.  The generic parameters would be visible 
only within the static initializer, while the declaration is visible 
throughout.  Imagine:

    private static HashMap<> functions;

     static { // initialize function types
         functions = new HashMap<String, Type[]>();

Compile-time checking of calls like functions.get(20) would have to 
inspect the static initializer, rather than the declaration.  Also, I 
expect it would be more complex to inspect situations like this:

    private static HashMap<> functions;

    // at some point
    functions = new HashMap<String, Type[]>();

    // later
    functions = new HashMap<Integer, String>();

The declaration acts as a kind of rendezvous point between users and 
providers of 'function', so it makes sense to propagate type information 
out from that point.

-- 
ss at comp dot lancs dot ac dot uk

[toc] | [prev] | [next] | [standalone]


#34309

FromSteven Simpson <ss@my.domain.invalid>
Date2016-05-16 22:35 +0100
Message-ID<a2rp0d-ctt.ln1@s.simpson148.btinternet.com>
In reply to#34304
On 16/05/16 19:23, Stefan Ram wrote:
> Steven Simpson <ss@my.domain.invalid> writes:
>>>> HashMap<> function = new HashMap<String, Type[]>
> ..
>> It would arguably be inappropriate to put the type information 'on the
>> right' in the the OP's case, where the declaration of the reference is
>> separate from its assignment.
>    There are examples outside of the scope of generics where
>    the more-detailed information about the type is on the left
>    and examples where it is on the right.


Sure.  I was not suggesting anything beyond the case of choosing whether 
to permit <> on the left or on the right.

-- 
ss at comp dot lancs dot ac dot uk

[toc] | [prev] | [next] | [standalone]


#34310

FromLew <lewbloch@gmail.com>
Date2016-05-16 16:10 -0700
Message-ID<f8a4d1b8-1a31-4ac9-aa6a-3eaec480e2bc@googlegroups.com>
In reply to#34300
ruben safir wrote:
> Gotchya, but it seems inconsistent.

Based on what? Your personal prejudices? There is no engineering rationale for your concern, and saying "it seems" is a weasel-wording for "I have decided that it is", with an implication that somehow this is not your personal and unfounded opinion.

Each programming language has to be taken according to its own lights. I don't know how you have decided that this idiom is "inconsistent", or what "consistent" is in your world, but in fact the syntax is entirely consistent. It is consistent with the definition of the language.

> why not like
> 
> HashMap<> function = new HashMap<String, Type[]>

Because that is not legal syntax in Java.

> Why declare function as a type at all.

Umm, what? Everything in Java has a type. What language did you think we are discussing?

It's also not usually a great idea to mix arrays and generics in Java.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#34294

FromAndreas Leitgeb <avl@logic.at>
Date2016-05-15 13:43 +0000
Message-ID<slrnnjgvfg.456.avl@login.logic.tuwien.ac.at>
In reply to#34289
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> Popping mad <rainbow@colition.gov> writes:
>>This whole syntax is new and strange to me, seemingly illogical
>>   private static HashMap<String, Type[]> functions; // built-in
>>function types
>   It's not a new syntax. It's a syntax error.
> Main.java:5: error: ';' expected
> function types
>               ^
> 1 error

Are you serious, that you didn't recognize this as a word wrap
artefact?

Or were you just bored and decided to pretend not to notice it?

[toc] | [prev] | [next] | [standalone]


#34359

FromRoedy Green <see_website@mindprod.com.invalid>
Date2016-05-20 18:00 -0700
Message-ID<ancvjbhe2hqbavffidh3bde7kmd17459o0@4ax.com>
In reply to#34289
On Sun, 15 May 2016 06:31:50 +0000 (UTC), Popping mad
<rainbow@colition.gov> wrote, quoted or indirectly quoted someone who
said :

>This whole syntax is new and strange to me, seemingly illogical

see http://mindprod.com/jgloss/hashtable.html

It is pretty much obsolete.
use
http://mindprod.com/jgloss/hashmap.html
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Incorrect documentation is often worse than no documentation.
~ Bertrand Meyer (born:1950 age 65)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web