Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #8761
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Substitute value in HashMap at runtime |
| Date | 2011-10-13 06:44 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <c2bb16de-3ffc-4701-9aa0-1f8f5451908a@f11g2000yqf.googlegroups.com> (permalink) |
| References | <70a582b4-956b-4145-9cb8-ba8fbaf751f5@b10g2000vbc.googlegroups.com> |
On Oct 13, 2:18 pm, mike <mikaelpetter...@hotmail.com> 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.
>
> How can I make my MyPreferences.getVariableValue() be evaluated at
> runtime? Any example?
One possible way is to change your Map to Map<String,
Callable<String>> and invoke call() at runtime.
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Callable.html
Of course you then need to provide a proper implementation. :-)
Btw, does your Map contain more entries? If not, it's completely
superfluous.
If you define the Map as static you also need to be aware of
concurrency issues if your application will ever access this from
multiple threads.
Cheers
robert
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
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