2 Ways to sort HashMap in Java? Example

So you have a Map in your Java program and you want to process its data in sorted order. Since Map doesn't guarantee any order for its keys and values, you always end up with unsorted keys and Map. If you really need a sorted Map then think about using TreeMap, which keeps all keys in a sorted order. This could be either natural order of keys (defined by Comparable) or a custom order (defined by Comparator), which you can provide while creating an instance of TreeMap. If you don't have your data in a sorted Map then only option remains is to get the keys, sort them and then process data in that order. Since keys are unique in Map, it returns a Set of keys, which means you cannot sort them by using Collections.sort() method, which accept a List. So what to do? Well, we can convert our Set into List as shown in this example, then sort them using sort() method in any order and process them accordingly.

10 Points about Enum in Java

Apart from Class and Interface, Enumeration type or Enum is another popular type in Java programming language. Enum allows you to represent fixed number of things in a type-safe manner e.g. days of the week, days of the month, planets in solar system, buttons on remote control, keys on keyboard and suits on playing cards. Before Enum was introduced, prior to Java 1.5, integer and string constants are used to represent fixed number of things, known as enum int pattern and enum string pattern. Though they serve the purpose, they had some major drawbacks, one of them was type-safety i.e. you cannot restrict them to represent fixed number of values  e.g. an integer constant, which is representing days of the week can have value as 100, which is incorrect, given we have only 7 days in a week.

Array length vs ArrayList Size in Java

One of the confusing part in learning Java for a beginner to understand how to find the length of array and ArrayList in Java? Main reason for the confusion is an inconsistent way of calculating length between two. Calling size() method on arrays and length, or even length() on ArrayList is common programming error made by beginners. The main reason for confusion is the special handling of an array in Java.  Java native arrays have built-in length attribute but no size() method while the Java library containers, known as Collection classes e.g. ArrayList<>, Vector<>, etc,  all have a size() method. There is one more thing which adds to this confusion, that is capacity, at any point capacity of any collection class is the maximum number of elements collection can hold. the size of collection must be less than or equal to its capacity.

Java 8 Stream Examples and Tutorial

It's been a couple of weeks Java 8 has released and a lot of Java developers are trying their hands on major enhancement e.g. lambda expression, method reference, new data and time classes, and more importantly Stream API for bulk data operations. In my opinion, the best way to learn any new feature or functionality is by writing short examples, and this is what prompt me to write this article. In this Java 8 tutorial, I have shared simple examples of java.util.Stream package. Streams are one of the most important addition on JDK, it allows you to leverage other changes e.g. lambda expression, method reference, functional interface and internal iteration introduced via forEach() method. Some of the most common things we do with Streams are filtering a collection, applying map and reduce function on all elements of the collection and taking advantage of lazy evaluation, built-in parallelism via parallelStream().

How to Make Executable JAR file in Eclipse IDE - Java

If you are Java programmer then you know what is the purpose of JAR file, but for those who are unaware, JAR file is deliverables of Java application. Just like C and C++  application produce EXE files, Java produces JAR files. In other words, A JAR (Java Archive) file is a ZIP format file that bundles Java classes into a single unit, it may contain all the resource needed by Java application as well. There are mainly two types of JAR file in Java:  Library JAR (normal JAR) files: JARs which are reusable libraries e.g. Apache commons JAR file, guava.jar itself or even JDBC drivers e.g. ojdbc6_g.jar. There is another type as well, Executable JAR files: JARs which can be executed as a standalone Java applications. The main difference between normal and executable JAR file is that later contains a mainfest file, which specifies a main-class entry. When you run that JAR file, Java starts your application by reading that main-class entry, because you need the main method to execute Java programs.

Difference between Polymorphism and Inheritance in Java and OOP

Programmers often confused among different object oriented concepts e.g. between Composition and Inheritance, between abstraction and encapsulation and sometime between Polymorphism and Inheritance. In this article, we will explore third one, Polymorphism vs Inheritance. Like in real world, Inheritance is used to define the relationship between two classes. It's similar to Father-Son relationship. In object oriented programming, we have a Parent class (also known as the super class) and a Child class (also known as the subclass). Similar to the real world, Child inherits Parents qualities, e.g. it's attribute, methods and code. Inheritance is actually meant for code-reuse. A child can reuse all the codes written in Parent class, and only write code for behaviour which is different than the parent. Though it’s possible to restrict something to parent itself by using the private and final keyword in Java.On the other hand, Polymorphism is an ability of Object to behave in multiple form.