Choose the Right Clock on Android

Android framework provides three different time keeping facilities. This post discusses the main differences and usage of them.

System.currentTimeMillis

It returns the number of milliseconds elapsed since the epoch, which is 00:00:00 January 1, 1970 UTC.

Note that this clock is not guaranteed to be monotonic, because of the followings:

  • The clock can be changed by network time sync
  • The clock can be updated by user or program

The clock can jump backwards or forwards unpredictably. We can listen for ACTION_TIME_TICK,ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.

When to use

  • Get the wall clock time
  • Inerval/elapsed time that needed to span across device reboot. This is not ideal, but a cheap and OK solution sometimes. Ideally we want to have a server provided timestamp to avoid all the unpredictably jump can happen to this clock.

When not to use

  • Interval or elapsed timing doesn’t need to span across device reboot. E.g.: memory cache expiration time

SystemClock.uptimeMillis

It returns the number of milliseconds since device boot. The clock stops when system enters deep sleep. The clock is guaranteed to be monotonic.

This clock is the basis for Thread.sleep(millls), Object.wait(millis), and System.nanoTime().

When to use

  • Interval or elapsed timing doesn’t need to span across device reboot, and we want to exclude device deep sleep

SystemClock.elapsedRealtime

It returns the number of milliseconds since device boot. The clock don’t stop even when system enters deep sleep. It’s also guaranteed to be monotonic.

elapsedRealtimeNanos(): Similar to method above, but returns number of nano seconds.

When to use

  • Interval or elapsed timing doesn’t need to span across device reboot, and we want to include device deep sleep

Caveats

1. When using System.nanoTime or elapsedRealtimeNanos, differences span greater than approximately 292 years (263 nanoseconds) will not be computed correctly due to numerical overflow.

2. When comparing two timestamps, we should use t2 – t1 > 0 instead of t2 > t1, because of possible numerical overflow.

In order to help us understand why t2 – t1 is better than t2 > t1 in case of numerical overflow, let’s assume that t1 and t2 are bytes within the range of [-128, 127].
When t1 = 126, t2 = t1 + 3 will result in numerical overflow, and t2 = -127. If we use t2 > t1, it will return false. But t2 – t1 = -127 – 126, which will again result in numerical overflow, and the result is 3.

This only works when the difference of t2 and t1 itself can be represented by the data type. In the byte example above, t2 – t1 must be less than or equal to 127.

Ghost Object Causing Crash in Java

A Bad Code Style

Java’s GC has made our life easier, but it also makes things more implicit and sometimes easier to make mistakes. Recently I encountered a bad code style which is easy to introduce bugs.

Imagining you have a class with a reference field and a method. The method creates a new object and assign it to the reference. Every time you call the method, a new object is created and assigned to the reference. The code is essentially like below,


class A {
    SomeClass B;
    void initB() {
         B = new SomeClass();
         …… do something with B … …
    }
}

Just call initB many times, this pattern creates a lot of objects without referencing to them. Since we’re not referencing to them, GC will do us the favor of garbage collecting them. However, things can easily go wrong.

When we do something with B after creation of the object, the object can be referred at somewhere else (e.g.: B is a task, and do something adds it to a system task queue). Now we call initB many times, we’ll end up with lots of objects somewhere and only one referred by B. We may not have control over the non-referenced objects any more. This results in garbage.

Things can go worse. If SomeClass defines some callback method, which is triggered later by some signal. We may ends up having callback method from many instances of SomeClass.

An Example in Android

Below is an example in Android.


package com.example.androidtest;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {

  private static final String TAG = "MainActivity";
  
  private Handler handler;
  private Runnable runnable;
  private Integer value;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    value = 10;
    startRunnable();
    startRunnable();
    removeRunnable();
  }
  
  private void startRunnable() {
    handler = new Handler();
    runnable = new Runnable() {
      @Override
      public void run() {
        Log.d(TAG, value.toString());
      }
    }; 
    handler.postDelayed(runnable, 2000);
  }
  
  private void removeRunnable() {
    handler.removeCallbacks(runnable);
    value = null;
  }
  
}

 

The startRunnable method creates a new Handler and Runnable objects, assigns them to the class reference field. We called this method twice, leaving a Handler and Runnable unreferenced by us.

However, these objects are still referenced by some Android system components. The postDelayed method essentially adds the runnable to a message queue associated with the current thread (The actual details are more complicated than this but you get the idea).

We then call removeRunnable to remove the runnable associated with handler, and assign null to value.

However, this doesn’t remove the ghost runnable associated with the ghost handler. And the run() method of the ghost runnable will be triggered, which will cause a crash when it tries to access value.toString().

The Fix

This coding style is bad and easy to introduce bugs. We essentially only want a single instance of Handler and Runnable at any time. If we have to create a new instance, we should ensure the old one is discarded properly, and leaving them referenced by system components is not.

If the system provides some callback functions that are guaranteed to be called once and we can create the objects inside those functions.

If we need to create objects inside some random methods, we can either reuse the old object, or properly reset the old object before creating new one. The code below shows how to do that to fix the example above.


package com.example.androidtest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class MainActivityNew extends Activity {

  private static final String TAG = "MainActivityNew";
  
  private Handler handler;
  private Runnable runnable;
  private Integer value;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    value = 10;
    startRunnable();
    startRunnable();
    removeRunnable();
  }
  
  private void startRunnable() {
    if (handler == null) {
      handler = new Handler();
    } else {
      handler.removeCallbacks(runnable);
    }
    runnable = new Runnable() {
      @Override
      public void run() {
        Log.d(TAG, value.toString());
      }
    }; 
    handler.postDelayed(runnable, 2000);
  }
  
  private void removeRunnable() {
    handler.removeCallbacks(runnable);
    value = null;
  }
  
}

 

In startRunnable method, if a Handler instance is available, we’ll just reuse it; If there’re potentially old Runnable associated with handler, we’ll remove them.

Dumping Python Pickle Files from Java

Pickle is a powerful serializing and deserializing mechanism supported by Python. With Jython jar file, we can load and dump Python pickle files from Java. Loading pickle file from Java has already been covered in a previous post. This post will discuss how to dump data to a pickle file in Java so a python script can deserialize it.

0. Download Jython jar file and the sources.jar file from Jython website.  Suppose we are using Eclipse IDE, add the downloaded Jython jar file as a library. We can also set the sources.jar as the attached source for the jar library for easier debugging.

1. Dump the data to a pickle file ids.pkl. The following Java from demonstrates how to put the data in a Java HashSet to a PyList and dump it.

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.HashSet;

 

import org.python.core.PyFile;

import org.python.core.PyList;

import org.python.core.PyString;

import org.python.modules.cPickle;

 

 

 

public class DumpPickle {

    public static void main(String[] args) {

        HashSet<String> appIds = new HashSet<String>();

        appIds.add("1234321432");

        appIds.add("1234321433");

        appIds.add("xsydfsflkfds");

        DumpPickle afpd = new DumpPickle();

        afpd.dumpHashsetToPickle(appIds);

    }

    

    public void dumpHashsetToPickle(HashSet<String> pIds) {

        File f = new File("ids.pkl");

        OutputStream fs = null;

        try {

            fs = new FileOutputStream(f);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

            return;

        }

        PyFile pyF = new PyFile(fs);

        PyList pyIdList = new PyList();

        for (String id : pIds) {

            PyString pyStr = new PyString(id);

            pyIdList.add(pyStr);

        }

        cPickle.dump(pyIdList, pyF);

        pyF.flush();

        pyF.close();

    }

 

}

Note that we will need to convert the Java data structure to Jython data structure before the dump operation.

2. Load the data from the pickle file ids.pkl. Below is a simple Python script that deserializes the data from ids.pkl to a list.

#!/usr/bin/python

import pickle

ids = []

ids = pickle.load( open( "ids.pkl", "r" ) )

for i in range(len(ids)):

    print ids[i]

3. Sample Execution

We can run the Java program first to produce the ids.pkl file, and then run the python script, which should print out the following.

1234321433
xsydfsflkfds
1234321432

As expected, we can get the list from the ids.pkl file.

Side note: the Jython jar file used for testing is jython-2.5.3.jar.

References:

Jython website: http://www.jython.org/

How Java Garbage Collector Work–A Simple Note

Recently I was asked about how Java Garbage Collector works. I remember I learned about it a few years ago, but seldom need to consider it in my development. (I simply follow the rules of set the reference to null when I’m done with the object.)  So I didn’t figure it out.

Today I spent an hour reading about Java GC and below is a note about it. It’s only the most basic stuff of GC.

————————————Notes Start Here——————————-

A garbage collector does two basic things, detect garbage objects and free the memory space taken by those objects.

Garbage detection is normally done by defining a set of roots and test if objects are reachable from these roots. Objects are not reachable from the roots are considered no longer in use and therefore should be garbage collected.

The Roots

The root set is implementation dependent but it generally includes the following.

  • Local variables, can be either primitive types or object reference. Local variables are inside methods, including method arguments, and they are stored in Java stack. (Note that instance variables are inside classes, not methods)
  • Object references in the constant pool of loaded classes. Constant pool can refer to strings stored on Java heap, including class name, method signatures, field names etc.

Rules

  • Any object referred by a root is reachable, and considered a live object
  • Any object referred by a live object is considered live

Approaches

  • Reference Counting: The garbage collector tracks the number of references to every object. A object is live as long as long there is references to it. The disadvantages of this approach is that it cannot detect cycles. If two or more objects refer to one another but not accessible by the program, the GC cannot garbage collect  these objects because they still have a reference count bigger than 0. Another advantage is the overhead of updating the counters for each objects.
  • Tracing: Trace out graphs of references from the roots and mark the objects along the graph paths. Any objects not marked are considered not live any more. Most JVM nowadays use tracing for garbage collection.

References:
Java’s garbage-collected heap: http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html?page=1

Loading Python Pickle Files from Java

Recently I wrote some program in Python, which dumps data into a pickle file. The file data is loaded into memory by another Python program for further processing. This works pretty well until I need to write a Java program which also loads the data saved in the pickle file.

I searched around but cannot find many resources about loading pickle files from Java. A few forum discussions mentioned calling Python script from Java using Jython interpreter. I digged deeper into the Jython implementation and found that we can actually load pickle files in Java with the help of Jython jar library.

Below we describe the steps of how to load pickle files from Java code.

0. Download Jython jar file and the sources.jar file from Jython website.  Suppose we are using Eclipse IDE, add the downloaded Jython jar file as a library. We can also set the sources.jar as the attached source for the jar library for easier debugging.

1. Create a Python pickle (or cpickle) File. Below is a simple Python script that dump a dictionary of lists to a pickle file.

#!/usr/bin/python

#import cPickle as pickle    #use either cpickle or pickle

import pickle

idToCountries = {}

testa = []

testa.append("us")

testa.append("en")

idToCountries["12345"] = testa

testb=[]

testb.append("cn")

testb.append("th")

idToCountries["12346"] = testb

outf=open("test.pkl", "wb")

pickle.dump(idToCountries, outf)

Save the file as test.py, and execute the commands below to create the test.pkl pickle file.

chmod +x test.py

./test.py

2. Load the data from the pickle file test.pkl, and put the data into a HashMap of ArrayLists. This is demonstrated in the Java file below.

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.concurrent.ConcurrentMap;

import java.io.FileInputStream;

import java.io.InputStream;

 

import org.python.core.PyDictionary;

import org.python.core.PyFile;

import org.python.core.PyList;

import org.python.core.PyObject;

import org.python.core.PyString;

import org.python.modules.cPickle;

 

public class PickleLoader {

    

    public static void main(String[] args) {

        PickleLoader loader = new PickleLoader();

        HashMap<String, ArrayList<String>> loadCnt1 =  loader.getIdToCountriesStr();

        System.out.println(loadCnt1.toString());

        HashMap<String, ArrayList<String>> loadCnt2 =loader.getIdToCountriesFileStream();

        System.out.println(loadCnt2.toString());

    }

    

    public HashMap<String, ArrayList<String>> getIdToCountriesStr() {

        HashMap<String, ArrayList<String>> idToCountries = new HashMap<String, ArrayList<String>>();

        File f = new File("test.pkl");

        System.out.println(f.length());

        BufferedReader bufR;

        StringBuilder strBuilder = new StringBuilder();

        try {

            bufR = new BufferedReader(new FileReader(f));

            String aLine;

            while (null != (aLine = bufR.readLine())) {

                strBuilder.append(aLine).append("n");

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        PyString pyStr = new PyString(strBuilder.toString());

        PyDictionary idToCountriesObj =  (PyDictionary) cPickle.loads(pyStr);

        ConcurrentMap<PyObject, PyObject> aMap = idToCountriesObj.getMap();

        for (Map.Entry<PyObject, PyObject> entry : aMap.entrySet()) {

            String appId = entry.getKey().toString();

            PyList countryIdList = (PyList) entry.getValue();

            List<String> countryList = (List<String>) countryIdList.subList(0, countryIdList.size());

            ArrayList<String> countryArrList = new ArrayList<String>(countryList);

            idToCountries.put(appId, countryArrList);

        }

//        System.out.println(idToCountries.toString());

        return idToCountries;

    }

    

    public HashMap<String, ArrayList<String>> getIdToCountriesFileStream() {

        HashMap<String, ArrayList<String>> idToCountries = new HashMap<String, ArrayList<String>>();

        File f = new File("test.pkl");

        InputStream fs = null;

        try {

            fs = new FileInputStream(f);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

            return null;

        }

        PyFile pyStr = new PyFile(fs);

        PyDictionary idToCountriesObj =  (PyDictionary) cPickle.load(pyStr);

        ConcurrentMap<PyObject, PyObject> aMap = idToCountriesObj.getMap();

        for (Map.Entry<PyObject, PyObject> entry : aMap.entrySet()) {

            String appId = entry.getKey().toString();

            PyList countryIdList = (PyList) entry.getValue();

            List<String> countryList = (List<String>) countryIdList.subList(0, countryIdList.size());

            ArrayList<String> countryArrList = new ArrayList<String>(countryList);

            idToCountries.put(appId, countryArrList);

        }

//        System.out.println(idToCountries.toString());

        return idToCountries;

    }

}

The getIdToCountriesStr method reads all the data from the pickle file and pass it as a string to the Jython cPickle.loads method. The getIdToCountriesFileStream open the file as FileInputStream and pass it to Jython cPickle.load method. This is preferred method for large pickle files.

In addition to loading the file, we also need to perform data type conversion to convert the PyList to List and then ArrayList, and PyDictionary to HashMap.

3. Sample Execution

The execution of the Java program will give us the following output.

{12346=[cn, th], 12345=[us, en]}
{12346=[cn, th], 12345=[us, en]}

As expected, both methods can load data from pickle file and save them into a Java HashMap.

Side note: the Jython jar file used for testing is jython-2.5.3.jar.

References:

Jython website: http://www.jython.org/

Switch Between Main Classes in a Jar File

Many developers used to write a main function for every class that can be tested separately. When we export it as a jar file, we usually specify a main class for execution of the jar file. Running this jar file is straightforward,

$ java -jar test.jar

But from time to time, some bugs are found at the system and we want to test a specific class. In other words, we want to run the main function in a class that is not set as main class.

1. Export the Jar File

At Eclipse, when exporting a runnable jar file, we’re given option to choose the main class at Launch configuration and three options of how to handle the libraries, as shown below,

In my project, I have used a library org.*.* and my package is like study.*.*

  • The first option unpack the libraries. The content of the jar file will contain the following

  • The second option put the libraries (probably in jar file) into the jar file. The content is like below,

  • The third option export the library to a separate folder, my exported jar file is named as test4.jar, and the library folder is named test4_lib

Interested readers can examine the META-INF/MANIFEST.MF to see how the library path and main class path is declared.

I figured out how to switch main class for the first and third option, but not the second. If anyone figured out how it works, please leave a comment. Thank you in advance for that!

2. Switch the Main Classes

At export, we can only specify one main class at launch configuration, say Main0. Now suppose we have another main class called Main1, and we want to run it. Then we’ll need to specify the classpath and the full path to the class.

$ java -classpath test.jar study.test.Main1

Note that we can even execute multiple main classes simultaneously in a single jar. For example,

$ java -classpath test.jar study.test.Main0 && java -classpath test.jar study.test.Main1

This will spawn two JVM to run the two processes.

My Java Tools/Libraries List

From time to time, I discovered the features I spent lots of time implementing already done by others. I think it’s a good idea to keep a list of libraries I encountered and remember to use them in my next project. This is the Java version. I’ll add new items to the list from time to time.

Category 1. Utility Libraries

Apache Commons: http://commons.apache.org/
Apache commons is a collection of lots of reusable Java components. Some popular components include the following,

  • Apache Commons IO: a library of utilities to assist with developing IO functionality
  • Apache Commons NET: a library implements the client side of many basic Internet protocols.
  • Apache Commons Collections: a library built on the JDk collection classes to provide new interfaces and implementations. e.g. Buffer interface
  • Apache Commons DBUtils: a set of classes to make working with JDBC easier. It’s designed to be small, transparent and fast.
  • Apache Commons CLI: a library that helps one to parse the command line parameters. It also helps to print out message that explains the commands. Several commonly used formats are supported.
    Guava is a collection of libraries used in Google’s Java based projects. It includes libraries for collections, caching, primitives support, concurrency, common annotations, string processing, I/O, and so on.

ConcurrentLinkedHashMap: http://code.google.com/p/concurrentlinkedhashmap/

a high performance version of java.util.LinkedHashMap for use as a software cache.

Category 2: Logging

Logging helps debugging in many places. However, log statement increases code size and reduce speed.

Apache log4j: http://logging.apache.org/log4j/1.2/index.html

log4j is designed to toggle logging by editing a configuration file. It allows one to easily log to a file, an OutputStream, a java.io.Writer, a remote log4j server or remote Unix Syslog daemon and many others.

Category 3: Communication

JSON IN Java: http://www.json.org/java/index.html
JSON IN Java provides classes to easily process information in JSON format.

JSON Simple: http://code.google.com/p/json-simple/

json-simple is a simple Java toolkit for JSON. It has no dependency on external libraries and high performance.

MessagePackhttp://msgpack.org/

MessagePack is a library provide efficient object serialization. It lets you exchange data among multiple languages like JSON but it’s faster and smaller. It also has a remote procedure call (RPC) system on top of it called MessagePack-RPC. 

Apache HttpComponents: http://hc.apache.org/
Apache HttpComponents is a toolset of low level Java components focusing on HTTP and related protocols. It mainly includes HttpCore, HttpClient and Asynch HttpClient.

  • HttpCore: used to build custom client and server side HTTP services.
  • HttpClient: a HTTP/1.1 compliant HTTP client implementation based on HttpCore.
  • Async HttpClient: a HTTP/1.1 compliant HTTP implementation based on HttpCore NIO and HttpClient components.

RabbitMQ: http://www.rabbitmq.com/

RabbitMQ is a open source message broker software which implements the Advanced Message Queuing Protocol (AMQP) standard. It enables asynchronous read and write through messaging.

jsoup: Java HTML Parser: http://jsoup.org/

jsoup is a Java library for extracting and manipulating data in HTML.

HtmlUnit: http://htmlunit.sourceforge.net/

HtmlUnit models HTML documents and provides APIs that enables one to load pages, fill out forms, click links etc., in a way that a normal browser does. It is typically used for testing or retrieving information from websites.

JTidyhttp://jtidy.sourceforge.net/

JTidy is a Java port of HTML Tidy, a HTML syntax checker and pretty printer.  It can also be used to clean up malformed HTML.

Category 4: Database

JDBC: http://www.oracle.com/technetwork/java/javase/jdbc/index.html
Java Database Connectivity (JDBC) API is the industry standard for communication between Java programs and a wide range of SQL databases and other tabular data sources (spreadsheets). For MySQL, the JDBC driver is called Connector/J.

Connector/J: http://www.mysql.com/downloads/connector/j/
Connector/J is a standardized database driver for Java platforms.

Category 5: Big Data

Apache Cassandra: http://cassandra.apache.org/
Apache Cassandra is a column based distributed database. Developing applications to communicate with Cassandra is usually done through a high level client like Hector.

Hector: http://hector-client.github.com/hector/build/html/index.html
Hector is a high level Java client for Apache Cassandra. It is easier to develop with Hector rather than Cassandra’s Thrift API.

Category 6: Search

Apache Lucent: http://lucene.apache.org/core/
Apache Lucent is a high-performance, full-featured text search engine library written entirely in Java.

Java Programming Cheat Sheet 4 — Collections

This is the 4th part of the Java Programming Cheat Sheet. You can find the first three parts here:

1. OO Concepts

2. Java Syntax

3. Basic I/O

Collection: sometimes called a container, an object that groups multiple elements into a single unit. The Java collection framework contains the following,

  • interfaces: abstract data types that represent collections. it allows collections to be manipulated independently of the details of their representation.
  • implementations: concrete implementations of the collection interfaces.
  • algorithms: they operates on objects that implements the collection interfaces. The algorithms are polymorphic, in the sense that they can operate on different implementations of appropriate interface.

Interfaces: Java Collection Framework  has the following interfaces, note that a map is not a true collection.

Figure 1. Java Collection Interfaces Hierarchy

  • All the core collection interfaces are generic.
  • Collection: the root of the collection hierarchy. Java doesn’t provide any direct implementations of this interface.

Set: a collection with unique elements. It models the set concept in mathematics. Java contains three general purpose implementations for Set

  • HashSet: stores elements in a hash table. It has best performance, but offers no order of iteration.
  • TreeSet: stores elements in a red-black tree, a self-balancing binary search tree. The elements are ordered based on their values, but it performs much slower than HashSet.
  • LinkedHashSet: stores elements in a hash table with linked list running through it. The elements are ordered according to insertion order. The performance is slightly slower than HashSet, but much better than TreeSet.

List: ordered collection, may contain duplicate elements. It includes interfaces for following operations,

  • positional access: access elements based on their numerical position in the list
  • search: search for a specific element in the list and returns its numerical position
  • iteration: use iterator to go through the list
  • range view: performs arbitrary range operations on the list, e.g. sublist
  • List provides two general purpose implementations
    • ArrayList: implemented using a dynamic array. Just like array.
    • LinkedList: implemented using linked list. Just like linked list.

Queue: holds elements before processing. It provides insertion, removal and inspection operations. It has two general implementations

  • LinkedList: LinkedList implements both List and Queue. It provides a FIFO (first in, first out) queue operations.
  • PriorityQueue: implemented based on a heap. Orders elements according to the comparator specified at construction.

Map: an object that maps keys to values. It cannot contain duplicate keys, and each key can map to at most one value.

  • Java doesn’t provide interface for a multimap, where each key of a map can map to multiple values. However, one can use a map whose values are List instances to emulate a multimap.
  • Map has three general-purpose implementations, similar to Set
    • HashMap: no order is maintained
    • TreeMap: sorted according to key
    • LinkedHashMap: insertion order or key access order (look up the value associated with a key brings the key to the end of the map). It also has removeEldestEntry method, which may be overriden to impose a policy to remove stale mapping automatically when new mappings are added.

SortedSet: a set that maintains its elements in ascending order. It provides additional operations compared with a normal set,

  • range view: arbitrary range operations on the sorted map
  • endpoints: return the first or last key in the sorted map
  • comparator access: return the Comparator, if any, used to sort the map

SortedMap: a map that maintains its entries in ascending order. It provides additional operations
compared with a normal map,

  • range view: arbitrary range operations on the sorted map
  • endpoints: return the first or last key in the sorted map
  • comparator access: return the Comparator, if any, used to sort the map

Algorithms: Java platform comes with a few algorithms that operates on the colletion objects. All are static methods of Collections class, with first argument is the collection on which the method is to be performed. The list of methods include,

  • Collections.sort: use merge sort, which is stable and fast
  • Collections.shuffle
  • Collections.reverse, fill, copy, swap, addAll
  • Collections.binarySearch
  • Collections.frequency
  • Collections.disjoint: check if two collections have some elements in common
  • Collections.max, Collections.min

References:

Java tutorial: http://docs.oracle.com/javase/tutorial/collections/

Java Programming Cheat Sheet 3–Basic I/O

This is the third part of the Java programming cheat sheet. You may also interested in the first part OO concepts and second part Syntax.

I/O from Command Line: Java provides two ways, through the standard streams and through the Console

  • standard streams: Java supports three standard streams: standard input (System.in), standard output (System.out) and Standard Error (System.err). The standard streams are byte stream.
BufferedReader Reader cin

 = new BufferedReader(new InputStreamReader(System.in));

  • Console: more advanced than standard streams, retrieved from System.console().
  • Console c = System.console();
  • c.readLine();
  • c.printf();

Byte Streams: for I/O of 8-bit bytes. All byte streams are descended from InputStream and OutputStream. All other stream types are built on byte streams
Character Streams: Java stores character values using Unicode. Character stream I/O automatically translates the internal format to and from the local character set. e.g. Unicode ⇔ ASCII.

  • all character stream classes are descended from Reader and Writer.
  • there’re two general-purpose byte-to-character bridge streams: InputStreamReader and OutputStreamWriter. One can use them to create character streams when there’re no prepackaged character stream classes that meet our needs.

Buffered Streams: buffered streams are designed to reduce I/O overhead by maintain a I/O buffer at memory, and the I/O is committed when necessary. There’re four buffered stream classes.

  • BufferedInputStream and BufferedOutputStream: creates buffered byte streams
  • BufferedReader and BufferedWriter: creates buffered character streams

Formatting Streams: there’re two classes with formatting abilities, PrintWriter and PrintStream

  • PrintStream: a byte stream class. System.out and System.err are instances of this class.
  • PrintWriter: a character stream class.
  • Both PrintStream and PrintWriter implement same set of methods for converting internal data into formatted output.
    • print and println format individual values in a standard way
    • format formats almost any number of values based on a format string, with many options for precise formatting, just like printf

References:

1. Oracle Java Tutorial: http://docs.oracle.com/javase/tutorial/
2. javapapers blog: http://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/

Java Programming Cheat Sheet 2–Syntax

This is the second part of java programming cheat sheet. You may also interested in the first part OO Concepts and third part Basic I/O.

Primitive Data Types: Java compiler assign a default value for class fields, but not local variables.

Data Type

Sign

Default Value (for fields)

byte

8-bit signed

0
short

16-bit signed

0
int

32-bit signed

0
long

64-bit signed

0L
float

32-bit signle-precision signed

0.0f
double

64-bit double-precision signed

0.0d
char

16-bit unsigned

‘u0000’ (or 0)

boolean

true or false

false

Array

  • declaration: int myArray[];
  • allocation: myArray = new int[5];
  • declaration and allocation: int myArray[] = new int[5];
  • initialization: myArray = {1, 2, 3, 4, 5}
  • declaration, allocation and initialization: int myArray[] = {1, 2, 3, 4, 5}
  • access: myArray[0] = 1; Java array starts at index 0.

Operators:

  • bitwise operators
    • ~: invert a bit pattern
    • &: bitwise AND
    • ^: bitwise exclusive OR
    • |: bitwise inclusive OR
    • >>: signed right shift
    • <<: signed shift left
    • >>>: unsigned right shift, which shifts a zero into the leftmost position
  • assignment, arithmetic and unary
    • assignment: =
    • arithmetic: +, -, *, /, %
    • unary: + (indicates positive), -(negates and expression), ++, –, ! (inverts the value of a boolean)
  • equality, relational and conditional
    • equality and relational operators: ==, !=, >, >=, <, <=
    • conditional: &&, ||, ?:
    • type comparison: instanceof

Control Flow

  • if-else
if(condition){

    statement;

} else {

    statement;

}

  • switch
switch (expression) {

    case <option 1>:

        <statement>;

        break;

    case <option 2>:

        <statement>;

        break;

    default:

        <statement>;

}

  • while
while (expression) {

     statement;

}

  • do-while
do {

    statement;

} while (expression);

  • for
for (initialization; termination; increment) {

    statements;

}