Convert Java 8 Streams to Collections

Process data using Java 8 Streams and store in Collections including List, Set or Map.

“If we knew what it was we were doing, it would not be called research, would it?”
― Albert Einstein

1. Introduction

Java 8 Streams provide a cool facility to combine several operations in a single statement and express it concisely. Once the data is processed, you can collect the data in a List (or another collection). In this article, we present several methods to collect the results of these stream operations into collections.

Continue reading “Convert Java 8 Streams to Collections”

Using a Java TreeSet

Covers the basic operations on a java TreeSet including creation, iteration, finding elements, etc.

The unexamined life is not worth living.
― Socrates

1. Introduction

A java Set is a Collection containing only unique elements. A TreeSet is an implementation of the Set which uses a Red-Black tree implementation. In this article, we demonstrate how to properly create and use a TreeSet.

Continue reading “Using a Java TreeSet”

Removing Items in a Loop from a List

Learn about several methods to remove items in a loop from a java collection.

“Puns are the highest form of literature.”
― Alfred Hitchcock

1. Introduction

When you have a List or a Set or any other Collection, you may sometimes need to remove items in a loop. For example, you may be maintaining a list of messages for processing, and need to occasionally purge stale items from the list. In such a scenario, you would loop around the collection and remove the stale items.

Continue reading “Removing Items in a Loop from a List”

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”