fork download
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4.  
  5. class hash {
  6. public static void main(String args[])throws Exception
  7. {
  8. Scanner s=new Scanner(System.in);
  9. int n=s.nextInt();//size of array or hashmap
  10. HashMap<Integer,Integer> arr=new HashMap<Integer,Integer>();
  11. //We have created a HashMap with key as Integer datatype and Value as Integer data type.
  12. for(int i=0;i<n;i++)
  13. {
  14. int tmp=s.nextInt();
  15. arr.put(tmp, tmp);
  16. //we kept both key and value as same... you'll understand it later ...
  17. }
  18. //Now let us have some input and lets check if its present or not.
  19. //Lets see whats stored in hashmap arr
  20. System.out.println(arr);
  21. int q=s.nextInt();//no. of queries
  22.  
  23. for(int i=0;i<q;i++)
  24. {
  25. int tmp=s.nextInt();
  26. if(arr.get(tmp)!=null)
  27. {
  28. System.out.println("Key present");
  29. }
  30. else
  31. {
  32. System.out.println("Not present");
  33. }
  34. }
  35.  
  36. }
  37. }
  38.  
Success #stdin #stdout 0.06s 711680KB
stdin
5
100 23 10 4 5
3
12 
4
55
stdout
{100=100, 4=4, 5=5, 23=23, 10=10}
Not present
Key present
Not present