Provider get() method in Java with Examples
Last Updated :
04 Dec, 2018
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:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
int i = 10 , j = 10 ;
try {
KeyPairGenerator sr = KeyPairGenerator.getInstance( "DSA" , "SUN" );
Provider provider = sr.getProvider();
Set<Object> set;
set = provider.keySet();
Iterator iter = set.iterator();
while (i > 0 ) {
Object element = provider.get(iter.next());
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:
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
KeyPairGenerator sr = KeyPairGenerator.getInstance( "DSA" , "SUN" );
Provider provider = sr.getProvider();
System.out.println( "Trying to get the value of an unmapped key" );
Object element = provider.get( "geeks" );
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
Share your thoughts in the comments
Please Login to comment...