Wednesday, August 15, 2018

Read/Write large excel (xlsx) file

In this article you will see how to write a huge excel file.

Using SXSSFWorkbook you can write large excel file. It is a Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy. This allows to write very large files without running out of memory as only a configurable portion of the rows are kept in memory at any one time.

Note - I have tested it to write 10,48,576 rows which created 40 MB excel file. If you want to write more rows than this then you may opt to write CSV file.

You need to add below dependencies in your pom.xml

Tuesday, August 7, 2018

Berlin Clock with Java

The Berlin Clock is a rather strange way to show the time. On the top of the clock there is a yellow lamp that blinks on/off every two seconds. The time is calculated by adding rectangular lamps.
The top two rows of lamps are red. These indicate the hours of a day. In the top row there are 4 red lamps. Every lamp represents 5 hours. In the lower row of red lamps every lamp represents 1 hour. So if two lamps of the first row and three of the second row are switched on that indicates 5+5+3=13h or 1 pm.
The two rows of lamps at the bottom count the minutes. The first of these rows has 11 lamps, the second 4. In the first row every lamp represents 5 minutes. In this first row the 3rd, 6th and 9th lamp are red and indicate the first quarter, half and last quarter of an hour. The other lamps are yellow. In the last row with 4 lamps every lamp represents 1 minute.

Sunday, August 5, 2018

JBehave Hello World example (Eclipse + Maven + JUnit)

JBehave is a framework for Behaviour-Driven Development (BDD). To know more about JBehave, click here.

Below is the eclipse folder structure -

Please create a maven project and add the following files -

Monday, July 30, 2018

Binary tree - DFS and BFS

 Depth first search

In depth first search (dfs) you start with a root node and keep on traversing left node (or right node) until you reach leaf node, then backtrack for other side.

There are two ways in which you can implement dfs search -

  • Recursive
  • Iterative using stack

Thursday, July 26, 2018

Least Recently Used (LRU) cache

Below java code is a simple implementation of Least Recently Used (LRU) cache using HashMap and LinkedList.

For simplicity i am taking [Integer, String] as a key value pair for the cache, you can use any other structure as per your requirement.

LRU cache requirement -

get(key) will return the value if  present otherwise will return 'NA'

set(key,value) will insert the value if it is not already present else update the value. Also when cache reaches its MAX capacity it should remove least recently used value before inserting a new value.

Logic -
HashMap will contain the key,value pair of the cache and LinkedList will be used to track the usage. As you know LinkedList is a Doubly-linked list implementation of the List which maked insertion and deletion faster. Here first item will be the oldest item whereas last item will be the most recent item.