Using Java Collectors

Use Java 8 Streams and Collectors to slice and dice lists including computing sums, averages, partitioning and more.

1. Introduction

Java 8 provides the new Streams facility which makes many Collection operations easy. Streaming items from a collector and filtering the data are trivial. As well as are sorting, searching and computing aggregates. That is, if you are familiar with the many Collectors functions available. We present some of these functions here.

Continue reading “Using Java Collectors”

Convert Excel to CSV (UTF-8)

Export data from Excel spreadsheet to CSV using Java. Properly handles exporting Unicode data in the spreadsheet.

1. Introduction

Let us look into how to convert Excel to CSV.

CSV stands for Comma-Separated-Values and is a very common format used for exchanging data between diverse applications. While the Excel Spreadsheet file format is complex (since it has to accommodate a lot more!), CSV is a simpler format representing just tabular data.

Continue reading “Convert Excel to CSV (UTF-8)”

Java Process Example Part 2

When reading and writing to a child process, it is necessary to execute the read and write blocks in separate threads to avoid deadlock.

1. Introduction

In the previous part of this Java Process Guide, we looked at how to read from and write to a child process. While executing the read and write blocks in a single thread works for simple cases, we should run these in separate threads to avoid deadlocks.

Continue reading “Java Process Example Part 2”

Java Process Example

Startup a native process from Java using Runtime. Read the output from the process and write some data to it. After you are done, terminate the process.

1. Introduction

Have you ever run into a situation where you need to execute an OS command from inside java and read its output? You can use the Process class to do so, but there are some caveats to consider.

Continue reading “Java Process Example”

Java ArrayList Benchmark

Learn about ArrayList and LinkedList performance. In this article, we test creation, addition and iteration performance of these containers.

“Don’t mistake activity with achievement.”
― John Wooden

1. Introduction

Let us examine the performance characteristics of List Implementations – ArrayList and LinkedList. We perform the testing for a variety of list sizes from 100 items to 1M items. We use the JMH test harness to conduct the test on a single core machine. Results are presented below.

Continue reading “Java ArrayList Benchmark”

Java HashMap – Remove Entries

Learn about removing entries from a HashMap, including removing by key, with an iterator and bulk removal with a filter.

“To live is the rarest thing in the world. Most people exist, that is all.”
― Oscar Wilde

 

1. Introduction

After covering HashMap basics, iteration and searching/sorting, let us look at a few ways of removing entries from a HashMap.

Continue reading “Java HashMap – Remove Entries”

Java HashMap Search and Sort

Search and sort a HashMap by key as well as by value.

“What you stay focused on will grow.”
― Roy T. Bennett

1. Introduction

Having explored HashMap in several previous articles (here and here), let us now learn how to search and sort a HashMap by key as well as value.

Continue reading “Java HashMap Search and Sort”

Java Collections – HashMap

Create a HashMap by parsing CSV. Declare and use a multi-map (a HashMap inside a HashMap).

“Don’t judge each day by the harvest you reap but by the seeds that you plant.”
― Robert Louis Stevenson

Check out the previous parts of this Java Collections Guide: Introduction, Part 1 – Collection, Part 2 – Sets and Part 3 – ArrayList.

1. Introduction

The HashMap implements the Map interface and provides the abstraction of a dictionary mapping of keys to values. It is a part of the Java Collections Framework but does not fall under the Iterable and Collection hierarchy. In this article, we learn how to use HashMaps.

Continue reading “Java Collections – HashMap”

Java Collections – ArrayList

How to properly create and use an ArrayList including adding, retrieving and modifying items.

Check out the previous parts of this Java Collections Guide: Introduction, Part 1 – Collection and Part 2 – Sets.

1. Introduction

The ArrayList is one of the most widely used classes in the Java Collections Framework (if not the entire JDK; that position probably belongs to String). It provides a simple array-like storage of items with dynamically adjusting capacity. In this article, we present some usage patterns of the List and ArrayList.

Continue reading “Java Collections – ArrayList”