Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java map get key from value

import java.util.HashMap;
import java.util.Map;
 
class Main
{
    public static <K, V> K getKey(Map<K, V> map, V value)
    {
        return map.entrySet().stream()
                .filter(entry -> value.equals(entry.getValue()))
                .findFirst().map(Map.Entry::getKey)
                .orElse(null);
    }
 
    public static void main(String[] args)
    {
        Map<String, Integer> hashMap = new HashMap();
        hashMap.put("A", 1);
        hashMap.put("B", 2);
        hashMap.put("C", 3);
 
        System.out.println(getKey(hashMap, 2));        // prints `B`
    }
}
Comment

java map get value

// Java code to illustrate the get() method
import java.util.*;

public class Map_Demo {
	public static void main(String[] args)
	{
		// Creating an empty Map
		Map<Integer, String> map = new HashMap<Integer, String>();

		// Mapping string values to int keys
		map.put(10, "Geeks");
		map.put(15, "4");
		map.put(20, "Geeks");
		map.put(25, "Welcomes");
		map.put(30, "You");

		// Displaying the Map
		System.out.println("Initial Mappings are: " + map);

		// Getting the value of 25
		System.out.println("The Value is: " + map.get(25));

		// Getting the value of 10
		System.out.println("The Value is: " + map.get(10));
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: all installed java 
Java :: bukkit java get player count 
Java :: java square root 
Java :: java generate random integer in range 
Java :: como ordenar un arraylist alfabeticamente en java 
Java :: Converting String Array to an Integer Array 
Java :: how to add chips dynamically android 
Java :: setbackground color hexadecimal android 
Java :: radio button in java 
Java :: has been compiled by a more recent version of the Java Runtime (class file version ), this version of the Java Runtime only recognizes class file versions up to 
Java :: String remove duplicate method in java 
Java :: Android Bitmap to Base64 String 
Java :: largest rectangle in histogram leetcode 
Java :: how to give square in java 
Java :: android maven dependency 
Java :: change size of array java 
Java :: antialiasing kjava 
Java :: Iterator to list (Java) 
Java :: java initialize class 
Java :: how to initialize an empty array in java 
Java :: java instanceof 
Java :: add custom font to android paint object 
Java :: java leap years 
Java :: the main of java 
Java :: find last element in array in java 
Java :: Java Looping Through Array Elements 
Java :: java check if property exists in class 
Java :: vscode code formatter cannot format 
Java :: reverse negative number 
Java :: print a list java 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =