Wednesday, June 29, 2022

Difference between HashMap and ConcurrentHashMap in Java? Example

HashMap vs ConcurrentHashMap in Java
What is the difference between an HashMap and ConcurrentHashMap in Java is one of the common interview questions and knowing the answer is not just important for interviews but also for writing robust and high performance Java cocd. ConcurrentHashMap in Java is introduced as an alternative of Hashtable in Java, which is a synchronized collection class, that makes the main difference between HashMap and ConcurrentHashMap which is one is non-synchronized , non-thread safe and not for use in Concurrent multi-threaded environment while ConcurrentHashMap is a thread-safe collection and intended to be used as primary Map implementation especially for multi-threaded and Concurrent environment. 

How to iterate over an Array in Java using foreach loop Example Tutorial

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 have to perform some operation on them e.g. filtering, transformation, or simply printing, the foreach loop comes to its full glory. There is not much difference between traditional for loop and Java 1.5 foreach loop, except that the former has counter and condition, which is checked after each iteration.

Difference between List and Set in Java [Answered]

List vs Set in Java
The main difference between List and Set is that List is an ordered Collection while Set is an unordered collection. Java collection framework offers several collection classes for various needs but all collections can be divided into broadly three categories: List, Set, and Map. All List, Set, and Map are defined as interfaces and then you have several implementations like ArrayList and Vector are the popular implementation of List interface, while HashSet is a popular implementation of the Set interface. 

Monday, June 27, 2022

Top 5 Books to learn Core Java for Beginners in 2024 - Best of lot

I receive half a dozen emails every day asking about which is the best to learn Java from scratch? Which book should I read in 2024 to learn Java? Or Which is the best Java for beginners? When I receive such an email, before giving my recommendation, I ask two questions, first do you have any programming experience? like, Are you coming from C, C++, or Python background? Or you are a fresher with no experience in programming, just a couple of classes on data structure and algorithm and operating systems in college.

Sunday, June 26, 2022

2 Ways to Remove Elements/Objects From ArrayList in Java [Example]

There are two ways to remove objects from ArrayList in Java, first, by using the remove() method, and second by using Iterator. ArrayList provides overloaded remove() method, one accepts the index of the object to be removed i.e. remove(int index), and the other accept objects to be removed, i.e. remove(Object obj). The rule of thumb is, If you know the index of the object, then use the first method which accept index, otherwise use the second method. By the way, you must remember to use ArrayList remove methods, only when you are not iterating over ArrayList if you are iterating then use Iterator.remove() method, failing to do so may result in ConcurrentModificationException in Java. 

Top 7 Online Courses to Learn Spring Framework in 2024 - Best of Lot

Hello Java developers, if you want to learn Spring Framework and look for the best resources like books, online courses, and tutorials, you have come to the right place. Earlier, I have shared the free Spring Framework courses, best spring framework books, and best Spring Boot courses, and today, I Am going to share the best Spring Framework courses for Java developers. It is one of the world's most popular development frameworks for using enterprise Java. It is used by millions of developers for creating easily testable, reusable, and high-performance code, and it is also one of the essential skills for Java developers. If you are not familiar with the Spring framework, this should be the first thing you should learn in 2024. 

Friday, June 10, 2022

How to swap two Integers without using a temporary variable in Java?

One of the oldest trick questions from a programming interview is, How do you swap two integers without using a temp variable? This was first asked to me on a C/C++ interview and then several times on various Java interviews. The beauty of this question lies both in the trick to thinking about how you can swap two numbers without the third variable, but also problems associated with each approach. If a programmer can think about integer overflow and consider that in its solution then it creates a very good impression in the eye of interviewers.

How to check if File is Empty in Java? Example Tutorial

One of the readers of my blog Javarevisited emailed me yesterday about how to check if a file is empty in Java and that's why I am writing this post to show that using an example. The common approach for checking if a file is empty or not is to first check if the file exists or not and then check if it contains any content, but Java has done that hard work for you. Well, it's pretty easy to check emptiness for a file in Java by using the length() method of the java.io.File class. This method returns zero if the file is empty, but the good thing is it also returns zero if the file doesn't exist. This means you don't need to check if the file exists or not.

Wednesday, June 8, 2022

[Solved] How to Print Alphabets in Upper and Lower Case in Java? Example Tutorial

Hello guys, if you are learning Java and looking for basic programming exercise to build your concepts then you have come to the right place. Earlier, I have shared 15 common Java programming exercises and today, you will learn one of them. One of the textbook exercises to get started with any programming language is writing a program to print alphabets in both upper and lower case. This program allows you to explore the String class in Java with the toUpperCase() and toLowerCase() method but normally when you start, it's asked to do this without any API methods.

Tuesday, June 7, 2022

[Solved] How to count Vowels and Consonants in Java String Word? Example

Hello guys, if you are wondering how to count number of vowels and consonants in a given word in Java then you have come to the right place. In the past, I have shared 100+ data structure questions and more than 75 programming interview questions, and In this article, we will take on a popular programming exercise of counting vowels in a word. You need to write a Java program to count how many vowels in a String, which is entered from the command prompt. It's similar to the program of counting the occurrence of characters in a String, in fact, it's a special case, where you need to count occurrences of all vowels, which includes five characters a, e, i, o and u

[Solved] How to convert Hexadecimal to Decimal, Binary and Octal in Java? Example

Hexadecimal to decimal and binary conversion in Java
Hello guys, if you are wondering how to convert a Hexadecimal number to decimal and binary number in Java then you have come to the right place. Earlier, I have shown you how to convert decimal to binary in Java and in this Java program will convert Hexadecimal number to decimal, binary and Octal in Java programming language using JDK standard API methods. For beginners who are not familiar with number system, hexadecimal system is base 16 number, while decimal is base 10, Octal is base 8, and binary is base 2 numbers in Number systems. 

How to create a Function to add two numbers in Java? Variable Argument Example Tutorial

How to add two numbers in Java
Hello guys, if you are just starting with Java programming and wondering how to write a function to add two numbers in Java then you have come to the right place. In this Java program tutorial, we will add two numbers provided by the user on the command line. Write a Java program to add two numbers in Java is a common programming exercise for programmers and mostly asked in college exams, school homework, and Java courses as well. 

Monday, June 6, 2022

How to Convert Stream to ArrayList in Java 8 - Collectors.toCollection() Example

You can use Collectors.toList(), toSet(), and toMap() to get all elements of Stream into any Collection like List, Set or Map, but if you want to get a particular collection e.g. ArrayList, then you need to use Collectors.toCollection(ArrayList::new) method. This method first creates an ArrayList using method reference and then adds all elements of Stream into the ArrayList. It's very useful if you have a long list of String and you want to create a smaller list containing only String starting with the letter "b"  like "Bluehost"

Sunday, June 5, 2022

3 Examples to Loop over a List in Java - ArrayList, LinkedList or Vector

3 ways to Loop through a List in Java
There are multiple ways to traverse or loop through a List in Java e.g. by using an Iterator, by using an enhanced for loop of Java 5, and not the forEach() method of Java 8. Given a List is an index-based collection if you know the index you can retrieve an object from a List and because of this, you can also use a traditional for loop which keeps count for iterating a List. Now the question is whether should you use the Iterator or enhanced for loop, or the forEach() method of Java 8 for looping over List in Java. 

RegularEnumSet vs JumboEnumSet in Java

Hello guys, the difference between RegularEnumSet and JumboEnumSet in Java was asked in a recent Java interview with one of my friends, unfortunately, he hasn't explored this topic well and couldn't answer this question precisely, but he made sure to learn about EnumSet after that. When he discussed this topic with me, I really liked it because, despite the usefulness of EnumSet and its fast implementation, not many developers know about it, despite being mentioned in Java's classic Effective Java. This makes me write this post, where we will mainly discuss a couple of differences between RegularEnumSet and JumboEnumSet in Java, but we will also touch base upon some of the important properties of EnumSet.

Saturday, June 4, 2022

What is fail safe and fail fast Iterator in Java?

Java Collections supports two types of Iterator, fail safe and fail fast. The main difference between a fail fast and fail safe Iterator is whether or not the underlying collection can be modified while its begin iteration. Fail safe Iterator allows that while fail fast Iterator doesn't, they throw ConcurrentModificatoinException when something goes wrong during iteration, except certain operations like removing an element using Iterator's remove() method. This behavior is implemented with a variable called modCount which keeps track of modification done on underlying collection. 

Wednesday, June 1, 2022

How to convert an Array to Collection like Set and List in Java? Example Tutorial

Hello Java programmer, if you are wondering how to convert an Array to Collection like Set or List in Java then you have come to the right place. In the past, I have shared how to convert ArrayList to HashMap in Java and In this example, we will learn how to convert a String array to Collection, Set or List in Java. You can convert an Array to Collection using helper method Arrays.asList() in Java. This is shortcut to convert array to List , Set or any Collection. This knowledge of converting an array to Collection can be really useful to any Java developer, as most legacy code tend to use an array, which means you either need to pass them your input as array or they return the result as an array. 

[Solved] How to solve climbing stairs problem in Java? Example

In this article, we are going to be solving the stairs problem using java, After that, I shall be explaining the concept of Array. but before we start writing codes it is fine to have an understanding of the problem we are about to solve. Given the number of stairs and the number of times you can move your legs, we would see how many possible ways you can achieve the climbing of stairs. The image below shows different possible ways you can climb stairs. For instance, let us say we have 5 steps on the stairs. and you can either walk with 2 moves or 1. 

How to Iterate over HashMap in Java? Map.entrySet().iterator() Example

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.