Found 2616 Articles for Java

Java Program to Sort map by keys

AmitDiwan
Updated on 30-Mar-2022 11:09:59

430 Views

In this article, we will understand how to sort map by keys. The Java Map interface, java.util.Map, represents a mapping between a key and a value. More specifically, a Java Map can store pairs of keys and values. Each key is linked to a specific value.Below is a demonstration of the same −Suppose our input is −Input map: {1=Scala, 2=Python, 3=Java}The desired output would be −The sorted map with the key: {1=Scala, 2=Python, 3=Java}AlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create a Map structure, and add values to it ... Read More

Java Program to Check if a set is the subset of another set

AmitDiwan
Updated on 30-Mar-2022 11:07:46

878 Views

In this article, we will understand how to check if a set is the subset of another set. A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.Below is a demonstration of the same −Suppose our input is −First set: [90, 75, 60, 45] Second set : [90, 60]The desired output would be −Is a sets sub-set of the other? trueAlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step ... Read More

Java Program to Calculate the difference between two sets

AmitDiwan
Updated on 30-Mar-2022 11:05:57

626 Views

In this article, we will understand how to calculate the difference between two sets. A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.Below is a demonstration of the same −Suppose our input is −First set: [90, 75, 60, 45] Second set: [90, 60]The desired output would be −After subtraction of two sets: [75, 45]AlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create two Sets, and ... Read More

Java Program to Detect loop in a LinkedList

AmitDiwan
Updated on 30-Mar-2022 10:57:43

251 Views

In this article, we will understand how to detect loop in a LinkedList. A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The loop exists in the linked listAlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Define the class with the relevant members. Step 5 - Create an instance ... Read More

Java Program to Get key from HashMap using the value

AmitDiwan
Updated on 30-Mar-2022 10:40:31

4K+ Views

In this article, we will understand how to get key from HashMap using the value. Java HashMap is a hash table based implementation of Java's Map interface. It is a collection of key-value pairs.Below is a demonstration of the same −Suppose our input is −Input HashMap: {Java=8, Scala=5, Python=15} Key: 8The desired output would be −The value of Key: 8 is JavaAlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create a HashMap of integer and string values and initialize elements in it using the ‘put’ method. Step 5 - Define ... Read More

Java Program to Remove duplicate elements from ArrayList

AmitDiwan
Updated on 30-Mar-2022 10:34:39

3K+ Views

In this article, we will understand how to remove duplicate elements from arrayList. The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified.Below is a demonstration of the same −Suppose our input is −Input list : [150, 250, 300, 250, 500, 150, 600, 750, 300]The desired output would be −The list with no duplicates is: [150, 250, 300, 500, 600, 750]AlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the ... Read More

Java Program to Update value of HashMap using key

AmitDiwan
Updated on 30-Mar-2022 10:34:53

558 Views

In this article, we will understand how to update value of HashMap using key. Java HashMap is a hash table based implementation of Java's Map interface. It is a collection of key-value pairs.Below is a demonstration of the same −Suppose our input is −Input HashMap: {Java=1, Scala=2, Python=3}The desired output would be −The HashMap with the updated value is: {Java=1, Scala=12, Python=3}AlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 – Create a hashmap of values and initialize elements in it using the ‘put’ method. Step 5 - Display the hashmap on ... Read More

Java Program to Iterate over a Set

AmitDiwan
Updated on 30-Mar-2022 10:29:47

1K+ Views

In this article, we will understand how to iterate over a set. A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.Below is a demonstration of the same −Suppose our input is −Input set: [Java, Scala, Mysql, Python]The desired output would be −Iterating over Set using for-each loop: Java, Scala, Mysql, PythonAlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create a hashset of values and initialize elements ... Read More

Java Program to Iterate over a HashMap

AmitDiwan
Updated on 30-Mar-2022 09:08:14

328 Views

In this article, we will understand how to iterate over a HashMap. Java HashMap is a hash table based implementation of Java's Map interface. It is a collection of key-value pairs.Below is a demonstration of the same −Suppose our input is −Input Hashmap: {Java=Enterprise, JavaScript=Frontend, Mysql=Backend, Python=ML/AI}The desired output would be −The keys of the Hashmap are: Java, JavaScript, Mysql, Python, The Values of the Hashmap are: Enterprise, Frontend, Backend, ML/AI, AlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create a hashmap of strings and initialize elements in it using ... Read More

Java Program to Convert the ArrayList into a string and vice versa

AmitDiwan
Updated on 30-Mar-2022 08:46:31

284 Views

In this article, we will understand how to convert the arrayList into a string and vice versa. The ArrayList class is a resizable array, which can be found in the java. util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified.Below is a demonstration of the same −Suppose our input is −Input string: Java ProgramThe desired output would be −The array after conversion from string is: J a v a P r o g r a mAlgorithmStep 1 - START Step 2 - Declare namely Step 3 ... Read More

Advertisements