Shriansh Kumar

Shriansh Kumar

211 Articles Published

Articles by Shriansh Kumar

Page 18 of 22

How to Fix \"class, interface, or enum expected\" Error in Java with Examples?

Shriansh Kumar
Shriansh Kumar
Updated on 20-Jul-2023 1K+ Views

Every Java programmer whether a beginner or experienced, encounters numerous errors while writing code. Generally, these errors are categorized as run time and compile time errors. The run-time error occurs while running the code after successful compilation and the compile-time error during compilation. The class, interface, or enum expected is an error thrown during the compilation of source code. It may occur due to several reasons, one of them being misplaced curly braces. In this article, we are going to explore the reasons for this error and corresponding ways to fix the class, interface, or enum expected error. ...

Read More

How to fix java.lang.ClassCastException while using the TreeMap in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 20-Jul-2023 634 Views

TreeMap is a class of Java Collection Framework that implements NavigableMap Interface. It stores the elements of the map in a tree structure and provides an efficient alternative to store the key-value pairs in sorted order. Note that while creating objects of TreeMap it is necessary to use either the Comparable Interface or Comparator Interface so that we can maintain the sorting order of its elements otherwise, we will encounter a java.lang.ClassCastException. In this article, we are going to explain how to use the Comparable and Comparator Interfaces to fix this ClassCastException in TreeMap. Fixing java.lang.ClassCastException in TreeMap ...

Read More

How to Fix java.lang.ClassCastException in TreeSet?

Shriansh Kumar
Shriansh Kumar
Updated on 20-Jul-2023 310 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. To fix this ClassCastException in TreeSet we can use either the Comparator Interface or the Comparable Interface. Let's discuss them in detail. The general ...

Read More

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

Shriansh Kumar
Shriansh Kumar
Updated on 20-Jul-2023 246 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
Shriansh Kumar
Updated on 20-Jul-2023 3K+ 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
Shriansh Kumar
Updated on 20-Jul-2023 797 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
Shriansh Kumar
Updated on 20-Jul-2023 2K+ 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
Shriansh Kumar
Updated on 19-Jul-2023 12K+ 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 the Number of Arguments Provided at Runtime in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 19-Jul-2023 1K+ 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

How to Create TreeMap Objects using Comparable Interface in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 19-Jul-2023 432 Views

TreeMap is a class of Java Collection Framework that implements NavigableMap Interface. It stores the elements of the map in a tree structure and provides an efficient alternative to store the key-value pairs in sorted order. Note that while creating objects of TreeMap we need to use the comparable interface so that we can maintain the sorting order of its elements. In this article, we are going to discuss a few Java programs to create TreeMap objects using comparable interface. Java Program to create TreeMap Objects using Comparable Interface Before jumping to the Java program ...

Read More
Showing 171–180 of 211 articles
« Prev 1 16 17 18 19 20 22 Next »
Advertisements