Python – More List Methods and Recipes

Remove items from a List and more.

“Happiness is a perfume you cannot pour on others without getting some on yourself.” ― Ralph Waldo Emerson

1. Introduction

In a previous article, we covered some python list methods related to adding items. Let us now continue the review of more list methods.

Continue reading “Python – More List Methods and Recipes”

Python List Methods: Adding Items

Check out this refresher on ways to add items to python lists

“Share your knowledge. It is a way to achieve immortality.” ― Dalai Lama XIV

1. Introduction

Python offers the list data type which is similar to an array in C/C++ or Javascript or a List in Java. Of course, there are differences between the list in python and these languages. Sigh, wouldn’t it have been nice if all the common languages offered such data types with the same methods – same name, parameters, everything? Then you could have concentrated on mastering one and you would have it all. At the very least, this could have been done for common programming entities such as strings, list/array, characters, integer and float numbers, booleans, etc.

Continue reading “Python List Methods: Adding Items”

Java 8 Streams Map Examples

Convert one value to another in a Streams Operation

“Always and never are two words you should always remember never to use. ” ― Wendell Johnson

1. Introduction

Do you know about Java 8 Streams?

Java 8 streams is a very powerful facility for working with collections. What formerly required multiple lines of code and loops can now be accomplished with a single line of code. A part of Java 8 streams is the map() facility which can map one value to another in a stream. In this article, let us examine some common use cases where map() is useful.

Continue reading “Java 8 Streams Map Examples”

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”

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”

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”

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”

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”

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 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”