tag:blogger.com,1999:blog-3102468434043486639.post6344282528208873708..comments2024-03-17T23:30:49.469-07:00Comments on Roy's musings: Creating a HashMap with entriesRoyhttp://www.blogger.com/profile/00633074227808113222noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-3102468434043486639.post-19361855282439562022016-12-11T22:30:33.956-08:002016-12-11T22:30:33.956-08:00The regarded aspects and ideas as mentioned would ...The regarded aspects and ideas as mentioned would help students around all those prospects and probabilities which must have been followed by the individual. newsletter plugin wordpresshttp://plugins.wpservice.org/how-to-choose-good-newsletter-wordpress-plugin/noreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-34360178313336891952011-02-02T05:12:17.222-08:002011-02-02T05:12:17.222-08:00Hi,
Thanks for this Nice artilce just to add while...Hi,<br />Thanks for this Nice artilce just to add while discussing about HashMap its worth mentioning following questions which frequently asked in Java interviews now days like <a href="http://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html" rel="nofollow">How HashMap works in Java</a> or <a href="http://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html" rel="nofollow">How get() method of HashMap works in JAVA </a> very often. on concept point of view these questions are great and expose the candidate if doesn't know deep details.<br /><br />Javin<br /><a href="http://javarevisited.blogspot.com/search/label/FIX%20protocol%20tutorial" title="Fix Protocol tutorial" rel="nofollow">FIX Protocol tutorial</a>Javin @ Tibco RV Tutorialhttp://javarevisited.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-12599708683214203672010-10-18T04:44:14.219-07:002010-10-18T04:44:14.219-07:00Hello Roy,
You can also use commons-lang3 ArrayUti...Hello Roy,<br />You can also use commons-lang3 ArrayUtils toMap function to create a Map<br />Map map = ArrayUtils.toMap(new Object[][]{{"shekhar",1},{"rahul",2}});<br /><br />But the problem is Map does not contain type information.It is a map of ObjectsShekhar Gulatihttps://www.blogger.com/profile/05894153169191332725noreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-1803195945735326992010-10-18T03:20:54.678-07:002010-10-18T03:20:54.678-07:00/* Sorry for all the edits */
final private static.../* Sorry for all the edits */<br />final private static Map SERVICE_URLS = Collections.unmodifiableMap(new HashMap() {{<br /> try {<br /> put("google", new URL("http://www.google.com"));<br /> put("yahoo", new URL("http://www.yahoo.com"));<br /> } catch (MalformedURLException ex) {<br /> log.log(Level.ERROR, "", ex);<br /> }<br /> }});Fernandohttps://www.blogger.com/profile/11487021651162128013noreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-80574312321023007272010-10-18T03:17:05.622-07:002010-10-18T03:17:05.622-07:00This comment has been removed by the author.Fernandohttps://www.blogger.com/profile/11487021651162128013noreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-31504487320788184042010-10-18T03:15:56.734-07:002010-10-18T03:15:56.734-07:00This comment has been removed by the author.Fernandohttps://www.blogger.com/profile/11487021651162128013noreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-61164445140373070862010-10-18T03:09:05.538-07:002010-10-18T03:09:05.538-07:00We used a simular approach.
Except, with an extra ...We used a simular approach.<br />Except, with an extra factory method, the type checks and illegalargumentexception can be removed:<br /><br />public class MyClass {<br /><br /> public static <K, V> Map<K, V> newHashMapWithEntries(Map.Entry<K, V>... entries) {<br /> Map<K, V> map = new HashMap<K, V>();<br /> if (entries != null) {<br /> for (Entry<K, V> entry : entries) {<br /> map.put(entry.getKey(), entry.getValue());<br /> }<br /> }<br /> return map;<br /> }<br /><br /> public static <K, V> Map.Entry<K, V> entry(K k, V v) {<br /> return new AbstractMap.SimpleEntry<K, V>(k, v);<br /> }<br /><br /> public static void main(String[] args) throws Exception {<br /> Map<String, URL> serviceUrls = <br /> newHashMapWithEntries(<br /> entry("google", new URL("http://www.google.com")),<br /> entry("yahoo", new URL("http://www.yahoo.com"))<br /> );<br /> }<br /><br />}Unknownhttps://www.blogger.com/profile/17691463871825167453noreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-54032821127991893982010-10-18T02:33:31.400-07:002010-10-18T02:33:31.400-07:00hm, what about:
ImmutableMap map = ImmutableMap.o...hm, what about: <br />ImmutableMap map = ImmutableMap.of("key", "val", "key1", "val2");m.milicevichttp://www.onehippo.comnoreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-85862711167151285562010-10-18T02:23:33.460-07:002010-10-18T02:23:33.460-07:00You can also try with Scala, I know, it's not ...You can also try with Scala, I know, it's not Java, but you can invoke Scala code within Java code.<br /><br />In Scala you can create Maps this way:<br /><br />var steps = Map (1 -> "Go straight on", 2 -> "Take the second turn on the left", 3 -> "It's opposite the hairdresser")Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-4043721781841673692010-10-18T02:20:48.292-07:002010-10-18T02:20:48.292-07:00Maybe I'm missing the point.
Why not doing it...Maybe I'm missing the point.<br /><br />Why not doing it in this way:<br /> private static final Map serviceUrls = new HashMap();<br /> static {<br /> serviceUrls.put("google", new URL("http://www.google.com"));<br /> serviceUrls.put("yahoo", new URL("http://www.yahoo.com")); <br /> }<br /><br />(The MalformedURLException must be catched)CChttps://www.blogger.com/profile/00783116132513246955noreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-74829050444297921152010-10-17T19:34:43.387-07:002010-10-17T19:34:43.387-07:00M.Milicevic: We can use the Google Collections (Gu...M.Milicevic: We can use the Google Collections (Guava) as well. Thank you for your suggestion. The one that I had given was a little less verbose than the version using Guava. In case someone is not already using Guava, this implementation would help.Royhttps://www.blogger.com/profile/00633074227808113222noreply@blogger.comtag:blogger.com,1999:blog-3102468434043486639.post-73810653445189107042010-10-17T05:01:59.771-07:002010-10-17T05:01:59.771-07:00Hi Roy,
why not use google collections library, e....Hi Roy,<br />why not use google collections library, e.g: <br />ImmutableMap map = new ImmutableMap.Builder()<br /> .put("one", 1)<br /> .put("two", 2)<br /> .build();<br /><br />cheersm.milicevichttp://www.onehippo.comnoreply@blogger.com