Open In App

Provider get() method in Java with Examples

Last Updated : 04 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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);
        }
    }
}


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:




// 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);
        }
    }
}


Output:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads