Search This Blog


Tuesday, April 7, 2015

How to parse String to Date in Java using JodaTime Example

In this Java tutorial we will learn how to parse String to Date using Joda-Time library, for example, we will convert date String "04-12-2014" to java.util.Date object which represent this date. Before Java 8 introduced its new Date and Time API,  Joda was only reliable, safe and easy way to deal with date and time intricacies in Java. Java's own Date and Time was not that great, starting from JDK 1.1 when they made java.util.Date a mutable object and when they introduced Calendar in Java 1.2. It is one of the most criticized feature of Java on communities along with checked exception and object cloning. Even though Java 8 has corrected its mistake with an excellent, shiny new API, which address all past issue, Joda date and time library still has a role to play in Java systems. First and foremost reason is because most of the large banks and clients are still running on Java 1.6 and will likely take another 5 to 6 year to adopt Java 8, Joda is the only friend you can trust to deal with date and time nightmares. One of the most common task in Java is to parse String to Dates and even though Java provides a utility class called SimpleDateFormat, its not safe to use in multi-threaded environment until you know how to use thread confinement to make SimpleDateFormat thread-safe. Many beginners either create new instance of SimpleDateFormat each time they have to convert String to Date or commit classical mistake of storing it in an instance or static variable, only to face mysterious issues later. Since most of the Joda Time classes are Immutable, you can use them easily and safely in concurrent application.

Tuesday, March 24, 2015

How to Remove Duplicates from ArrayList in Java

ArrayList is the most popular implementation of List interface from Java's Collection framework, but it allow duplicates. Though there is another collection called Set which is primarily designed to store unique elements, there are situations when you receive a List e.g. ArrayList in your code and you need to ensure that it doesn't contain any duplicate before processing. Since with ArrayList you cannot guarantee uniqueness, there is no other choice but to remove repeated elements from ArrayList. There are multiple ways to do this, you can follow the approach we used for removing duplicates from array in Java, where we loop through array and inserting each element in a Set, which ensures that we discard duplicate because Set doesn't allow them to insert, or you can also use remove method of ArrayList to get rid of them, once you found that those are duplicates, but the simplest approach to remove repeated objects from ArrayList is to copy them to a Set e.g. HashSet and then copy it back to ArrayList. This will remove all duplicates without writing any more code. One thing to noted is that, if original order of elements in ArrayList is important for you, as List maintains insertion order, you should use LinkedHashSet because HashSet doesn't provide any ordering guarantee. BTW, if you are using deleting duplicates while iterating, make sure you use Iterator's remove() method and not the ArrayList one to avoid ConcurrentModificationException.  In this tutorial we will see this approach to remove duplicates.

Tuesday, March 17, 2015

10 Books Every Programmer Should Read

There is saying that if you learn from your mistakes, you will only going to learn few things, but if you learn from other's mistake, you can learn lot of things in short time, and what could be better way to learn from someone's experience then reading books. Programming as a career is about constant learning and updating yourself, but unfortunately there is no right way to begin. School and Colleges are far behind when it comes to real world programming, and every computer science graduate is not lucky to work in a company which is great in training. Only viable option is to read book and follow advice from the great programmer and authors who are gone to that path. I still regret that why I didn't come to know about Clean Code when I started programming. These are the books which can change your career, can make you better programmer. These books are more about a way of thinking, organizing, and becoming better at the craft of software engineering. They won't make you better at any particular programming language e..g Java but they will help you to become a better Programmer.