Java Articles - Page 107 of 745

Java program to convert collection into array

Aishwarya Naglot
Updated on 24-Jun-2025 13:23:12

1K+ Views

The Collection is a framework that provides architecture to store and manipulate a group of objects. Java Collections can achieve all the operations that you perform on data, such as searching, sorting, insertion, manipulation, and deletion. In this article, we will understand how to convert a collection into an array in Java. Following are the ways to convert a collection into an array in Java: Using Iteration Using toArray() Method Using Streams Collection to Array using Iteration The naive way to convert a collection into an array is by traversing the collection and adding each element in the ... Read More

Java Program to Check if a string contains a substring

Aishwarya Naglot
Updated on 04-Aug-2025 18:51:48

3K+ Views

A string is a class in Java that stores a series of characters enclosed within double quotes, and a continuous sequence of characters within that string is known as a substring. Those characters are string-type objects. In this article, we will learn how to write Java programs to check if a string contains a substring or not.  Java Program to Check if a String Contains a Substring We can check whether the given string contains a substring or not in the following ways: Using indexOf() Method Using contains() Method ... Read More

Java Program to Shuffle the Elements of a Collection

Aishwarya Naglot
Updated on 24-Jun-2025 12:37:37

412 Views

The Collection is a framework that provides an architecture to store and manipulate a group of objects. In Java, Collections can perform all the operations that you perform on data, such as searching, sorting, insertion, manipulation, and deletion. In this article, we will learn how to shuffle the elements of a collection in Java. Shuffling refers to randomly rearranging the elements of a collection. Let's take an example as follows: Input: list: [Java, program, is, fun, and, easy] Output: list: [fun, easy, Java, program, and, is] Ways to Shuffle Elements of a Collection in Java ... Read More

Java Program to Format time in AM-PM format

Shriansh Kumar
Updated on 01-Aug-2024 10:59:17

2K+ Views

Time formatting is a process of converting date and time values into human-readable strings with specific formats. The resulting string describes how date/time values should be represented (such as flat files or human-readable output). In this article, we will understand how to format time in AM-PM format. In 12-hour clock system, AM-PM format is used to define time. Here, AM stands for Ante meridiem which refers to the time before noon, whereas PM stands for Post meridiem which shows the time period after noon. Example Scenario Input: current_date = Thu Mar 17 16:04:31 IST 2024 Output: Current Time ... Read More

Java Program to Get the Size of the Collection

Aishwarya Naglot
Updated on 21-Aug-2025 12:32:32

218 Views

The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. In this article we will look at how to get the size of a collection in Java. The size of a collection is the number of elements it contains. Following are the ways to get the size of a collection in Java: Using size() Method Using loops Using Streams API Getting Size of Collection Using size() Method The size() method is ... Read More

Java Program to Convert a String into the InputStream

Shriansh Kumar
Updated on 30-May-2025 17:14:32

2K+ Views

To convert a given string into an InputStream, Java provides the ByteArrayInputStream class that is used along with a built-in method named getBytes(). While displaying a long input string, we are sometimes required to process it in smaller units. At that time, converting that string into an InputStream will be helpful. In this article, we will learn how we can use the ByteArrayInputStream class to convert a string into an InputStream with the help of example programs. Before discussing that, we need to learn about the InputStream and BufferedReader classes and the getBytes() method first: InputStream Class There are two fundamental ... Read More

Java program to compare elements in a collection

Aishwarya Naglot
Updated on 26-Aug-2025 17:18:53

831 Views

In Java, the Collection framework provides an architecture to store and manipulate a group of objects. Collections support operations such as searching, sorting, insertion, manipulation, and deletion. In this article, we will learn how to compare elements in a collection in Java using different approaches. Before learning about the methods, let us understand the problem with the help of the following input-output scenarios: Scenario 1 Input: ["Java", "Python", "JavaScript", "C++"] Element to Compare: "Java" Output: Java is equal to Java Python is not equal to Java JavaScript is not equal to Java C++ is not equal to ... Read More

Java program to print a collection

AmitDiwan
Updated on 20-Sep-2024 21:42:34

938 Views

In this article, we will understand how to print a collection in Java. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. Problem Statement Write a program in Java to print a Collection. Below is a demonstration of the same − Input  Run the program Output The Elements of the collection are: Language : Java | Language_id : 101 Language : Scala | Language_id : 102 Language : Python | Language_id : ... Read More

Java program to lookup enum by string value

Aishwarya Naglot
Updated on 01-Sep-2025 12:56:48

912 Views

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). For example, we have constants like week days, months, or set of languages etc. In Java, we can create an enum to represent these constants. In this article, we will learn how to lookup an enum by its string value in Java. Java Program to Lookup Enum by String Value lets learn how you can convert a string to its corresponding enum value using the following ways and also print the enum value: Using valueOf() Method Using EnumSet and stream() Using ... Read More

Java Program to Implement switch statement on strings

AmitDiwan
Updated on 30-Mar-2022 06:08:59

293 Views

In this article, we will understand how to implement switch statement on strings. The switch expression is evaluated once. The value of the expression is compared with the values of each case. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).Below is a demonstration of the same −Suppose our input is −Input string: JavaThe desired output would be −Using siwtch cases: We shall use Java for our codingAlgorithmStep 1 - START Step 2 - Declare a string namely input_string. Step 3 - Define the values. Step 4 - Define a stwtch statement ... Read More

Advertisements