Saturday, 6 December 2008

Basics of HashMap

HashMap is an unsynchronized hashtable which permits null values. It extends the AbstractMap class, which is a partial implementation of the Map interface.

Methods inherited from Map include:

Object get(Object key)
Object put(Object key, Object value)
void putAll(Map t) - copy all mappings from t
Object remove(Object key) - returns previously associated value or null

To access the collection of keys and values in a Map, the interface declares the following methods:

public Set keySet()
public Collection values()

To iterate through the keys of a HashMap you can do:

final Iterator mapIterator = map.keySet.iterator();
while (mapIterator.hasNext()) {
final String key = (String) mapIterator.next();
final CustomType value = (CustomType) map.get(key);
}

HashMap provides constant-time performance for its basic operations (get and put) i.e. RETRIEVAL and INSERTION, assuming the hash function distributes the elements properly among the buckets.

Basics of gnu.getop

A Java port of GNU getopt based on C getopt() functions in glibc. Getopt partitions the inputs to the command-line program into optional and non-optional inputs.

import gnu.getopt.Getopt;

Getopt g = new Getopt("porridge", (String[]) args, "a:b:c:");
g.setOpterr(false); // we'll do our own error handling

while((c=getopt())!=-1) {
switch(c) {
case 'a': _alpha = g.getOptarg(); break;
case 'b': _beta = g.getOptarg(); break;
case 'c': _gamma = g.getOptarg(); break;
}

Once these options are processed, the next phase is to process non-optional arguments.
A summary of useful methods on Getopt object:

g.getopt() - returns the current option passed to the command-line
g.getOptind() - returns the NEXT index in ARGV of the next element to be scanned (non-optional argument)

Friday, 5 December 2008

IntelliJ Keyboard Shortcuts

IntelliJ is a very fast editor from jetbrains and does remote debugging more than twice as fast as Eclipse. Searching is also slicker than Eclipse. Here are some handy shortcuts for jet-setting across your source code.

Cntrl-N Jump to class
Cntrl-E Scroll through open files
Cntrl-F and F3 Search and repeat last search
Cntrl-F12 Jump through class (fields and methods)

Alt-F7 Find usages (also click "Open in new tab" to get tabbed search)
Alt-left, Alt-right Scroll through panes (Cntrl-E if you want to jump >1 step at a time)
Cntrl-B Jump to DECLARATION
Cntrl-Alt-B Jump to IMPLEMENTATION

Alt-3 go to Find screen

The Cntrl-MouseUp/MouseDown combination can be used to dynamically zoom in/out of the current pane. This is the same key combination in Internet Explorer for the same operation.

The founder of JetBrains is Sergey Dmitriev whose hobbies include pure mathematics, including functional analysis. here's an interview with him. Official bio is here.

Thursday, 4 December 2008

Basics of java.lang

rt.jar contains a lot of the core Java classes, such as those in java.lang, and java.net. When learning the core classes think of what each one is and what they are used for. Try and think of at least three examples in each case (use Google code search to help you).

Examples:

java.lang.Character (implements Serializable, Comparable). Character is a wrapper for a char. Useful e.g. when processing a character input from stdin or from gnu.getopt. Characters can be converted to strings using toString().

java.lang.System (extends Object). This fundamental class contains sytem-wide operations that act on the currently running virtual machine.

  • Exit the VM. exit(statusCode) terminates the VM. A value other than zero indicates abnormal termination
  • Bring in code from a shared library. static void load(String filename), static void loadLibrary(String libname)
  • Garbage collect. System.gc() prompts the VM to run the garbage collector.
  • Reference std input, stderr and stdout. This is done via the static PrintStream fields System.in, System.err and System.out
  • Restrict sensitive operations. This can be done by creating a SecurityManager and calling setSecurityManager on the System class.

java.lang.System is the moral equivalent of java.lang.Runtime; both classes facilitate interaction with the currently running VM. Many methods on System e.g. System.gc, can also be called on the Runtime class, e.g. Runtime.gc.

Coding Conventions - Lower Camel Case and Upper Camel Case

Java generally uses "lower camel case" (LCC) for method names. Example: br.readLine();
C# generally uses "upper camel case" (UCC) for method names. Example: sr.ReadlLine();

This is also reflected in the use of "main" in Java and "Main" in C#.

Strings are represented by "class String" in both Java and C#. The difference is in C# String is aliased to string which is more natural to use and similar to C++.

One of the earliest commercial examples of camel case is the 1950s CinemaScope film projection system.

Old School java.io

Java's BufferedReader extends the abstract Reader class, which reads character streams.

import java.io.BufferedReader;
import java.io.FileReader;

try {
BufferedReader br = new BufferedReader( new FileReader( someFile ) );
while ( br.ready() ) {
String line = br.readLine();
// processLine(line)
}

br.close();
} catch (Exception e) {}

read() will read one character, readline() will read a line. Don't forget to close the reader when you're done. The BufferedReader has two constructors, one that takes a "regular" Reader object, and the other that takes a Reader and the size of the buffer.

Tuesday, 2 December 2008

To sun.misc.Signal or not sun.misc.Signal?

In Java an application can install its own signal handler via sun.misc.Signal. This is deprecated in Java 1.6.

http://www.ibm.com/developerworks/java/library/i-signalhandling/