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”

An Introduction to Python Sets

Python supports sets which are a collection of unique elements and provide operations for computing set union, intersection and difference.

“The world is a book and those who do not travel read only one page.”
― Augustine of Hippo

1. Introduction

A set is a collection of unique elements. A common use is to eliminate duplicate elements from a list. In addition, it supports set operations like union intersection and difference.

Continue reading “An Introduction to Python Sets”

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 Streams groupingBy Examples

Learn how to perform SQL-like grouping and summarizing calculations on Java Collections (List, Map, etc).

1. Introduction

Have you wanted to perform SQL-like operations on data in a List or a Map? Maybe computing a sum or average? Or perhaps performing an aggregate operation such as summing a group? Well, with Java 8 streams operations, you are covered for some of these.

A previous article covered sums and averages on the whole data set. In this article, we show how to use Collectors.groupingBy() to perform SQL-like grouping on tabular data.

Continue reading “Java Streams groupingBy Examples”

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”

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”