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


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

Re: Substitute value in HashMap at runtime

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Substitute value in HashMap at runtime
Date 2011-10-13 05:53 -0700
Organization http://groups.google.com
Message-ID <16756426.262.1318510428301.JavaMail.geo-discussion-forums@prgv7> (permalink)
References <70a582b4-956b-4145-9cb8-ba8fbaf751f5@b10g2000vbc.googlegroups.com>

Show all headers | View raw


mike wrote:
> If I create a HashMap with something like:
> 
> static Map<String,String> map = new
> HashMap<String,String>("variable",MyPreferences.getVariableValue());
> 
> If I do it like this I guess that MyPreferences.getVariableValue()
> will not be substituted but be the "plain" string.

Nope.  If you do it like that your code will fail to compile (assuming you're referring to 'java.util.HashMap').
<http://download.oracle.com/javase/7/docs/api/java/util/HashMap.html>

Put together a Simple Self-Contained Compilable Example (SSCCE) per 
http://sscce.org/

Seriously.  Do it.

Even if you use a correct constructor, if 'MyPreferences.getVariableValue()' is not of type 'String' you have a problem:

 public class Foo
 {
   static Map<String,String> map = new HashMap<>();
   static
   {
     map.put( "variable", MyPreferences.getVariableValue() );
   }
 }

The type of the entry must match the type of the target.

BTW, I assume that 'getVariableValue()' is a static member of 'MyPreferences', given that you named the latter as a type and not a variable.

> How can I make my MyPreferences.getVariableValue() be evaluated at
> runtime? Any example?

Use the expression 'MyPreferences.getVariableValue()'.

Let's see that SSCCE in your next post, otherwise there's not much point in continuing, is there?  We need full data to understand what you aim to accomplish, and you need full data for any answer to make any sense.

SSCCE.

http://sscce.org/

-- 
Lew

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


Thread

Substitute value in HashMap at runtime mike <mikaelpetterson@hotmail.com> - 2011-10-13 05:18 -0700
  Re: Substitute value in HashMap at runtime Lew <lewbloch@gmail.com> - 2011-10-13 05:53 -0700
  Re: Substitute value in HashMap at runtime Robert Klemme <shortcutter@googlemail.com> - 2011-10-13 06:44 -0700
    Re: Substitute value in HashMap at runtime Lew <lewbloch@gmail.com> - 2011-10-13 10:12 -0700
      Re: Substitute value in HashMap at runtime mike <mikaelpetterson@hotmail.com> - 2011-10-14 06:36 -0700
        Re: Substitute value in HashMap at runtime mike <mikaelpetterson@hotmail.com> - 2011-10-14 06:54 -0700
          Re: Substitute value in HashMap at runtime markspace <-@.> - 2011-10-14 08:13 -0700
  Re: Substitute value in HashMap at runtime Roedy Green <see_website@mindprod.com.invalid> - 2011-10-14 09:40 -0700

csiph-web