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 :: java linkedlist 
Java :: default constructor java 
Java :: java :: operator 
Java :: hashmap declare and initialize with values in 1 line java 
Java :: /= java 
Java :: interface in java 
Java :: java bufferedreader 
Java :: import class java 
Java :: how to replace a character with another character in a string in java 
Java :: file handling in java 
Java :: arduino convert byte array to string 
Java :: java graph 
Java :: how to saperate string to array 
Java :: finding min and max from given number in java 
Java :: string program to calculate number of characters in a string java 
Java :: android studio random number between 1 and 10 
Java :: java read all text from file 
Java :: get image from resourcestream javafx 
Java :: java dato numero big starkoverflow 
Sql :: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. 
Sql :: select nls_date_format 
Sql :: how to install mysql ubuntu 
Sql :: sql add column after another 
Sql :: set max_allowed_packet mysql 
Sql :: sql get missing id 
Sql :: sql drop procedure if exists 
Sql :: mysql find tables with column name 
Sql :: OSError: mysql_config not found 
Sql :: sql server check if temp table exists 
Sql :: mysql where one year ago 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =