Simple quick to use examples to sort Map by values, using Java 8 Stream APIs, in ascending and descending (reverse) orders. In the center of logic is the method Map.Entry.comparingByValue() which compares Map.Entry in natural order on value. Sort Map by Values using Stream APIs In java 8, Map.Entry class has static method comparingByValue() to […]
Simple quick to use examples to sort a Map by key, using TreeMap and Stream APIs, in ascending and descending (reverse) orders.
To iterate list in java is very basic operation, but over the years it’s gone through some significant changes. We will go through all these changes in given examples.
Below examples uses different techniques to iterate over java collections. Use what’s suitable and easy for you in any situation. Iterable.forEach method (Java 8) Recently introduced in java 8, This method can be called on any Iterable and takes one argument implementing the functional interface java.util.function.Consumer. e.g. Java “foreach” loop (Java 5) The “foreach” loop […]
Difference between Enumerator and Iterator can be asked to you in any java interview. In this post, I am listing down few differences which you may cite while answering the question. Difference between enumerator and iterator First of all, enumerations are only applicable for legacy classes e.g Hashtable, Vector. Enumerations were part of initial java […]
Many times you will be asked this question in your interviews. e.g. How Iterator works in java, how it remove objects from List? While removing objects from list, when it may produce ConcurrentModificationException? In Java, when you create an iterator, it starts to count the modifications that were applied on the collection. If the iterator […]
If you are preparing for a job as entry level java programmer, you can expect this interview question. Though the question becomes really easy when you know other concepts like how hashmap works, yet after getting a many queries from my blog readers who are preparing for or working at junior level, I have decided […]
Without argument, java collections is one of the most important area where you will be tested in any position whether junior or senior. The scope is so much wide, that its almost impossible to cover all the questions. Yet based on my previous interviews, I am attempting to put as many as possible GOOD java […]
In my previous post related to “How HashMap works in java“, I explained the internals of HashMap class and how they fit into whole concept. But when interviewer ask you about HashMap related concepts, he does not stop only on core concept. The discussion usually goes in multiple directions to understand whether you really understand […]
A very popular interview question indeed. It is asked immediately after “How HashMap works?”. Lets make a reasoning around a good key class design. The very basic need for designing a good key is that “we should be able to retrieve the value object back from the map without failure“, otherwise no matter how fancy […]
Vector is a another legacy implementation of List interface provided with java bundle. It is almost similar to ArrayList expect it is synchronized also. It has its own advantages as well as disadvantages e.g. Vector doesn’t need additional synchronization while accessed from multiple threads but it degrades its performance for the same reason. Here I […]
We have already learned about how HashMap in java works internally. If my last similar post, I tried to compare different “for loop” flavors available in java. These studies usually help in setting up best practices for your next project. I this post, I decided to compare traversal in hash map in java. HashMap is […]
For loop is very common control flow statement in programming languages such as java. I am not going to describe the basics of “for loop” as it is beyond the scope of this article and most of us are already well aware of this. In this post, I will list down various ways to use […]
We all know what toString() method does. It returns a string representation of the object. This representation has been made informative for most of the java classes provided in JDK bundle. But still, arrays are one of such exceptions toString() method does not provide any useful information. In this post, we will learn about JDK’s […]
Easy to follow examples of sorting a collection of objects in any order using Comparable or Comparator Interfaces.
Most of you will agree that HashMap is most favorite topic for discussion in interviews now-a-days. I have gone through several discussions with my colleagues time to time and it really helped. Now, I am continuing this discussion with you all. I am assuming that if you are interested in internal working of HashMap, you […]