<p>What is the most straightforward way to create a hash table (or associative array...) in Java?  My google-fu has turned up a couple examples, but is there a standard way to do this?</p>

<p>And is there a way to populate the table with a list of key->value pairs without individually calling an add method on the object for each pair?</p>

[{'answer_id': 29334, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': 2, 'selected': False, 'text': '<pre><code>import java.util.HashMap;\n\nMap map = new HashMap();\n</code></pre>\n'}, {'answer_id': 29336, 'author': 'Edmund Tay', 'author_id': 2633, 'author_profile': 'https://Stackoverflow.com/users/2633', 'pm_score': 6, 'selected': True, 'text': '<pre><code>Map map = new HashMap();\nHashtable ht = new Hashtable();\n</code></pre>\n\n<p>Both classes can be found from the java.util package. The difference between the 2 is explained in the following <a href="http://www.jguru.com/faq/view.jsp?EID=430247" rel="noreferrer">jGuru FAQ entry</a>.</p>\n'}, {'answer_id': 29342, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 1, 'selected': False, 'text': '<p>What <a href="https://stackoverflow.com/questions/29324/how-do-i-create-a-hash-table-in-java#29336">Edmund</a> said.</p>\n\n<p>As for not calling .add all the time, no, not idiomatically. There would be various hacks (storing it in an array and then looping) that you could do if you really wanted to, but I wouldn\'t recommend it.</p>\n'}, {'answer_id': 29356, 'author': 'Cem Catikkas', 'author_id': 3087, 'author_profile': 'https://Stackoverflow.com/users/3087', 'pm_score': 3, 'selected': False, 'text': '<p>Also don\'t forget that both Map and Hashtable are generic in Java 5 and up (as in any other class in the <a href="http://java.sun.com/javase/6/docs/technotes/guides/collections/index.html" rel="noreferrer">Collections framework</a>).</p>\n\n<pre><code>Map&lt;String, Integer&gt; numbers = new HashMap&lt;String, Integer&gt;();\nnumbers.put("one", 1);\nnumbers.put("two", 2);\nnumbers.put("three", 3);\n\nInteger one = numbers.get("one");\nAssert.assertEquals(1, one);\n</code></pre>\n'}, {'answer_id': 30413, 'author': 'Mocky', 'author_id': 3211, 'author_profile': 'https://Stackoverflow.com/users/3211', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>And is there a way to populate the table with a list of key->value pairs without individually calling an add method on the object for each pair?</p>\n</blockquote>\n\n<p>One problem with your question is that you don't mention what what form your data is in to begin with. If your list of pairs happened to be a list of Map.Entry objects it would be pretty easy.</p>\n\n<p>Just to throw this out, there is a (much maligned) class named java.util.Properties that is an extension of Hashtable. It expects only String keys and values and lets you load and store the data using files or streams. The format of the file it reads and writes is as follows:</p>\n\n<pre><code>key1=value1\nkey2=value2\n</code></pre>\n\n<p>I don't know if this is what you're looking for, but there are situations where this can be useful. </p>\n"}, {'answer_id': 31797, 'author': 'izb', 'author_id': 974, 'author_profile': 'https://Stackoverflow.com/users/974', 'pm_score': 5, 'selected': False, 'text': '<p>You can use double-braces to set up the data. You still call add, or put, but it\'s less ugly:</p>\n\n<pre><code>private static final Hashtable&lt;String,Integer&gt; MYHASH = new Hashtable&lt;String,Integer&gt;() {{\n    put("foo",      1);\n    put("bar",      256);\n    put("data",     3);\n    put("moredata", 27);\n    put("hello",    32);\n    put("world",    65536);\n }};\n</code></pre>\n'}, {'answer_id': 13080134, 'author': 'hedrick', 'author_id': 1146047, 'author_profile': 'https://Stackoverflow.com/users/1146047', 'pm_score': 0, 'selected': False, 'text': '<p>It is important to note that Java\'s hash function is less than optimal. If you want less collisions and almost complete elimination of re-hashing at ~50% capacity, I\'d use a Buz Hash algorithm <a href="http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200101/homework10/hashfuncs.html" rel="nofollow">Buz Hash</a></p>\n\n<p>The reason Java\'s hashing algorithm is weak is most evident in how it hashes Strings.</p>\n\n<p><code>"a".hash()</code> give you the ASCII representation of <code>"a"</code> - <code>97</code>, so <code>"b"</code> would be <code>98</code>. The whole point of hashing is to assign an arbitrary and "as random as possible" number.</p>\n\n<p>If you need a quick and dirty hash table, by all means, use <code>java.util</code>.  If you are looking for something robust that is more scalable, I\'d look into implementing your own.</p>\n'}, {'answer_id': 55966592, 'author': 'Juraj Valkučák', 'author_id': 11446430, 'author_profile': 'https://Stackoverflow.com/users/11446430', 'pm_score': -1, 'selected': False, 'text': '<pre><code>Hashtable&lt;Object, Double&gt; hashTable = new Hashtable&lt;&gt;();\n</code></pre>\n\n<p><strong>put values</strong>\n...</p>\n\n<p><strong>get max</strong></p>\n\n<pre><code>Optional&lt;Double&gt; optionalMax = hashTable.values().stream().max(Comparator.naturalOrder());\n\nif (optionalMax.isPresent())\n System.out.println(optionalMax.get());\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/29324', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/145/']