Java Articles

Page 152 of 450

Java streams counting() method with examples

Aishwarya Naglot
Aishwarya Naglot
Updated on 08-May-2025 649 Views

In this article, we will learn how to count the number of elements in a stream using the counting() method in Java Streams. Java Streams provide an efficient way to process data collections, and the Collectors.counting() method is useful for counting the number of elements in a stream. The counting() method is a static method of the Collectors class, which is part of the java.util.stream package. It returns a long value representing the number of elements in the stream. Counting the Number of Elements in the Stream The following are the steps to count the number of elements in the ...

Read More

How to print the maximum occurred character of a string in Java?

Vivek Verma
Vivek Verma
Updated on 08-May-2025 922 Views

A String class can be used to represent character strings; all the string literals in Java programs are implemented as instances of the String Class. In Java, Strings are constants and their values cannot be changed (immutable) once declared.Printing Maximum Occurred Character The maximum occurring character of a string refers to the character that appears multiple times (more than any other character in a string) in a string. To count the maximum occurring character of a string, we have the following approaches - Using an Array Using HashMap Using an Array Using an array is one way to ...

Read More

Difference between ArrayList.clear() and ArrayList.removeAll() in java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 08-May-2025 1K+ Views

The ArrayList class in Java is a Resizable-array implementation of the List interface. It allows null values. Java clear() Method V/S removeAll() Method There are some important differences between the clear() and removeAll (Collection c) methods of the ArrayList class. This table compares these two methods. Key clear() removeAll() ...

Read More

What happens when we try to add a duplicate key into a HashMap object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 08-May-2025 4K+ Views

No, HashMap does not allow duplicate keys. The HashMap is a class that implements the Map interface. It is based on the Hash table. It is used to store key-value pairs. It allows null keys and values, but it does not allow duplicate keys. You can store key-value pairs in the HashMap object. Once you do so, you can retrieve the values of the respective keys, but the values we use for keys should be unique. Let's understand what will happen if we try to add a duplicate key into a HashMap.Addng a duplicate key to a HashMapThe put() method ...

Read More

Reading data from keyboard using console class in Java

Maruthi Krishna
Maruthi Krishna
Updated on 08-May-2025 1K+ Views

The Console class is part of the Java.io package and is used to read input from the keyboard or user and write output to the console. Unlike Scanner, the Console class provides methods to read text and passwords. If you read a password using the Console class, it will not be displayed to the user (without showing the typed characters). To use the Console class, we need its object, and the Console object is obtained by calling System.console() as shown below - Console console = System.console(); Note: If you try to execute the program in a non-interactive environment like an IDE, it doesn’t work. It ...

Read More

How to set a tooltip text for each item of a JList in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 07-May-2025 906 Views

In this article, we will learn to set a tooltip text for each item of a JList in Java. Tooltips give meaningful information as users hover over UI components. Swing's JList does not support tooltips on a per-item basis, so it will involve some customization. What is JList? A JList is a subclass of the JComponent class, and it can be used to display a list of objects that allows the user to select one or more items. A JList can generate a ListSelectionListener interface and needs to implement the abstract method valueChanged(). Syntax The following is the syntax for ...

Read More

How to Write/create a JSON file using Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 07-May-2025 66K+ Views

In this article, we will learn to write/create a JSON file using Java. JSON has become the standard for data exchange for any kind of application. Java also has a collection of libraries helpful in creating and storing JSON files. JSON JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. Sample JSON document Below is an example of a JSON file: { "book": [ { ...

Read More

When will be an IllegalStateException (unchecked) thrown in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 06-May-2025 551 Views

In this article, we will learn how an IllegalStateException (unchecked) is thrown in Java. IllegalStateException is a subclass of RuntimeException. It occurs when a method is used at the wrong time or when an object is not in the right state. What is an IllegalStateException? An IllegalStateException is an unchecked exception in Java. This exception may arise in our Java program mostly if we are dealing with the collection framework of java.util.package. There are many collections, such as List, Queue, Tree, and Map, of which List and Queues (Queue and Deque) throw this IllegalStateException under particular conditions. When will ...

Read More

Convert List of Characters to String in Java

Aishwarya Naglot
Aishwarya Naglot
Updated on 06-May-2025 1K+ Views

In this article, we will learn to convert a list of characters to strings in Java. Sometimes, when we handle character-based data structures, we need to convert a list of characters into a string. Problem Statement Given a list of characters, the task is to convert it into a single string where the order of characters remains unchanged. Input:  list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e'); Output: Welcome Methods to Convert a List of Characters to a String The following are the different approaches to convert a list of characters to a string in Java - ...

Read More

How can we set a border to JCheckBox in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 05-May-2025 761 Views

In this article, we will learn to set a border for JCheckBox in Java. JCheckBox is a Swing component that is commonly used in user interface selection. Although it includes a default appearance, we can customize its look by setting borders in order to have a good visual effect. What is a JCheckBox? A JCheckBox is a component that extends JToggleButton, and an object of JCheckBox represents an option that can be checked or unchecked. Syntax The following is the syntax for JCheckBox initialization: JCheckBox Checkbox_name = new JCheckBox(); If there are two or more options, then any ...

Read More
Showing 1511–1520 of 4,498 articles
« Prev 1 150 151 152 153 154 450 Next »
Advertisements