Snippet IT IT News, Programming, Internet and Blogging

5Jan/100

Java: Format Integer Into Fixed Width String

Sometime, you may want to format an integer or a long value into a fixed width String. You need to display the value in fixed width especially in reports where you want to keep the numbers in aligned (e.g. 3456 as 0003456 and 1234 as 0001234).

Other than that, you may also want to format the integer value for a better visibility. For example, to display integer value as a document number (e.g. 12345678 as 00-01234-5678), for example an account number.

The Java.format() is not flexible enough to solve my problem. At least it cannot be used to format a fixed length document number in the format I want: XX-XXXX-XXXX. Therefore I decide write my own format function.

10Nov/090

Java: Loading Large Data into JTable or JList

In software programming (especially Graphical User Interface or GUI programming), many times, we need to load data from file or database into a table or list to be displayed to user on computer screen. Most of the time, the size of the data can be determined only during runtime. The data could be very small to a few rows of record and could be very large to a few millions rows of record.

14May/090

Java: Least Recently Used (LRU) Cache

Least Recently Used or in short LRU is an performance optimizing algorithm. LRU algorithm is widely used in software programming as well as in hardware instructions (e.g. CPU).

In many case, in programming, we may want to cache some data from physical disk, for example database and files, but we cannot load all the data into memory due to the memory limitation on the server or PC. In those cases, you may want to consider to use LRU algorithm to solve the memory limitation problem.

12Apr/090

Java: Randomly sort values in a array (the generic way) in single pass

Few years back (yes, about 4 years back), I wrote an article about randomly sort query results in MySQL. The main problem in randomly sort query results is that when you have a very large data in your database and the statement to sort the result may change the data execution plan causing DMBS does not choose the right plan. Therefore, sometime it is more wise to sort it in your own program instead of too depending on DBMS's decision which may affect the overall performance of a program.

20Mar/091

Java: Format integer into number of decimal places and performance

For certain reasons (e.g. precision issue), some programmer may store a float value (e.g. money) in integer variable. For example -12345678.90 is stored as -1234567890. To convert the integer value to number with number of decimal places is a problem.

There are several way to do it. You can simply use the Java's String.format(), DecimalFormat or Float.toString(). Most of them does not meet my requirements: Simple, fast, precise and with thousand separator. Therefore I wrote a simple class that ease myself.