Sometimes, we need to merge multiple lists into one before performing any operation, say Iteration or transformation. It's quite common to merge two lists, or combine them into a bigger list and there are multiple ways to do it. In this article, we will take a look at two simple way to join two lists in Java, you can further extend that idea to join any number of List or it's implementation e.g. ArrayList or LinkedList in Java. One way to merge multiple lists is by using addAll() method of java.util.Collection class, which allows you to add the content of one List into another List. By using the addAll() method you can add contents from as many List as you want, it's the best way to combine multiple List.
Saturday, July 19, 2025
Saturday, July 12, 2025
Binary Search vs Contains Performance in Java List - Example
There are two ways to search an element in a List class, by using contains() method or by using Collections.binarySearch() method. There are two versions of binarySearch() method, one which takes a List and Comparator and other which takes a List and Comparable. This method searches the specified list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the natural ordering of its elements (as by the sort(List) method) prior to making this call. If List is not sorted, then results are undefined. If the List contains multiple elements equal to the specified object, there is no guarantee which one will be returned. This method runs in log(n) time for a "random access" list (which provides near-constant-time positional access).
Labels:
coding
,
core java
,
java collection tutorial
,
Java Programming Tutorials
Friday, June 27, 2025
2 Examples to Convert Byte[] Array to String in Java
Converting a byte array to String seems easy but what is difficult is, doing it correctly. Many programmers make the mistake of ignoring character encoding whenever bytes are converted into a String or char or vice versa. As a programmer, we all know that computer's only understand binary data i.e. 0 and 1. All things we see and use like images, text files, movies, or any other multi-media are stored in form of bytes, but what is more important is the process of encoding or decoding bytes to a character. Data conversion is an important topic in any programming interview, and because of the trickiness of character encoding, this question is one of the most popular String Interview questions on Java Interviews.
Labels:
Array
,
core java
,
data structure and algorithm
,
Java Programming Tutorials
,
programming
,
String
Wednesday, June 11, 2025
How to Generate MD5 Hash in Java - String Byte Array Digest Example
There are multiple ways to generate the MD5 hash in Java program. Not only Java API provides a convenient method for generating MD5 hash, you can also use popular open-source frameworks like Spring and Apache commons Codec to generate MD5 digest in Java. MD5 is a popular Message-Digest Algorithm, which is most commonly used to check data integrity e.g. comparing MD5 checksum to see, if any file is altered or not. Though MD5 has not considered a good cryptographic algorithm for security purposes due to several vulnerabilities found on it, it's still good enough or checking the integrity of the file. MD5 hashing algorithm generates a 128 bit or 16-byte long hash value.
Labels:
Array
,
core java
,
data structure and algorithm
,
Java Programming Tutorials
,
programming
Wednesday, June 4, 2025
Top 23 Design Patterns Experienced Java Programmers Should Learn
Hello guys, if you want to learn Design Patterns and looking for a comprehensive tutorial which covers all important object oriented design pattern then you have come to the right place. Earlier, I have shared best Design pattern courses and books for experienced developers and in this article, I will introduce you with 23 common object oriented design patterns for writing better code. Design Patterns are tried and tested way of solving a problem within a given context. They are not invented, rather they are discovered, which is also obvious by use of word pattern. By using design pattern, you take knowledge of all wide communities and can safely use it to solve that problem. At times they might not give you perfect solution, require bit of tweaking, but they certainly provides one of the best solution, as long as you use them properly. While doing object oriented development, they are numerous task which appears quite often e.g. creating objects, structuring code, and implementing different behaviors based on different context.
Saturday, April 12, 2025
How to Reverse an Array in Java? Integer and String Array Example Tutorial
This Java tip is about, how to reverse an array in Java, mostly primitive
types e.g. int, long, double and String arrays. Despite
of Java’s rich Collection API, use of array is quite common, but standard JDK
doesn’t have great utility classes for Java. It’s difficult to convert between
Java Collection e.g. List,
Set
to primitive arrays. Java’s array utility class java.util.Arrays, though
offers some the critical functionalities like comparing
arrays in Java and support
to print arrays, It lacks a lot of common features, such as combining
two arrays and reverse primitive or object array.
Labels:
Array
,
coding
,
core java
,
data structure and algorithm
,
Java Programming Tutorials
Wednesday, April 9, 2025
How to Format and Display Number to Currency in Java - Example Tutorial
Displaying financial amounts in the respective currency is a common requirement in Java-based E-commerce applications. For example, if you are selling products on-line globally, you will show price of products in their local currency rather than USD or some other currency. Storing price in every currency is not a good option because of maintenance, and more realistically fluctuation in exchange rates. That's why many of these applications prefer stores price of books, electronic goods, or whatever product they are selling in USD, and the responsibility of converting that price to local currency and displaying is left to client-side code. If your client is a Java-based client e.g. Swing GUI, Java FX client, or a JSP web page, you can use java.text.NumberFormat class to format currency in Java.
Labels:
core java
,
Java Programming Tutorials
,
programming
Tuesday, April 8, 2025
2 Ways to Combine Arrays in Java? Integer, String Array Copy Example
There are multiple ways to combine or join two arrays in Java, both for primitive like int array and Object e.g. String array. You can even write your own combine() method which can use System.arrayCopy() to copy both those arrays into the third array. But being a Java developer, I first looked in JDK to find any method which concatenates two arrays in Java. I looked at java.util.Arrays class, which I have used earlier to compare two arrays and print arrays in Java, but didn't find a direct way to combine two arrays. Then I looked into Apache Commons, ArrayUtils class, and bingo, it has several overloaded methods to combine int, long, float, double, or any Object array.
Labels:
Array
,
coding
,
core java
,
data structure and algorithm
,
Java Programming Tutorials
Friday, March 1, 2024
2 ways to Split String with Dot (.) in Java using Regular Expression? Examples
You can use the split() method of java.lang.String class to split a string based on the dot. Unlike comma, colon, or whitespace, a dot is not a common delimiter to join String, and that's why beginner often struggles to split a String by dot. One more reason for this struggle is the dot being a special character in the regular expression. If you want to split String on the dot you need to escape dot as \\. instead of just passing "." to the split() method. Alternatively, you can also use the regular expression [.] to split the String by a dot in Java. The dot is mostly used to get the file extension as shown in our example.
Saturday, September 23, 2023
3 ways to parse String to float in Java? Examples
float and double are two data type which is used to store floating-point values in Java and we often need to convert String to float in Java and sometimes even a Float object or float primitive to String. One thing, which is worth remembering about floating-point numbers in Java is that they are approximate values, a float value 100.1f may hold the actual value of 100.099998, which will be clear when we have seen examples of converting float to String and vice-versa. By the way, It's easy to parse String to float and vice-versa, as rich Java API provides several ways of doing it.
Sunday, August 13, 2023
Mortgage Calculator in Java: A Step-by-Step Guide with Code and Example
Hello guys, its been long time since I shared a Java programming exercise so today I am going to share a popular one, how to create a Mortgage calculator in Java. A mortgage calculator is a powerful tool that allows individuals to estimate their monthly mortgage payments based on factors such as loan amount, interest rate, and loan term. So, its not just a Java programming exercise but a useful tool to calculate mortgage payment for your home loan, car loan or business loans. In the past, I have shared top 50 Java programs from coding interviews as well as 10 Java programming exercises and In this article, we'll walk you through the process of creating a simple mortgage calculator using Java. We'll cover the necessary concepts and provide you with a complete Java code example to help you get started.
Labels:
Coding problems
,
homework
,
Java Programming Tutorials
Wednesday, July 26, 2023
3 Examples to Generate Random Alphanumeric String in Java - UUID Example
Many times we need random alphanumeric String for one or another purpose and Thankfully Java provides a couple of ways to create random Strings which can be numeric, alphanumeric, or simply alphabetic. If you are familiar with Java API to generate random numbers like java.util.Random class and Math.random() method than It's quite easy to create an array with choice of characters like Alphanumeric, numeric, alphabets, special characters, and choose random characters to construct random String. If you like using an open-source library and happen to be using Apache commons-lang then generating an alphanumeric String is just one line of code as shown in the first example in our Java program.
Labels:
core java
,
Java Programming Tutorials
Thursday, May 25, 2023
Difference between valueOf and parseInt method in Java? Example
Both valueOf and parseInt methods
are used to convert String to Integer in Java, but there is subtle differences
between them. If you look at the code of valueOf() method, you
will find that internally it calls parseInt() method to convert String to Integer, but
it also maintains a pool of Integers from -128 to 127 and if the requested integer
is in the pool, it returns an object from the pool. This means two integer objects
returned using the valueOf() method can be the same by the equality operator. This
caching of Immutable object, does help in
reducing garbage and help garbage collectors.
Labels:
coding
,
core java
,
Java Programming Tutorials
Thursday, April 13, 2023
3 Examples of Parsing HTML File in Java using Jsoup
HTML is the core of the web, all the pages you see on the internet are HTML, whether they are dynamically generated by JavaScript, JSP, PHP, ASP or any other web technology. Your browser actually parse HTML and render it for you. But what would you do, if you need to parse an HTML document and find some elements, tags, attributes or check if a particular element exists or not from Java program. If you have been in Java programming for some years, I am sure you have done some XML parsing work using parsers like DOM and SAX, but there is also good chance that you have not done any HTML parsing work. Ironically, there are few instances when you need to parse HTML documents from core Java application, which doesn't include Servlet and other Java web technologies.
Labels:
coding
,
Java and HTML
,
Java Programming Tutorials
,
Jsoup
Monday, August 15, 2022
Object Oriented Programming Example in Java? Tutorial
I remember when I was doing my engineering, the OOPS concept was in the first semester. Back then it looks like some alien things to me, because I have never done programming and even the smallest program we get to write like checking whether the number is palindrome or not, or printing the Fibonacci series, we never really needed to use OOPS. When needed, I just memorize things and write them down in exams :). That worked, I passed the exam but effectively I didn't learn OOP or Object Oriented Programming on those days.
Monday, September 20, 2021
How to set Java Path and Classpath in Windows 7, 8 and Windows 10 - Tutorial
So, you just bought a new PC or Laptop with Windows 8 operating system, and wondering how to set PATH and Classpath on Windows 8; Or, you might have just upgraded your Windows 7 laptop to the professional edition of Windows 8 and looking to set JDK Path to compile Java programs. Not to worry, this is the second step for anyone who wants to learn Java programming. Of course, the first step is to install JDK. In this Java tutorial, we will see step by step guide to set Java PATH and CLASSPATH in the Windows 8 operating system. By the way, if you are learning Java in Windows 10 operating system, You should still be able to set the Java path and classpath in Windows 10 by following the steps given here, because navigation steps for modifying environment variables on Windows 10 and Windows 8 are almost same.
Labels:
core java
,
Java Programming Tutorials
,
windows 8
Wednesday, September 8, 2021
10 Difference between Primitive and Reference variable in Java - Example Tutorial
There are two types of variables in Java, primitive and reference type. All the basic types e.g. int, boolean, char, short, float, long and double are known as primitive types. JVM treats them differently than reference types, which is used to point objects e.g. String, Thread, File, and others. Reference variables are not pointers but a handle to the object which is created in heap memory. The main difference between primitive and reference type is that primitive type always has a value, it can never be null but reference type can be null, which denotes the absence of value. So if you create a primitive variable of type int and forget to initialize it then it's value would be 0, the default value of integral type in Java, but a reference variable by default has a null value, which means no reference is assigned to it.
Labels:
core java
,
Java basics
,
Java Programming Tutorials
Friday, August 13, 2021
How to do static import in Eclipse - Java Example Tutorial
Do you know what is a shortcut to doing static import in Eclipse? Well, I didn't know before, but today I come to know that shortcut Ctrl+Shift+M (Source > Add Import) can not only be used to add missing imports but can also help with static import in the Java program. Suppose you are using lots of static variables from a utility class e.g. TimeUnit by referring them with the class name, just like we refer to static variables. In Eclipse IDE, you can select the whole reference variable and press Ctrl+Shift+M and it will automatically import that static element using static import in Java.
Labels:
core java
,
Eclipse
,
Java Programming Tutorials
,
programming
Saturday, July 31, 2021
When to Make a Method Static in Java? Example
Making a method static in Java is an important decision. Though, static keyword is one of the fundamental concepts, many times programmers get confused to make a particular method static or not. In Java programming, the main motivation for making a method static is convenience. You can call a static method without creating any object, just by using its class name. So if you need a method, which you want to call directly by class name, make that method static. Utility classes e.g. java.lang.Math or StringUtils are good examples of classes, which use static methods. Before making a method static, you should look into the limitations of static methods as well, as you can not override static methods in Java.
Labels:
core java
,
Java Programming Tutorials
Tuesday, July 27, 2021
How to Install JDK on Windows - Java Programming Tutorial Example
Installing JDK is the first step in learning Java Programming. If you are
using Windows 8 or Windows 7 Operating System, then installing JDK is quite
easy as you just need to follow instructions given by the Java SE Installation
wizard. The only thing which requires some attention is, choosing the correct JDK
installer based upon, whether you are running with 32-bit or 64-bit Windows 8
or Windows 7 OS. JDK 7 is the latest Java version but JDK 6 is still most popular
in the software and programming world. You can choose to install JDK 7 or JDK 6 based
on your course material.
Labels:
core java
,
Java Programming Tutorials
,
windows 8
Subscribe to:
Comments
(
Atom
)