Provider get() method in Java with Examples

The get() method of java.security.Provider class is used to return the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that (key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

Syntax:



public Object get(Object key)

Parameters: This method takes the key as a parameter whose associated value is to be returned.

Return Value: This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Below are the examples to illustrate the get() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// get() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        // Declaring int values
        int i = 10, j = 10;
  
        try {
            // creating the object of  KeyPairGenerator
            KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA", "SUN");
  
            // getting the Provider of the KeyPairGenerator sr
            // by using method getProvider()
            Provider provider = sr.getProvider();
  
            // Declaring the variable of set<Map> type
            Set<Object> set;
  
            // getting unmodifiable Set view of the property entries
            set = provider.keySet();
  
            // Creating the object of iterator to iterate set
            Iterator iter = set.iterator();
  
            while (i > 0) {
  
                // getting the mapped value in element
                // using get() method
                Object element = provider.get(iter.next());
  
                // printing the mapped value
                System.out.println("value is : " + element);
                i--;
            }
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

value is : SHA1withDSA
value is : SHA1withDSA
value is : SHA1withDSA
value is : Software
value is : sun.security.provider.JavaKeyStore$DualFormatJKS
value is : SHA
value is : sun.security.provider.SHA
value is : sun.security.provider.JavaKeyStore$CaseExactJKS
value is : Software
value is : sun.security.provider.DSA$SHA256withDSA

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// get() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
            // creating the object of  KeyPairGenerator
            KeyPairGenerator sr = KeyPairGenerator.getInstance("DSA", "SUN");
  
            // getting the Provider of the KeyPairGenerator sr
            // by using method getProvider()
            Provider provider = sr.getProvider();
  
            // getting the mapped value in element
            // using get() method
            System.out.println("Trying to get the value of an unmapped key");
            Object element = provider.get("geeks");
  
            // printing the mapped value
            System.out.println("value is : " + element);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Trying to get the value of an unmapped key
value is : null


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.