Best Way to Compare Two Strings in Java Alphabetically with Example

There are multiple ways to compare two strings alphabetically in Java e.g. == operator, equals() method or compareTo() method, but which one is the best way to check if two strings are equal or not? Programmers often confused between == operator and equals() method, and think that comparing strings using == operator should be faster than equals() method, and end up using that. Though they are not completely wrong, they often missed the point that == operator is designed to compare object equality, not String equality, which is actually defined in equals()method and compare Strings alphabetically. When you compare two strings using the == operator, it may or may not return true, especially if you are expecting result based on contents. It will only return true if both reference variables are pointing to the same objects, like in the case of interned string or String literals. Otherwise, it will return false, even if the content of String remain same.

How to Iterate over Array in Java 1.5 using foreach loop Example

Though there are many ways to loop over Array in Java, including classical for loop, while loop with length counter, nothing matches the elegance of Java 1.5 foreach loop, which makes iteration super easy and super cool. When we need to iterate through each element of an array and has to perform some operation on them e.g. filtering, transformation or simply printing, foreach loop comes to its full glory. There is not much difference between traditional for loop and Java 1.5 foreach loop, except that former has counter and condition, which is checked after each iteration, on the other hand, foreach loop does this task under the hood. But this elegance comes at the cost of control, as you can not control iteration using counter, so depending upon your need, you have to choose between them. 

Can we Override Private Method in Java? Inner Class?

No, you cannot override private methods in Java, private methods are non-virtual in Java and access differently than non-private one. Since method overriding can only be done on derived class and private methods are not accessible in a subclass, you just can not override them. By the way, one more possibility of overriding private methods in an inner class, since private methods are accessible in an inner class, and that’s why it is one of the tricky java interview questions. Anyway, this will also not work because private methods are bonded during compile time and only Type (or Class) is used to locate a private method. For Example in below code where it looks like that nested class is an overriding private method, but if you call privateMethod() with a type of super class but the object of the subclass, it will only execute privateMethod() declared in the parent class, which is not exactly method overriding. 

Best Way to Iterate Over Each Entry of HashMap in Java

What is the best way to Iterate over HashMap in Java? and not just HashMap, but any Map implementation including old Hashtable, TreeMap, LinkedHashMap and relatively newer ConcurrentHashMap, is a frequently asked query from Java Programmers, with some experience under his belt. Well, when it comes to choosing between different ways to iterate over Map in Java, it's you need, which plays an important role. For example, if you just want to iterate over each entry of HashMap, without modifying Map, then iterating over entry set using Java 1.5 foreach loop seems the most elegant solution to me. The reason, it just two lines of code using a foreach loop and Generics, and by getting the set of entries, we get key and value together, without further searching in HashMap. This makes it also the fastest way to loop over HashMap in Java. 

Difference between Comparator and Comparable in Java - Interview Question

Comparator and Comparable are two interfaces in Java API, which is used to compare two objects in Java. Though both are used for comparison there are some differences between them, a major difference between Comparable and Comparator is that former is used to define the natural ordering of object e.g. lexicographic order for java.lang.String, while later is used to define any alternative ordering for an object.  The main usage of java.lang.Comparable and java.util.Comparator interface is for sorting a list of objects in Java. For example to sort a list of Employee by their Id, we can use Comparable interface and provide additional sorting capability, we can define multiple comparators e.g. AgeComparator to compare the age of the employee, SalaryComparator to compare the salary of employees etc.  This brings another important difference between Comparator and Comparable interface in Java, you can have only one ordering via Comparable e.g. natural ordering while you can define multiple Comparator for alternative ordering as discussed above.

Top 5 Data Structures in Java

Java API provides built-in support for common data structures, essential for writing programs e.g. array, linked list, map, set, stack and queue. You don't need to implement these data structure by yourself, you can directly use it in your program, thanks to rich and efficient implementation provided by Java API. This is also one reason, why Java is the best programming language. Since data structure is core to any program and choice of a particular data structure greatly affects both functionality and performance of Java applications, it's worth an effort to explore different data structure available in Java. Many of these data structure are part of hugely popular Java Collection Framework, and almost all Java programs may be, except hello world make use of Collection in one or another form. In this Java tutorial, we will take a look at common data structure e.g. Array, LinkedList, Stack, Queue, Map, Set and How they are implemented in Java, along with how to use them.

java.lang.OutOfMemoryError: Java heap space : Cause and Solution

So you are getting  java.lang.OutOfMemoryError: Java heap space and run out of ideas on what to do, especially if you are user of any Java application and not the programmer or developer, this could be a tricky situation to be in. I receive lots of emails from Minecraft user ( a popular Java game), along with junior developers who are using Tomcat, JBoss, Websphere, Android user, who uses Android apps and several other Swing based Java desktop application user complaining about java.lang.OutOfMemoryError: Java heap space in there Mobile or Laptop. For users, there is not much they can do, but to restart the application. When you restart your application, previous memory get reclaimed by OS and it's a fresh start for your application, but you may lose data, which is not saved, especially if you are playing Java games like Minecraft.