Found 9326 Articles for Object Oriented Programming

How to Fix java.lang.ClassCastException in TreeSet By Using Custom Comparator in Java?

Shriansh Kumar
Updated on 20-Jul-2023 16:22:44

91 Views

The TreeSet is a generic class of Java Collection Framework that implements the SortedSet Interface. It stores the elements of the set in a tree structure. Furthermore, all the elements are stored in a sorted manner and they must be mutually comparable if we are attempting to add custom class objects otherwise we will encounter a java.lang.ClassCastException. Here, custom class objects mean user-defined objects that are created with the help of a constructor. One way to fix this ClassCastException is by using a custom comparator. Let's discuss it in detail. The general syntax for TreeSet is as ... Read More

How to Get All the Values of the LinkedHashMap in Java?

Shriansh Kumar
Updated on 20-Jul-2023 16:01:51

229 Views

LinkedHashMap is a generic class of Java Collection Framework that implements Map Interface. As the name suggests, it is a sub-class of the HashMap class and uses a doubly LinkedList to store the entries in insertion order. It maintains Key-Value pair of entries. The Key is an object that is used to fetch and receive value associated with it. Hence, we can use this key along with 'get()' method to get all the values from LinkedHashMap. The aim of this article is to explain different ways to print all values of the LinkedHashMap. Java Program to Get all ... Read More

How to Get a Value From LinkedHashMap by Index in Java?

Shriansh Kumar
Updated on 20-Jul-2023 15:59:42

376 Views

LinkedHashMap is a generic class of Java Collection Framework that implements Map Interface. As the name suggests, it is a sub-class of the HashMap class and uses a doubly LinkedList to store the entries in insertion order. However, LinkedHashMap does not do any kind of indexing to store the entries, therefore, it is quite a challenge to get specified values from LinkedHashMap with the help of an index. The aim of this article is to explain how to find a value from LinkedHashMap using the index. Java Program to Get a Value from LinkedHashMap by Index Before jumping ... Read More

How to Find the Minimum or Maximum Element from LinkedHashSet in Java?

Shriansh Kumar
Updated on 20-Jul-2023 15:57:10

65 Views

LinkedHashSet is a class of Java Collection Framework that implements the Set interface and extends the HashSet class. It is a linked list type of collection class. It stores and returns the objects in the order in which they were inserted. Finding maximum and minimum elements from a LinkedHashSet collection is one of the common tasks that are frequently asked in exams and even in job interviews. In this article, we are going to explore a few approaches for performing the given task. Program to get Minimum and Maximum elements from LinkedHashSet To find the maximum ... Read More

How to find the Entry with largest Value in a Java Map

Shriansh Kumar
Updated on 20-Jul-2023 15:51:32

861 Views

In Java, Map is an object that stores its element in key-value pairs. The Key is an object that is used to fetch and receive value associated with it. The keys must be unique, but the values associated with them may be duplicated depending on the type of Map class we use. There are several approaches to find an entry with the largest key in Java map and these approaches also depend on the type of Map class we are working with. In this article, we will discuss how to find the entry with largest key in HashMap ... Read More

How to Find Prime and Palindrome Numbers using Multi-Threading in Java?

Shriansh Kumar
Updated on 20-Jul-2023 15:37:02

367 Views

Multithreading is a feature of the Java programming language that allows us to perform multiple operations simultaneously. In it, the operation gets divided into multiple smaller parts called a thread. Each thread performs one independent task without affecting the other thread's performance. The main benefit of multithreading is the optimal use of resources like CPU and it boosts the execution time of allocated operations. Finding prime and palindrome numbers are two of the basic programming task that every beginner programmer perfroms. However, in this article, we are going to do the same task in an exciting way. We will ... Read More

How to find Max Memory, Free Memory and Total Memory in Java?

Shriansh Kumar
Updated on 20-Jul-2023 15:35:17

696 Views

Memory management is an important aspect of any Java application. Having the knowledge of how much memory is available, how much is used and how much is free may help you to optimize your code and avoid performance issues or memory leaks. This article aims to help in finding max memory, free memory and total memory that are a crucial part of Java Heap. Note that the amount of memory allocated to a Java program depends on the environment. Java Program to find Max Memory, Free Memory and Total Memory Java provides the following methods and classes that ... Read More

How to find duplicate elements in a Stream in Java

Shriansh Kumar
Updated on 19-Jul-2023 19:06:38

7K+ Views

Finding duplicate elements in a stream of data is one of the common questions that is asked in Java interviews and even in the exams of many students. Java provides several ways to find duplicate elements, we will focus mainly on two ways: the first one is by using Set of Java Collection Framework and the other one is by using the built-in method of stream named Collections.frequency(). Java Program to find duplicate elements in a Stream Before discussing the different ways to get duplicate items from a collection of data, it is necessary to ... Read More

How to Find User Defined Objects From LinkedHashSet in Java?

Shriansh Kumar
Updated on 19-Jul-2023 18:49:36

149 Views

LinkedHashSet is a class of Java Collection Framework that implements the Set interface and extends the HashSet class. It is a linked list type of collection class. It stores and returns the objects in the order in which they were inserted, therefore it does not allow duplicate objects. In this article, we will use the built-in method 'contains()'' to find user-defined objects from LinkedHashSet. The user-defined objects are created with the help of constructors. Java Program to get User-Defined Objects from LinkedHashSet Let's have a brief introduction of two important in-built methods that we will use in our ... Read More

How to Find the Number of Arguments Provided at Runtime in Java?

Shriansh Kumar
Updated on 19-Jul-2023 18:40:22

412 Views

In Java, one way of passing arguments at runtime is by using the command line or terminal. While retrieving those values of command line arguments, we may need to find the number of arguments provided by the user at runtime which is possible with the help of length property. This article aims to explain the process of passing and getting the number of arguments provided by user with the help of example programs. Getting the Number of Arguments provided by User at Runtime Before finding the number of command line arguments, our first step is ... Read More

Advertisements