Creating a HashMap and Passing It As a Parameter in One Step

H

Hal Vaughan

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?

Second, is there a way I can create a HashMap the same way, with just one
line, specifying 2-3 keys and their values?

Thanks!

Hal
 
K

Karl Uppiano

Hal Vaughan said:
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.
 
M

Michael Rauscher

Hal said:
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?

Second, is there a way I can create a HashMap the same way, with just one
line, specifying 2-3 keys and their values?


E.g.

public class Maps {
public static final <K,V> HashMap<K, V> asHashMap( K[] keys,
V[] values ) {
HashMap<K, V> result = new HashMap<K, V>();
if ( keys.length != values.length )
throw new IllegalArgumentException();

for ( int i = 0; i < keys.length; i++ )
result.put( keys, values );
return result;
}

public static final <K,V> Map<K, V> asMap( K[] keys, V[] values ) {
return asHashMap( keys, values );
}
}

Now you can do something like this:

HashMap<String, Integer> map = new HashMap<String, Integer>(
Maps.asMap(new String[]{"A","B","C"}, new Integer[]{1,2,3})
);

Or even simpler:

HashMap<String, Integer> map = Maps.asHashMap( ... );

Bye
Michael
 
M

Mike Schilling

Hal Vaughan said:
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?

I've seen it called an anonymous array.
Second, is there a way I can create a HashMap the same way, with just one
line, specifying 2-3 keys and their values?

The short answer is "no"; only arrays have this special syntax, probably
because arrays are the only built-in aggregate type. Of course, there's
nothing to stop you from writing a HashMapCreator class, something like (not
compiled or tested, so please ignore any typos)

public class HashMapCreator
{
public static Map create(Object[] params)
{
HashMap map = new HashMap();
for (int i = 0; i < params.length; i+=2)
{
map.put(params, params[i+1];
}
return map;
}
}

which lets you write

Map map = HashMapCreator.create(new Object[] {"color", "red", "size",
"XXL"});

In Java 1.5 you can make create a varargs method and make the call just

HashMapCreator.create("color", "red", "size", "XXL");
 
D

Doug Pardee

Hal said:
is there a way I can create a HashMap the same way, with just one
line, specifying 2-3 keys and their values?

setValues(new HashMap(){{put("a","value-a"); put("c","value-c");}});

Yes, it's a messy syntax with parentheses, curly braces, and semicolons
scattered about willy-nilly.

The way that this works is that the statements inside of the second set
of curly braces constitute the "instance initializer" for the anonymous
subclass of HashMap. It's kind of like a constructor with no
parameters.

See section 8.6 of the Java Language Spec (3rd ed.):
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.6
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top