Search This Blog


Saturday, July 4, 2015

How to use PriorityQueue in Java? An Example

PriorityQueue is another data structure from Java Collection framework, added in Java SE 5. This class implements Queue interface and provides a sorted element from head of the queue. Though it provides sorting, its little different with other Sorted collections e.g. TreeSet or TreeMap, which also allows you to iterate over all elements, in priority queue there is no guarantee on iteration. The only guaranteed PriorityQueue gives is that lowest or highest priority element will be on head of the queue. So when you call remove() or poll() method, you will get this element and next on priority will acquire the head spot. Like other collection classes which provides sorting, PriorityQueue also uses Comparable and Comparator interface for priority.  when you add or remove elements from PriorityQueue, other elements are compared to each other to put highest/lowest priority element at head of the queue.  priority queue data structure is internally implemented using binary heap data structure, which allows constant time access to maximum and minimum element in heap by implementing max heap and min heap data structure. In these data structure root of the binary tree always contain either maximum value (max heap) or minimum value (min heap), since you have constant time access to root, you can get max or minimum value in constant time. Once this element is removed from root, next maximum/minimum is promoted to root. So, if you want to process elements in their relative priority order, you can use PriorityQueue in Java. It provides constant time access to highest or lowest priority element. Good knowledge of Java Collection framework is absolutely necessary to become an expert Java developer and if you want to become one, I suggest you to read Java Generics and Collection by Maurice Naftalin. One of the best book which extensively cover this topic and explains about almost all the collection classes available in Java till Java SE 6.

Thursday, July 2, 2015

5 Good Books to Learn Java 8 Functional Programming

TL;DR : I have read many books while learning Java 8, but the the best book I found was Java SE 8 for Really Impatient from Cay S. Horstmann. Why I recommend this book? three reasons, first clarity, second size of the book is not overwhelming, and third and most important I was able to connect to this book. I didn't find it boring or feeling sleepy as soon as I started reading, which in my opinion is a litmus test for any book.  Its been more than a year since Java 8 was launched in March 2014 and number of Java developers looking to learn Java 8 features like lambda expression, streams, and new date and time API is increasing exponentially. Since Java 8 is  significantly different from earlier Java version, thanks to the functional programming approach introduced in this release to take advantage of multicore CPU, programmers are finding it hard to grasp new concepts. Questions like which books should I refer to start learning Java 8? or what is the best book to learn Java 8 is increasingly seen in various places on internet. I had started learning Java 8 from last year itself, even before Java SE 8 was released and I have gone through many books in past one year. When many of my reader asked me to suggest a good book to learn Java 8, I thought about writing this blog post and sharing my experience with some of the best Java 8 books. These books are equally useful for beginners and senior and experience Java programmers looking to learn  functional programming in Java 8.

Wednesday, July 1, 2015

How to append text to existing File in Java? Example

In last tutorial, you have learned about how to write data to a file in Java and in this tutorial you will learn how to append text to a file in Java. What is difference between simply writing to a file vs appending data to a file? In case of writing to a file, program can start writing from start but in case of appending text, you start writing from the end of the file. You can append text into an existing file in Java by opening a file using FileWriter class in append mode. You can do this by using special constructor provided by FileWriter class, which accepts a file and a boolean, which if passed as true then open the file in append mode. This means you can write new content at the end of the file. One of the common example of appending text into file is logging but for that you don't need to write your own logger, there are several good logging library available in Java world e.g. Log4j, SLF4j, Logbak and even java.util.logging is good enough. In this tutorial, you will learn how to append data to an existing text file from Java program. As I said previously, if you are starting fresh in Java then I suggest you to better follow a book because they provide comprehensive coverage, which means you can learn lot of things in quick time. You can follow either core Java by Cay S. Horstmann or Java : A Beginners guide by Herbert Schildt, both are very good book and highly recommended for beginners in Java. Good point about a beginner's guide is that it also covers Java 8 while core Java 9th Edition only covers up-to Java 7.