Java Articles

Page 135 of 450

What is the use of the Cleaner class in Java 9?

Alshifa Hasnain
Alshifa Hasnain
Updated on 10-Jun-2025 1K+ Views

In this article, we will learn about the use of the Cleaner class in Java 9. Below, we will first tell the basic usage and then we will know how we can use the Cleaner method practically. Garbage Collector An Object that has been created during the program execution is automatically removed by the Garbage Collector (GC). When an object not referenced by any thread and when JVM determines that this object can't be accessed, then it can be eligible for garbage collection. The finalize() method The Object class has a finalize() method, which is automatically called by GC before it attempts to remove the ...

Read More

How to save, edit, and drop a snippet in JShell in Java 9?

Alshifa Hasnain
Alshifa Hasnain
Updated on 10-Jun-2025 602 Views

Java Shell or JShell is an official REPL(Read-Evaluate-Print-Loop) introduced with Java 9. It provides an interactive shell for quickly prototyping, debugging and without the need for the main() method or the need to compile the code before executing it. JShell is easily started by typing "jshell" in the command prompt. What are Snippets? JShell allows the execution of Java statements,  variables, methods, class definitions, imports, and expressions. These pieces of Java code are referred to as snippets. Every snippet is assigned an ID(unique) that we can use later to reference it. Example Below is an example to represent a snippet ...

Read More

When to use the ofNullable() method of Stream in Java 9?

Alshifa Hasnain
Alshifa Hasnain
Updated on 10-Jun-2025 2K+ Views

In this article, we will learn to use the ofNullable() method of Stream in Java 9. We will first go through the Stream class, after that we will learn about the ofNullable() method along with its syntax and real-world use cases. Stream Class Stream was introduced in Java 8, the Stream API is used to process collections of objects. It represents a sequence of elements that support various operations to perform computations in parallel. Creating an instance of a stream: Stream stream; A stream is not a type of data structure, it takes input from Collections, Arrays, etc. The ofNullable() ...

Read More

How To Get the Arc Length from Given Angle in Java?

Vivek Verma
Vivek Verma
Updated on 10-Jun-2025 651 Views

An arc of a circle is a segment or portion of the circumference of a circle. In other words, a curve between a pair of distinct points. The length of the arc refers to the length between two points along the arc (two points along a section of a circle or curve). It is a percentage of the circumference of the circle. When two lines intersect each other, the common meeting point is called a vertex and the geometrical figure between the two arms/lines is called an angle. Following diagram will help you to understand the arc and its length: ...

Read More

How to find the last occurrence of an element in a Java List?

Aishwarya Naglot
Aishwarya Naglot
Updated on 10-Jun-2025 4K+ Views

In this article, we will learn how to find the last occurrence of an element in a Java List. It is a collection in which we can store and access elements in serial order. We can use the following ways to find the last occurrence of an element in a Java List: Using a For Loop LastIndexOf() Method Using Stream API Using a For Loop We would use a for loop to iterate through the list in reverse order and check if the desiredelement is equal ...

Read More

How to determine if all elements are the same in a Java List?

Aishwarya Naglot
Aishwarya Naglot
Updated on 10-Jun-2025 2K+ Views

In this article, we will explore how to determine if all elements in a Java List are the same. The following are the ways to achieve this: Using a For Loop Using Stream API Using a For Loop We will iterate through the list and compare each element with the first element. If we find any element that is not equal to the first element, we will return false. If we reach the end of the list without finding any different element, we will return true. Example In the below ...

Read More

How do I remove multiple elements from a list in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 10-Jun-2025 5K+ Views

A List extends a collection that is used to store elements in a sequential manner. Let's learn how to remove multiple elements from a list in Java. The following are a few ways to do that: Using a for Loop Using removeAll() Method Using Stream API Using a For Loop To remove multiple elements from a list using a for loop. We will iterate through the list and check if each element is in the list of elements to be removed. If it is, we will remove ...

Read More

How do I set the size of a list in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 10-Jun-2025 9K+ Views

Java list size is dynamic. It increases automatically whenever you add an element to it, and this exceeds the initial capacity. You can define the initial capacity at the time of list creation so that it allocates memory after the initial capacity is exhausted. We can do this using the ArrayList Constructor and Collections.nCopies() method. In this article, we will explore both methods to set the size of a list in Java. Using ArrayList Constructor Using Collections.nCopies() Let's explore these methods in detail. Using ArrayList Constructor We can create an ...

Read More

Java Program to Iterate over ArrayList using Lambda Expression

Aishwarya Naglot
Aishwarya Naglot
Updated on 10-Jun-2025 3K+ Views

The ArrayList class is a resizable array that can be found in java.util package. The difference between a built-in array and an ArrayList in Java is that the size of an array cannot be modified. Lambda Expression is a feature that was introduced in Java 8. It is useful when we use collections, such as ArrayList, Set, or Map, and we want to perform operations on the elements of these collections. In this article, we will learn how to iterate over an ArrayList using a lambda expression in Java. Iterating Over an ArrayList Using Lambda Expressions There are two main ...

Read More

How To Find the Length of Hypotenuse in Java?

Vivek Verma
Vivek Verma
Updated on 10-Jun-2025 2K+ Views

Hypotenuse refers to the longest side, which is opposite to the right angle of a right-angled triangle. Following is the diagram of the Hypotenuse: Length of the Hypotenuse The length of the hypotenuse can be calculated using the Pythagorean theorem. According to it, the sum of the squares of the lengths of two sides is equal to the square of the length of the third side as follows: a2 + b2 = c2 c = √(a2 + b2) Where a, b, and c refer to the three sides of the right-angled triangle. You can write the above formula ...

Read More
Showing 1341–1350 of 4,498 articles
« Prev 1 133 134 135 136 137 450 Next »
Advertisements