{"id":11272,"date":"2014-12-28T05:02:25","date_gmt":"2014-12-27T18:02:25","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=11272"},"modified":"2016-03-04T22:52:45","modified_gmt":"2016-03-04T12:52:45","slug":"java-hashtable-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/java-hashtable-primer-tutorial\/","title":{"rendered":"Java Hashtable Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Java\/Hashtable\/HashtableArraylist.jpg\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Java Hashtable Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Java\/Hashtable\/HashtableArraylist.jpg\" title=\"Java Hashtable Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Java Hashtable Primer Tutorial<\/p><\/div>\n<p>Some time ago (with <a target=_blank href=\"#jxapt\" title=\"Java XCode ArrayList Primer Tutorial\">Java XCode ArrayList Primer Tutorial<\/a> as shown below) we used a (Java &#8220;collection&#8221;) data structure called <a target=_blank title='Dynamic array information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Dynamic_array'>ArrayList<\/a> in Java via Xcode on a Mac laptop.  Today we do this again (without the Xcode these days), but also use a <a target=_blank title='Hash table information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Hash_table'>Hashtable<\/a> (Java &#8220;collection&#8221;) data structure, to create a simple Contact List program.   Are you familiar with Hashtables?  They can simulate, in a lot of ways, what a relational database can do for you to use a Java <a target=_blank title='Java collections' href='http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Collections.html'>collection<\/a> to address that &#8220;age old&#8221; programming logic &#8220;sentence&#8221; &#8230; <\/p>\n<p><code><br \/>\n<i>hashtable[\"key\"] = \"value\"<\/i><br \/>\n<\/code><\/p>\n<p>&#8230; do you see how like a database (table design) that statement is?  And also how like the way a lot of us think, that is?  Also close in thought to this are associative arrays.<\/p>\n<p>The combination of ArrayLists and Hashtables add many possibilities for sorting ideas for our Contact List (Java) program we develop (and compile) today.  The population of the ArrayList can sometimes proceed at the same time as the Hashtable, as we (mostly) do with our code today (making use of the interactive (console) <a target=_blank title='Java Scanner class' href='http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Scanner.html'>Scanner<\/a> class), but we also populate an ArrayList after the (populating) event for the reverse Enumeration part of the data, and this may interest you &#8230; look for &#8230;<\/p>\n<p><code><br \/>\n  for (Enumeration<string> e = contactsHT.elements(); e.hasMoreElements();) {<br \/>\n   sortingEmail.add(e.nextElement());<br \/>\n  }<br \/>\n<\/code><\/p>\n<p>The compilation instructions are very simple for a Terminal command line session on a Mac laptop (but could equally apply for other platforms too) &#8230;<\/p>\n<ul>\n<li>javac TestHashtableArraylist.java<\/li>\n<li>java TestHashtableArraylist<\/li>\n<\/ul>\n<p>&#8230;. though there is <a target=_blank title='Java command line argument ideas' href='http:\/\/docs.oracle.com\/javase\/tutorial\/essential\/environment\/cmdLineArgs.html'>functionality<\/a> to accept extra command line arguments to specify a <i>name<\/i> of interest that you want to find the <i>email<\/i> of during the normal execution of the program.<\/p>\n<p>Here is some downloadable Java source code you could call <a target=_blank title=\"TestHashtableArraylist.java\" href=\"http:\/\/www.rjmprogramming.com.au\/Java\/Hashtable\/TestHashtableArraylist.java_GETME\">TestHashtableArraylist.java<\/a> and you can get a close up of the tutorial <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Java\/Hashtable\/HashtableArraylist.jpg\" title=\"Click picture\">picture<\/a>.<\/p>\n<p>Hope you get some ideas out of Hashtables and ArrayLists and the combination of the two, perhaps, in Java, today, with our tutorial.   Hashtable and ArrayList concepts also exist in languages like VB.Net and C#.  Hope to see you again soon.<\/p>\n<hr \/>\n<p id=\"jxapt\">Previous relevant <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=720\" title=\"Java XCode ArrayList Primer Tutorial\">Java XCode ArrayList Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Java\/JavaArrayList\/\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Java XCode ArrayList Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Java\/JavaArrayList\/JavaTool_XCode_Find_Occurrences-0of.jpg\" title=\"Java XCode ArrayList Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Java XCode ArrayList Primer Tutorial<\/p><\/div>\n<p>Here is a tutorial that uses ArrayList structure in Java via XCode on a Mac laptop.<\/p>\n<p>This program counts the occurrence of characters within the contents of a text file.<\/p>\n<p>We have one input text file which we are going to read.<br \/>\nWe use one ArrayList<string> to contain characters of interest to find.<br \/>\nWe use another ArrayList<integer> to contain character counts collected.<\/p>\n<p>Breaking this down, it would be good to:<\/p>\n<p>a) have method that reads the text file and does the report<br \/>\nb) have method to count occurrences of a character within a String<br \/>\nc) have method to count number of words within a String (and total characters in words)<\/p>\n<p>This way you can report:<\/p>\n<p>. occurrences of a character of interest within a line of the input text file<br \/>\n. total count and percentage of characters of interest within the whole text file<br \/>\n. total count and percentage of other characters within the whole text file<br \/>\n. total count of words within the whole text file<br \/>\n. average word size is ((total characters in words) \/ (total count of words))<\/p>\n<p>So we&#8217;ll build up a FindOccurrences class this way.<\/p>\n<p>Need to have rules about the input text file.   Will have it that:<\/p>\n<p>1) there is an optional first header record with number of records of interest<br \/>\n2) all records need {cr} (and\/or {lf}) delimitation (as a text file should)<\/p>\n<p>How do we obtain the filename?  Will have it that:<\/p>\n<p>(1) it can optionally be passed as a command line parameter<br \/>\n(2) if not we will prompt for it and allow a non-answer meaning it is inputfile.txt<\/p>\n<p>Do we assume a and A (for instance) are the same thing?   Will have it that:<\/p>\n<p>(a) assume as a default a and A are to be treated as one entity for searching purposes<br \/>\n(b) there is a, albeit awkward, way to say that a and A are separate, via (2) above<\/p>\n<p>Let&#8217;s see some  <a target=_blank title='click picture' href='http:\/\/www.rjmprogramming.com.au\/Java\/JavaArrayList\/'>Java<\/a> programming code for this, and how it works &#8230;\n<\/p>\n<p><a target=_blank href='http:\/\/www.rjmprogramming.com.au\/Java\/JavaArrayList\/FindOccurrences.java_GETME'>Download source code and rename to FindOccurrences.java<\/a><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d780' onclick='var dv=document.getElementById(\"d780\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=ArrayList\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d780' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d11272' onclick='var dv=document.getElementById(\"d11272\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Hashtable\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d11272' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago (with Java XCode ArrayList Primer Tutorial as shown below) we used a (Java &#8220;collection&#8221;) data structure called ArrayList in Java via Xcode on a Mac laptop. Today we do this again (without the Xcode these days), but &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/java-hashtable-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,33,37],"tags":[104,1819,223,234,253,255,380,391,556,644,672,723,885,997,1044,1097,1252,1319,1360],"class_list":["post-11272","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-software","category-tutorials","tag-arraylist","tag-associative-array","tag-collection","tag-command-line","tag-console","tag-contact-list","tag-email","tag-enumeration","tag-hashtable","tag-java","tag-key","tag-mac","tag-operating-system-2","tag-programming","tag-relational-database","tag-scanner","tag-terminal","tag-tutorial","tag-value"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/11272"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=11272"}],"version-history":[{"count":1,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/11272\/revisions"}],"predecessor-version":[{"id":20494,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/11272\/revisions\/20494"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=11272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=11272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=11272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}