Found 33676 Articles for Programming

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

812 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

918 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

895 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

282 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

Java Program to Replace the Spaces of a String with a Specific Character

AmitDiwan
Updated on 30-Mar-2022 05:49:41

1K+ Views

In this article, we will understand how to replace the spaces of a string with a specific character. 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: Java Program is fun to learn Input character: $The desired output would be −The string after replacing spaces with given character is: Java$Program$is$fun$to$learnAlgorithmStep 1 - START Step 2 - Declare a string namely input_string, a char namely input_character. Step 3 - Define the values. Step 4 - Using the function replace(), replace the ... Read More

Java Program to Join Two Lists

AmitDiwan
Updated on 29-Mar-2022 13:50:24

344 Views

In this article, we will understand how to join two lists. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.Below is a demonstration of the same −Suppose our input is −First list is defined as: [Java, Python] Second list: [Mysql, Redshift]The desired output would be −The list after joining two strings: [Java, Python, Mysql, Redshift]AlgorithmStep 1 - START Step 2 - Declare two lists ... Read More

Java Program to Access elements from a LinkedList

Aishwarya Naglot
Updated on 20-Aug-2025 12:26:31

454 Views

The Linked List in a Java is a data structure that is used for storing data in linear order. It has a series of node where each node contains two parts: data and a reference to the next node in the list. The first node is called the head and the last node is called the tail. In this article, we will learn how to access elements from a LinkedList in Java. Following are the ways to access elements from a LinkedList in Java: Using get() Method Using getFirst() Methods Using getLast() Method Accessing Elements from a LinkedList ... Read More

Java Program to Add elements to a LinkedList

AmitDiwan
Updated on 10-Aug-2023 12:14:07

369 Views

LinkedList is a generic class of Java Collection Framework that implements three interfaces namely List, Deque, and Queue. It provides the features of a LinkedList data structure which is a linear data structure where each element are linked with each other. There are several operations we can perform on LinkedList including appending, removing and traversing of elements. To add elements to a LinkedList Collection we can use various built-in methods such as add(), addFirst() and addLast(). We are going to explore how we can use these methods to add elements to a LinkedList. Adding elements to a LinkedList in Java ... Read More

Java Program to Find Common Elements in Two ArrayList

AmitDiwan
Updated on 04-Jul-2024 16:59:09

3K+ Views

In this article, we will learn how to find common elements in two array-list. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arraysthat can grow as needed. Problem Statement In the given array, we aim to find the common elements in two ArrayList objects in Java, array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Below is a demonstration of the same − Input − First list: [Java, Scala, Shell, JavaScript] Second list: [Java, Python, Shell] Output ... Read More

Advertisements