java examples

When working with Java programming, it is often essential to determine whether a given string represents a numeric value. This task is crucial for a variety of scenarios, such as validating user input, performing calculations, or processing data. By accurately identifying numeric strings, you can ensure the correctness and integrity of your application’s logic. The…

Read More Java: Check if String is Numeric

Accessing elements from an ArrayList is a fundamental operation in Java programming. ArrayLists are commonly used to store and manage collections of objects dynamically. Being able to retrieve elements enables you to work with the data stored within the ArrayList effectively. Whether you need to display elements, perform calculations, or manipulate the data in any…

Read More How to Get an Element from an ArrayList in Java?

Finding the maximum and minimum values in an array is a common requirement in various programming scenarios. It allows you to extract key insights from your data, such as identifying the highest and lowest values, or determining the range of values present. This knowledge can be utilized in numerous applications, including statistical analysis, data processing,…

Read More Finding Max and Min Values in a Java Array

Working with strings is a fundamental aspect of programming, and Java provides a rich set of tools and methods for manipulating strings. One common task when working with strings is to remove whitespace characters such as spaces, tabs, and newlines. There are several ways to accomplish this in Java, including using the replaceAll() method from…

Read More Removing Whitespaces in Java Strings

class PrintArrayElementsExample { public static void main(String[] args) { int[] arr1 = {1, 7, 9, 5, 2, 8, 3}; String[] arr2 = {“Megan”, “Tom”, “Melissa”, “John”, “Steve”}; // print elements of the arr1 System.out.println(Arrays.toString(arr1)); // print elements of the arr2 System.out.println(Arrays.toString(arr2)); } } Output: [1, 7, 9, 5, 2, 8, 3] [Megan, Tom, Melissa, John,…

Read More Print Array Elements in Java

public class SplitString { public static void main(String[] args) { String names = “Tom,Steve,John,Megan,Melissa”; // split string String[] arr = names.split(“,”); // print the size of the array System.out.println(arr.length); // print the elements Stream.of(arr).forEach(System.out::println); } } Output: 5 Tom Steve John Megan Melissa   If you need a list, instead of an array, use the…

Read More Split a comma-separated String 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