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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!198.186.194.250.MISMATCH!news-out.readnews.com!news-xxxfer.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Substitute value in HashMap at runtime
Date Thu, 13 Oct 2011 05:53:48 -0700 (PDT)
Organization http://groups.google.com
Lines 45
Message-ID <16756426.262.1318510428301.JavaMail.geo-discussion-forums@prgv7> (permalink)
References <70a582b4-956b-4145-9cb8-ba8fbaf751f5@b10g2000vbc.googlegroups.com>
Reply-To comp.lang.java.programmer@googlegroups.com
NNTP-Posting-Host 173.164.137.214
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1318510547 19415 127.0.0.1 (13 Oct 2011 12:55:47 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Thu, 13 Oct 2011 12:55:47 +0000 (UTC)
In-Reply-To <70a582b4-956b-4145-9cb8-ba8fbaf751f5@b10g2000vbc.googlegroups.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8759

Show key headers only | 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