Java Comparator Example for Custom Sorting Employee by Name, Age and Salary

In this tutorial, we will see Java Comparator example to sort an Employee object by name, age and salary. In order to sort Employee object on different criterion, we need to create multiple comparators e.g. NameComparator, AgeComparator and SalaryComparator, this is known as custom sorting in Java. This is different then natural ordering of object, provided by compareTo() method of java.lang.Comparable interface. Though both compare() and compareTo() method looks similar they are different in a sense that, former accept one parameter, while later accept two parameter. Former compare passed object to current object, on the other hand compare() method compares two different object passed to it. You can create this comparator as nested static class, because they require access of private members of class. Once you create these Comparator implementations, all you need to do is to override compare() method accordingly e.g. in compare() method of NameComparator compare name of two employee and return positive if name of first employee is greater than second, return negative if name of first employee is less than second and return zero if name of both employees are equal. By using Generics, you can avoid casting of object into Employee inside compare() method, as shown in example of this article. Once you have your Comparator classes ready, you can create a bunch of employee and sort them using any Comparator by passing List of employees and Comparator into Collections.sort(List, Comparator) method. By the way this Java example also implements equals(), hashcode(), compareTo() and toString() method for Employee object, along with JUnit test for testing various Comparator implementation in file

10 Tips to Improve Programming Skill and become better Programmer, Developer

Many Java developers ask me How to become a better programmer, how do I improve my programming skills, or I am good at Java but not so good on problem-solving skill etc. This is a reality, today's era is full of language expert than real programmers. It's easy to understand keywords, methods and API of Java programming language, but same time it's difficult to solve real problems, design reusable and robust software and get the maximum of data structure and algorithm. I have often seen Java programmers are not doing so well when asked to design and code a solution in limited time, but same time they are really good with Java concepts and all theory. Even senior programmers of 4 to 6 years experience in Java programming, sometimes fail to solve questions like designing

Eclipse Not Able To Connect Internet, Market Place - Configure Connection Proxy Settings

If you are using Eclipse in your company, you might have faced issues, where Eclipse is not able to connect to internet. Since most of companies uses proxy for connecting to Internet, its important to configure proxy settings in Eclipse to allow Eclipse to access Internet behind organization's firewall. If your company regularly changes there internet proxy settings then this also needs to be updated whenever your Proxy server changes. Since Eclipse needs internet connection for connecting to Eclipse Market place, installing and updating plugins, downloading Maven dependencies from remote maven repository, it's really frustrating when Eclipse just hung up, shows connecting and time out after a long time. If you see Eclipse is not able to connect to Market Place or not able to download any Maven dependency, first thing to check is your connection proxy settings. If you don't know where to find connection proxy settings for Eclipse IDE or doesn't know how to configure proxy setting for internet in Eclipse IDE, then you can follow steps given in next section of this article. By the way, this is one of the several tips, which I have shared on Eclipse IDE e.g. Eclipse  keyboard shortcuts, Eclipse debugging tips and productivity settings. Since Eclipse is one of my favourite and most widely used FREE IDE for Java development, it's important for every Java developer to some useful configuration and settings details.

Why Default or No Argument Constructor is Important in Java Class

Almost all Java developers knows that compiler adds a default constructor or better known as no argument constructor in every Java class, but many of them forget that, it only does when you don't provide any other constructor. Which means it becomes developers responsibility to add a no argument constructor, if he is adding explicit constructor. Now, Why it's important to provide default constructor in Java, What happens if your class don't have a no argument constructor? Well, this is how it's asked in many Java interviews, most commonly as part of Spring and Hibernate interviews. It's not mandatory to define default constructor, but if you are writing Hibernate persistent class, JPA entities or using Spring framework to manage object creation and wiring dependencies, you need to be bit careful. Many of open source framework, uses reflection to create instance or Object at runtime, based upon name of class. For example When Hibernate creates instance of entities using reflection it uses Class.newInstance() method, which require a no argument constructor to create an instance. It's effectively equivalent of new Entity(). This method throws InstantiationException if it doesn't found any no argument constructor in Entity class, and that's why it's advised to provide a no argument constructor.

Why become Java Certified? Does Oracle Certified Professional Earn more?

I have seen many Java programmers in doubt whether to go for Java certifications or not? Whether getting Java certified will help them to earn a better salary? or after becoming Java certified from Oracle will help them to get a Job. In this article, we will see what are benefits of getting Java certified. I know higher income, better Job prospect and a better chance of promotion are at the top of the benefits of becoming Java certified, but this certification provides more value than that. Another common question many Java developer asked to me is, whether I should go for OCPJP 7 (Oracle Certified Professional for Java SE 7), or OCAJP 7 (Oracle Certified Associate for Java SE 7) or do you need to pass OCA before going for OCP Java SE 7?. One more doubt is in their naming, especially after Oracle's acquisition of Sun Microsystems. These certifications are known as different names e.g. SCJP (Sun certified Java Programmer) is still the more popular name for Java certification than OCP. Well, getting Java certified and recognized is always good for your career, knowledge and confidence, but it's even more important for beginners and junior Java developers, who have just started their career. They are the one who needs something extra, apart from their regular computer science degree to impress the perspective employer and get an edge over other candidates. There are many advantages and reasons of why Java certifications like SCJP, also known as OCPJP is good for Java programmers, and everybody has their own reasons of getting certified. I think following 10 reasons make sense why should you become a Java certified professional:

Top 5 Free Scala Programming Books for Java Programmers, Download as PDF or Read Online

Scala is another JVM based programming language, which is quickly gaining popularity because of its interesting mix of object oriented and functional programming paradigm. Many companies has started using Scala for there strategic and mission critical development. One of the best known is Twitter, which is also one of the key factor in growing popularity of Scala programming language. If you are a Java, C++ or C# programmer, who is willing to learn Scala in your spare time, then you will be surprised to see how much helpful content is available for FREE on internet. By the way Java 8 is not far way, only couple of months to go and there are already some decent tutorials available online. If you are learning Java 8, you may like to see my list of resources on Java 8 as well. I personally prefer books to start with and that's why, when I recently started learning Scala, I did some research on Scala programming books, which are available for FREE download or online reading. I am surprised to see lots of good contents, including Scala for the Impatient from Cay Horstmann, which I already had. Other good FREE Scala books I found was Effective Scala, looks like inspired by Effective Java, from Marius Eriksen, Twitter Inc. This is not as exhaustive as Effective Java and I guess it will evolve into similar length, but still contains lots of good advice on Scala programming. One more Scala book which is available for free is Programming in Scala, First Edition by Martin Odersky, Lex Spoon, and Bill Venners, this is bit old, first published at December 10, 2008, but still a good book to have, given Martin, inventor of Scala is co-author. Another good tutorial cum book I found is Scala School, collection of lectures from Twitter to prepare experienced engineers to be productive Scala programmers. You can read this on-line. Best things about all these resources are that they are FREE, you don't need to spend single penny to get these tutorials, of-course if you like you can also purchase some more good books, I have mentioned in my earlier post about difference between Scala and Java programming language. Now let's see from where can you download these free Scala programming books and how good they are.

How to Remove Objects from Collection or ArrayList in Java while Traversing - Iterator remove() method Example

How do you remove objects from Java collections like ArrayList, while iterating is one of the frequent questions my reader asked to me in my post about Top 25 Java Collection Interview Questions. Well, this question may seems quite easy, because every java.util.Collection implementation e.g. List or Set has remove() method to delete a particular object, which can be used to remove elements from any Collection e.g. ArrayList, LinkedList or Vector. Well, this is where things goes wrong and interviewers are interested to see, whether you can point about remove() method from Iterator or not. Answer of this question is as simple as that, you should be using Iterator's remove() method to delete any object from Collection you are iterating, but this is not the end of this question. Most likely you will be asked to explain, what is difference in removing object using remove() method of Collection over remove() method of Iterator and why one should use over other? Reason is ConcurrentModificationException, if you use remove() method of List, Set or basically from any Collection to delete object while iterating, it will throw ConcurrentModificationException. Though remove() method of java.util.Collection works fine to remove individual object, they don't work well, when you are iterating over collection.  Let's see a code example to clear doubts

How to Remove Duplicates from Array without using Java Collection API

This is a coding questions recently asked to one of my reader in a Java Technical interview. Question was to remove duplicates from an integer array without using any collection API classes like Set or LinkedHashSet, which can make this task trivial. In general, if you need to do this for any project work, I suggest better using Set interface, particularly LinkedHashSet, because that also keep the order on which elements are inserted into Set. Only for technical interview perspective, you need to do this using either loops or recursion,  depending upon what is your strongest area. In this article, I am sharing a naive solution, which has lots of limitation to be considered as production quality code, It's not the best solution but still a solution. Main problem, while dealing with array is not finding duplicates, it's about removing them. Since array is a static, fixed length data structure, you can not change its length. This means, deleting an element from an array requires creating a new array and copying content into that array. If your input array contains lots of duplicates then this may result in lots of temporary array. It also increase cost of copying contents, which can be very bad. Given this restriction, you need to come out with a strategy to minimize both memory and CPU requirements.

Does Java Certifications like SCJP, OCJP or OCPJP helps in Job and Career?

First of all, Wish you very Happy New Year 2014 !!, May god brings success to you all. I receive lots of questions about the usefulness of Java certifications like SCJP, now known as OCJP or OCPJP. Questions like, does getting a Java Certification helps to land a job, or does certified Java developer earn more or gets better offers, are quite common to Java developers, especially freshers. In this post, I will try to answer few of these question based upon my own experience as experience Java programmer and a certified Java developer. Well, I did my first Java Certification, SCJP (Sun Certification for Java Programmers) a long back and then the subsequent year I completed my SCWCD (Sun Certification for Web Component Developer). I did because that time, I was hungry to learn more about Java, I enjoy doing Java exercises and above all my company was reimbursing the cost of certification, of course if you happened to score more than 95%. That time I was in the start of my career and this sort of motivation worked for me, but my biggest gain was my learning about subtle details of Java programming as part of my preparation I learned by passing those exams certainly helps me to understand the language better. There was no way,  I would have known subtle details about finally block,  checked exceptions and multi-threading in the very short span of time. I realize that preparation is the key, that's the process which will benefit you most. When you prepare, you learned a lot by the following the concept, doing exercises and then analyzing why certain options are correct and others are not. This technical knowledge gives you an edge over another candidate during written exams, telephonic interviews and even on faced to face Java interviews.