Java Examples

In this tutorial, we will explore different methods to find duplicate elements in a Java array, including using nested loops, sets, streams, and hash tables. We will also discuss the time and space complexity of each method, so you can choose the most efficient one for your specific use case. Whether you’re a beginner or…

Read More How to Find Duplicate Elements in a Java Array

class EscapeDoubleQuotesInString { public static void main(String[] args) { String str = “Hello, I’m \”Steve\” and I am \”Java\” developer”; System.out.println(str); } } Output: Hello, I’m “Steve” and I am “Java” developer Escape double quotes in a JSON object in Java There are many cases where you might need to escape double quotes. In this…

Read More Escape Double Quotes in Java String

<dependency> <groupid>org.apache.poi</groupid> <artifactid>poi-ooxml</artifactid> <version>5.0.0</version> </dependency> If you are not familiar with Maven, you can check out this tutorial: Create Java Project with Maven. import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Product { private int id; private String color; private String size; private int cost; private String description; } import org.apache.poi.ss.usermodel.Cell;…

Read More Read and write Excel files in Java

Want to learn how to merge two ArrayLists in Java? In this post, we will cover the following ways: Using the List.addAll() method Using the Java 8 Streams Using the ListUtils.union() method Merge two ArrayLists in Java using the List.addAll() method There is addAll() method from the List interface that appends all of the elements in…

Read More How to merge two ArrayLists in Java?

In this post, you will learn how to Serialize and Deserialize an ArrayList in Java using the FileOutputStream and FileInputStream classes. How to Serialize an ArrayList in Java? Serialization is the process of changing the state of an object into a byte stream. For example, we use it to write some Java objects into a file. In…

Read More Serialize and Deserialize an ArrayList in Java

Need to remove a character from String in Java?  You can do that by converting the String to a StringBuilder class and using the deleteCharAt() method that removes the character at a specified position. Example class Test { public static void main(String[] args) { String str = “Learn Java”; str = new StringBuilder(str).deleteCharAt(5).toString(); System.out.println(str); } }…

Read More Remove a Character from String in Java

In one of the previous examples, we covered how to sort an ArrayList in ascending order. In this post, you will learn how to sort ArrayList in descending order in Java. We will explore the following ways: Using the sort() method Using the Collections.sort() and Collections.reverse() methods Sort ArrayList in descending order in Java using the…

Read More Sort ArrayList in descending order in Java

Want to learn how to count character occurrences in Java String? Below is the program that uses a Map to store chars as keys and the number of occurrences as values. class Test { public static void main(String[] args) { String str = “moiujjmhrqdoasdsooqavvae”; Map<Character, Integer> map = new HashMap<>(); for (int i = 0;…

Read More Count Character occurrences in Java String

In this post, you will learn to check if a String contains only digits in Java. We will cover two methods: Using the Character.isDigit() method Using the Integer.parseInt() method Check if a String contains only digits in Java using the Character.isDigit() method In this example, we use a for loop to iterate over the elements…

Read More Check if a String contains only digits in Java

We often need to compare two ArrayList in Java. In this post, you will learn how to do that in two ways: Using the ArrayList.equals() method Using the CollectionUtils.isEqualCollection() method Compare two ArrayList in Java using the ArrayList.equals() method The ArrayList.equals() method compares two ArrayList. It compares the sizes of the lists, elements equality, and the…

Read More Compare two ArrayList in Java