Java Hashtable Primer Tutorial

Java Hashtable Primer Tutorial

Java Hashtable Primer Tutorial

Some time ago (with Java XCode ArrayList Primer Tutorial as shown below) we used a (Java “collection”) data structure called ArrayList in Java via Xcode on a Mac laptop. Today we do this again (without the Xcode these days), but also use a Hashtable (Java “collection”) 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 collection to address that “age old” programming logic “sentence” …


hashtable["key"] = "value"

… 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.

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) Scanner class), but we also populate an ArrayList after the (populating) event for the reverse Enumeration part of the data, and this may interest you … look for …


for (Enumeration e = contactsHT.elements(); e.hasMoreElements();) {
sortingEmail.add(e.nextElement());
}

The compilation instructions are very simple for a Terminal command line session on a Mac laptop (but could equally apply for other platforms too) …

  • javac TestHashtableArraylist.java
  • java TestHashtableArraylist

…. though there is functionality to accept extra command line arguments to specify a name of interest that you want to find the email of during the normal execution of the program.

Here is some downloadable Java source code you could call TestHashtableArraylist.java and you can get a close up of the tutorial picture.

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.


Previous relevant Java XCode ArrayList Primer Tutorial is shown below.

Java XCode ArrayList Primer Tutorial

Java XCode ArrayList Primer Tutorial

Here is a tutorial that uses ArrayList structure in Java via XCode on a Mac laptop.

This program counts the occurrence of characters within the contents of a text file.

We have one input text file which we are going to read.
We use one ArrayList to contain characters of interest to find.
We use another ArrayList to contain character counts collected.

Breaking this down, it would be good to:

a) have method that reads the text file and does the report
b) have method to count occurrences of a character within a String
c) have method to count number of words within a String (and total characters in words)

This way you can report:

. occurrences of a character of interest within a line of the input text file
. total count and percentage of characters of interest within the whole text file
. total count and percentage of other characters within the whole text file
. total count of words within the whole text file
. average word size is ((total characters in words) / (total count of words))

So we’ll build up a FindOccurrences class this way.

Need to have rules about the input text file. Will have it that:

1) there is an optional first header record with number of records of interest
2) all records need {cr} (and/or {lf}) delimitation (as a text file should)

How do we obtain the filename? Will have it that:

(1) it can optionally be passed as a command line parameter
(2) if not we will prompt for it and allow a non-answer meaning it is inputfile.txt

Do we assume a and A (for instance) are the same thing? Will have it that:

(a) assume as a default a and A are to be treated as one entity for searching purposes
(b) there is a, albeit awkward, way to say that a and A are separate, via (2) above

Let’s see some Java programming code for this, and how it works …

Download source code and rename to FindOccurrences.java

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Operating System, Software, Tutorials and tagged , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

10 Responses to Java Hashtable Primer Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *