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

Discussion in 'Java' started by Hal Vaughan, Sep 28, 2006.

  1. Hal Vaughan

    Hal Vaughan Guest

    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
     
    Hal Vaughan, Sep 28, 2006
    #1
    1. Advertising

  2. Hal Vaughan

    Karl Uppiano Guest

    "Hal Vaughan" <> wrote in message
    news:...
    >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.
     
    Karl Uppiano, Sep 28, 2006
    #2
    1. Advertising

  3. Hal Vaughan schrieb:
    > 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
     
    Michael Rauscher, Sep 28, 2006
    #3
  4. "Hal Vaughan" <> wrote in message
    news:...
    >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");
     
    Mike Schilling, Sep 28, 2006
    #4
  5. Hal Vaughan

    Doug Pardee Guest

    Hal Vaughan wrote:
    > 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
     
    Doug Pardee, Sep 28, 2006
    #5
  6. Hal Vaughan

    ddimitrov Guest

    ddimitrov, Oct 1, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jaap de verwant slachter
    Replies:
    0
    Views:
    1,291
    jaap de verwant slachter
    Jul 1, 2003
  2. Roy in

    need step by step example

    Roy in, Aug 3, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    376
    Roy in
    Aug 3, 2003
  3. Vince Darley
    Replies:
    4
    Views:
    4,570
    emilchacko
    Mar 2, 2010
  4. Claudio Grondi
    Replies:
    2
    Views:
    379
    Claudio Grondi
    Jul 3, 2006
  5. Rakesh
    Replies:
    10
    Views:
    12,274
    Mike Schilling
    Apr 8, 2008
Loading...

Share This Page