Learn the intricacies of Java Number to String Conversion and vice-versa.
“It is a good thing for an uneducated man to read books of quotations.”
― Winston S. Churchill
Number conversion is a frequent need while programming. You may have a need to convert from a hex string, decimal string or octal string. Or maybe you are using a number from a different base. The reverse conversion is also required many times: converting from a decimal integer to hex string, octal string, etc. Let us see how we can do these conversions.
Continue reading “Java Number Conversion”
Learn the intricacies of using the Java Path API in this tutorial
“The mind is not a vessel to be filled, but a fire to be kindled.”
The Java Path API is a new introduction starting from version 1.7. It provides convenience methods for manipulating file paths in a system-independent way. Let us examine the usage of the Path API in more detail.
Continue reading “Java Path API Tutorial”
Efficiently read text and binary files in Java
“The world is a book and those who do not travel read only one page.”
― Augustine of Hippo
What’s the most efficient and easiest way to read a large file in java? Well, one way is to read the whole file at once into memory. Let us examine some issues that arise when doing so.
Continue reading “Java – Reading a Large File Efficiently”
Learn how to properly create and use ThreadLocal to share data between threads.
“A lie gets halfway around the world before the truth has a chance to get its pants on.”
― Winston S. Churchill
Getting multi-threaded programming right is something of an art; it can be hard to foresee and avoid the many pitfalls. One such hazard comes from attempting to share a variable across multiple threads.
Continue reading “Java ThreadLocal – Proper Usage”
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
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”
Covers the basic operations on a java TreeSet including creation, iteration, finding elements, etc.
The unexamined life is not worth living.
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”
Learn about several methods to remove items in a loop from a java collection.
“Puns are the highest form of literature.”
― Alfred Hitchcock
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 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
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”
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
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”
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
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”