Found 2616 Articles for Java

Iterate TreeMap in Reverse Order in Java

Shriansh Kumar
Updated on 20-Jul-2023 16:47:58

674 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. In other words, it always returns the elements in ascending order. However, Java provides a few ways to iterate through the TreeMap in descending order. In this article, we are going to explore the ways of iterating TreeMap in Reverse order. Iterate TreeMap in Reverse Order in Java We will use the following ways to print elements of TreeMap in reverse order: ... Read More

How to Get and Set Default Character Encoding or Charset in Java?

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

1K+ Views

In Java, the default character encoding is determined by the 'file.encoding' which is a system property and is usually set by the operating system or the JVM. However, sometimes a Java programmer may need to get or set the default character encoding programmatically for various reasons. For this purpose, the 'java.nio.charset' package provides various classes and methods. In this article, we are going to learn the different ways to get and set default character encoding or charset. Get and Set Default Character Encoding or Charset in Java First, let's discuss what is default character encoding or charset. ... Read More

How to Generate MD5 Checksum for Files in Java?

Shriansh Kumar
Updated on 20-Jul-2023 16:41:13

337 Views

Checksum is an encrypted sequence of characters that are generated with the help of various hashing algorithms like MD5 and SHA-1. The checksum is applicable for the downloadable files. Whenever we generate a checksum for files, it stays the same as long as the file does not get altered or corrupted. In this article, we are going to explore the MD5 checksum which is a hash value used to verify the integrity of files. It is a way of ensuring that the file one has downloaded or transferred is exactly the same as the original one. To generate MD5 checksum ... Read More

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

Shriansh Kumar
Updated on 20-Jul-2023 16:37:39

387 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
Updated on 20-Jul-2023 16:28:18

242 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
Updated on 20-Jul-2023 16:25:39

101 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 Fix java.lang.ClassCastException in TreeSet By Using Custom Comparator in Java?

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

95 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

231 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

410 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

66 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

Advertisements