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.
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
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.
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.
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?.
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.
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.
This is a coding question recently asked to one of my readers 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.
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 on 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).