After using the ArrayList mentioned in my last post, I thought to see what the HashMap is all about. Once defined, the HashMap takes a "key" and a "value". In the code snippet below, the "1" is the key and the "JAN" is the value.
These are used within database code where within a table, there will be a key in one column and a value in the other column. Not being into extreme database programming that would require the use of a key/value, thought it would be useful to at least understand how to code these.
ArrayFunction1() was coded to put the key/value into myHashMap.
ArrayFunction2() was coded to extract various items from myHashMap.
If this looks familiar, it is a direct copy of the ArrayList() in the previous post as well. Found it very convenient were all that was needed was a search/replace operation.
public static HashMap <Integer, String> ArrayFunction1 () {
HashMap<Integer, String> myHashMap = new HashMap<Integer, String>();
Logger("ArrayFunction1: Entry");
...
myHashMap.put(1,"JAN");
myHashMap.put(2,"FEB");
myHashMap.put(3,"MAR");
myHashMap.put(4,"APR");
...
Logger("ArrayFunction1: Exit");
}
public static void ArrayFunction2 (HashMap<Integer, String> myinputarray) {
Logger("ArrayFunction2: Entry");
...
Logger("ArrayFunction2: keySet: " + myinputarray.keySet());
Logger("ArrayFunction2: entrySet: " + myinputarray.entrySet());
Logger("ArrayFunction2: values: " + myinputarray.values());
...
Logger("ArrayFunction2: Exit");
}
And this is the output. It shows the keySet(), and entrySet() and the values() that are in the HashMap. I left out some of the boolean methods (containsValue()), containsKey()). They work pretty cool, showing true or false if the item is present in either the key or value.
ArrayFunction2: keySet: [1, 2, 3, 4]
ArrayFunction2: entrySet: [1=JAN, 2=FEB, 3=MAR, 4=APR]
ArrayFunction2: values: [JAN, FEB, MAR, APR]
Wondering what that Logger() function is? It is that method shown in the previous post that accepts a String as the only argument and simply outputs the input string to stdout. In otherwords, Logger() is a just wrapper to System.out.println().
This blog covers hiking, biking, geocaching, kitchen remodel, some programming (no tips), and work.
Sunday, January 19, 2014
Saturday, January 4, 2014
What to look for when code reviewing java source
Troubleshooting Java compile warnings:
As I continue to work with Java, from time to time my source code, when compiled returns the following messages to stdout. This is a fairly common message as I maintain certain source files at work. Here is an example of the particular message.
Note: ArrayListTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Sometimes a programmer will inherit a Java project from another person who in
turn may have got that project from someone else. So we have a hand-me-down
Java source file that when compiled, returns those two notes mentioned above.
The Java compiler is nice enough to tell you that there is a situation there
and to recompile with the -Xlint. The -Xlint will output all the warnings to
stdout. There could be times where it would not be practical to actually run
the -Xlint option. Checking the ant build script may offer a means to use that
-Xlint as a compile option.
Using the -Xlint will be helpful in finding the offending line of Java source
code. For an example, here is a defined array list both the correct way
(commented) and the way that causes those two above compilation notes.
// create an array list
//ArrayList <String> al = new ArrayList<String>();
ArrayList al = new ArrayList();
So when it comes down to it, the coder should keep in mind when inheriting a
project that is a hand-me-down, they may need to keep things in mind like
ensuring data types are declared in such a way so it keeps the compiler happy. Either way the program will run, it is just a really good practice to ensure source compiles clean.
As for me, as I go through this one source file at work, perhaps a few hundred lines or so, I'll know what I'm suppose to be looking for in order to clean up these messages. Maybe I'll be lucky enough and find where in the ant script to insert this compiler option.
As I continue to work with Java, from time to time my source code, when compiled returns the following messages to stdout. This is a fairly common message as I maintain certain source files at work. Here is an example of the particular message.
Note: ArrayListTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Sometimes a programmer will inherit a Java project from another person who in
turn may have got that project from someone else. So we have a hand-me-down
Java source file that when compiled, returns those two notes mentioned above.
The Java compiler is nice enough to tell you that there is a situation there
and to recompile with the -Xlint. The -Xlint will output all the warnings to
stdout. There could be times where it would not be practical to actually run
the -Xlint option. Checking the ant build script may offer a means to use that
-Xlint as a compile option.
Using the -Xlint will be helpful in finding the offending line of Java source
code. For an example, here is a defined array list both the correct way
(commented) and the way that causes those two above compilation notes.
// create an array list
//ArrayList <String> al = new ArrayList<String>();
ArrayList al = new ArrayList();
So when it comes down to it, the coder should keep in mind when inheriting a
project that is a hand-me-down, they may need to keep things in mind like
ensuring data types are declared in such a way so it keeps the compiler happy. Either way the program will run, it is just a really good practice to ensure source compiles clean.
As for me, as I go through this one source file at work, perhaps a few hundred lines or so, I'll know what I'm suppose to be looking for in order to clean up these messages. Maybe I'll be lucky enough and find where in the ant script to insert this compiler option.
Subscribe to:
Posts (Atom)