Use Java8 Optional to Avoid Problems

Use Java 8 Optional to avoid unexpected conditions like NPE in your code.

“Life always begins with one step outside of your comfort zone.”
― Shannon L. Alder

1. Introduction

Java version 1.8 provides the new class Optional which represents an abstration of an optional value. That is the object may contain a value or a null. This class provides several useful features which help java programmers avoid NullPointerException. Let us look into this class in more detail.

Continue reading “Use Java8 Optional to Avoid Problems”

Using Java ArrayList’s List Iterator

A ListIterator allows traversal of a Java ArrayList in forward or backward direction.

“A man devoid of hope and conscious of being so has ceased to belong to the future.”
― Albert Camus, The Myth of Sisyphus and Other Essays

1. Introduction

Java’s ArrayList class provides a list iterator which allows, among other things, traversal of the list in either direction. This is in comparison to a normal Iterator that allows traversal of the list in forward direction only. In this article, we delve into the usage and behavior of the ListIterator when used with an ArrayList.

Continue reading “Using Java ArrayList’s List Iterator”

Searching a List in Java

Easily search a java list using java 8 predicate functions.

“Make improvements, not excuses. Seek respect, not attention.”
― Roy T. Bennett, The Light in the Heart

1. Introduction

Searching, sorting and iterating are some of the fundamental operations required when using a list. Searching refers to finding one or more objects in the list that match a given condition. In this article, we look at some methods for searching a list in java.

Continue reading “Searching a List in Java”

Sorting Lists in Java

Learn how to sort a List in java. Use Java 8 Lambdas to compose sort conditions.

“You can only be afraid of what you think you know.”
― Jiddu Krishnamurti

1. Introduction

Sorting a list is a very common requirement when programming in java. Out of the box, java provides for sorting using a performant algorithm so there is no need to roll your own algo for the purpose. In this article, we demonstrate how to sort a list in java.

Continue reading “Sorting Lists in Java”

How to Convert Large CSV to JSON

Learn how to use Jackson Streaming to convert a large CSV to JSON.

“We are a way for the cosmos to know itself.”
― Carl Sagan, Cosmos

1. Introduction

Let us today look into converting a large CSV to JSON without running into memory issues. This previous article showed how to parse CSV and output the data to JSON using Jackson. However, since that code loads the entire data into memory, it will run into issues loading large CSV files such as:

Continue reading “How to Convert Large CSV to JSON”

Using Timer Class to Schedule Tasks

Learn how to use the Timer and the TimerTask classes to implement simple task scheduling for your application.

“Humor is reason gone mad.”
― Groucho Marx

1. Introduction

Scheduling tasks to run is a need which sometimes arises in a java program. Maybe you want to run periodic cleanup of some resource. Or check on the status of some job. Or maybe fetch a URL which might not be available the first time.

Continue reading “Using Timer Class to Schedule Tasks”

Java – Pivot Table using Streams

Implement a Pivot Table in Java using Java 8 Streams and Collections.

“Money may not buy happiness, but I’d rather cry in a Jaguar than on a bus.”
― Françoise Sagan

1. Introduction

Today let us see how we can implement a pivot table using java 8 streams. Raw data by itself does not deliver much insight to humans. We need some kind of data aggregation to discern patterns in raw data. A pivot table is one such instrument. Other more visual methods of aggregation include graphs and charts.

Continue reading “Java – Pivot Table using Streams”

Java Regex – Simple Patterns

Learn the basics of regular expression pattern matching in Java. Covers a few basic regular expression constructs.

“Life’s under no obligation to give us what we expect.”
― Margaret Mitchell

Introduction

Regular Expressions are the bread and butter of string pattern matching. They allow you to express a search pattern in general terms without being too specific about what you are searching for.

This article covers the basics of string pattern matching using regular expressions.

Continue reading “Java Regex – Simple Patterns”

Sort Large CSV File using SQLite

Sorting a large CSV file by loading it into SQLite. Much faster and easier to process.

“When you’re at the end of your rope, tie a knot and hold on.”
― Theodore Roosevelt

1. Review

We are trying to sort a large CSV file. The file contains a couple of million rows – not large by “big-data” standards, but large enough to face problems working with it.

Continue reading “Sort Large CSV File using SQLite”

Sorting a Large CSV File

Large CSV files present a challenge when need arises to sort. Learn how to do that using a database.

“All of life is a constant education.”
― Eleanor Roosevelt, The Wisdom of Eleanor Roosevelt

1. Introduction

Let us explore some ways of sorting large data sets.

By large, I don’t mean typical “big-data” sizes – which might consist of billions of rows. Such data sets fall into the realm of “big data” which we are not exploring today. Instead I am talking of sorting a rather large CSV file – maybe a couple of million rows.

Continue reading “Sorting a Large CSV File”