Python itertools – ifilter, islice, imap, izip

Make your life easier with these itertools when dealing with iterables

“A lie gets halfway around the world before the truth has a chance to get its pants on.” ― Anonymous

1. Introduction

Python provides the itertools package which provides convenience functions for many common iterator operations. We have covered count(), cycle() and chain() in the first part of this series, and compress(), dropwhile(), and groupby() in the second part. In this article, we present a few examples of ifilter(), islice(), imap() and izip().

Continue reading “Python itertools – ifilter, islice, imap, izip”

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”

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”

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”

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”