Java Articles

Page 127 of 450

Java program to shift array elements to the left

Alshifa Hasnain
Alshifa Hasnain
Updated on 07-Aug-2025 1K+ Views

What Is Left Shifting in an Array? Shifting the array elements to the left by the given number of positions is also known as left rotation of the array. The element at the beginning gets pushed out of the front during the shift, but instead of being deleted, they are re-attached at the end of the array. Let us consider an example for a better understanding: Original Array: After 1 Left Shift: We can see in the above figure that, after the first shift, the element "1" is pushed out and then reattached at the end of the ...

Read More

Checking for Null or Empty in Java.

Aishwarya Naglot
Aishwarya Naglot
Updated on 04-Aug-2025 13K+ Views

A string is a collection of characters. In Java, it is represented by the String class, and it accepts NULL values. Checking for Null or Empty in Java In this article, we will learn if a string is null or empty in Java. The following are the ways to check if a string is null or empty in Java: Using isEmpty() Method Using length() Method Using isBlank() Method Using isEmpty() Method The isEmpty() method of the String class checks if a string is empty. It returns TRUE if ...

Read More

Java Program to Get the middle element of LinkedList in a single iteration

Aishwarya Naglot
Aishwarya Naglot
Updated on 04-Aug-2025 332 Views

The java.util.LinkedList class represents a linked list in Java, specifically a doubly-linked list, i.e., we can traverse through the list either from the beginning or from the end, whichever is closer to the specified index. We can create and perform various operations on a linked list using the methods provided by this class.Retrieving the Middle Element of a LinkedList  In this article, we will understand how to find the middle element of a LinkedList using Java. We will use the following ways to do so: Using a While Loop Using size() ...

Read More

Can we call a method on \"this\" keyword from a constructor in java?

Maruthi Krishna
Maruthi Krishna
Updated on 04-Aug-2025 6K+ Views

The “this" keyword in Java is used as a reference to the current object within an instance method or a constructor. Using this, you can refer to the members of a class, such as constructors, variables, and methods. Calling a Method using "this" From a Constructor Yes, as mentioned, we can call all the members of a class (methods, variables, and constructors) from instance methods or constructors. Example In the following Java program, the Student class contains two private variables, name and age, with setter methods and a parameterized constructor that accepts these two values. From the constructor, we are ...

Read More

Get Size of Java LinkedHashSet

Aishwarya Naglot
Aishwarya Naglot
Updated on 29-Jul-2025 456 Views

LinkedHashSet is a collection in Java that maintains the insertion order of elements. It is part of the Java Collections Framework and extends the HashSet class. It stores unique elements and allows null values, but only one null element. It is similar to HashSet; the main difference is that a LinkedHashSet maintains a linked list holding of the entries of the current object, allowing it to maintain the order of elements. Let's learn how to get the size of a LinkedHashSet in Java. The following are some example scenarios: Scenario 1 Input : set = {1, 2, 3, 4, ...

Read More

Get an element from a Stack in Java without removing it

Aishwarya Naglot
Aishwarya Naglot
Updated on 29-Jul-2025 3K+ Views

The Stack is a data structure that is used to store elements in a Last In First Out (LIFO) manner. In Java, a stack is represented by the java.util.Stack class. It provides methods to create and manipulate a stack. Get an Element of a Stack without Removing it The pop() method of the Stack class removes the element at the top of the stack and returns it. But there is no direct way to access an element without removing it. However, we can see/view the top element of a Stack in Java using the peek() method. This method returns the ...

Read More

Can a constructor be made final in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 29-Jul-2025 7K+ Views

A constructor is a special method in Java that is used to initialize objects. It gets called when an instance of a class is created. The constructor has the same name as the class and does not have a return type. The question is whether a constructor can be declared or made final in Java? The answer is no. Let's understand why. Why can't we declare a Java Constructor "final"? In Java, the final keyword is used to restrict modification of the members of a class (methods and variables). For example, a final method of a class cannot be overridden ...

Read More

How to iterate over a TreeMap in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 28-Jul-2025 3K+ Views

TreeMap is based on the Red-Black tree structure, which is a It is a part of the Java Collections Framework. It is a sorted map and maintains the order of its keys. Its order depends on the natural ordering of the keys or by a Comparator.Since the TreeMap is not a Collection, we cannot use the for-each loop to iterate through it. Instead, we can use the entrySet() method to get a set view of the mappings contained in the map, which we can then iterate over to access the keys and values. Let's explore some scenarios to understand the ...

Read More

Iterate through elements of Java LinkedHashSet

Aishwarya Naglot
Aishwarya Naglot
Updated on 28-Jul-2025 526 Views

LinkedHashSet is a part of Java's Collection Framework. It is a type of Set that keeps the order of elements the same as they were added. It uses a combination of a hash table and a linked list to store elements.Our task is to traverse/iterate through the elements of a LinkedHashSet in Java. Let's look at some scenarios to understand the problem better: Scenario 1 Input: {1, 2, 3, 4, 5} Output: 1 2 3 4 5 Explanation: The LinkedHashSet contains the elements 1, 2, 3, 4, and 5. When we iterate through it, we get the elements in the ...

Read More

Check if a Java HashSet Collection contains another Collection

Aishwarya Naglot
Aishwarya Naglot
Updated on 28-Jul-2025 1K+ Views

HashSet is a class in Java that implements the Set interface. It is used for storing unique elements. Our task is to check if a HashSet contains another collection in Java. Let's look at some scenarios to understand the problem better: Scenario 1 Input: {1, 2, 3, 4, 5} Collection to check: {2, 3} Output: true Explanation: The HashSet contains all the elements of the collection {2, 3}. Scenario 2 Input: {1, 2, 3, 4, 5} Collection to check: {6, 7} Output: false Explanation: The HashSet does not contain all the elements of the collection {6, 7}. Checking ...

Read More
Showing 1261–1270 of 4,498 articles
« Prev 1 125 126 127 128 129 450 Next »
Advertisements