Sunday, 9 July 2017

Q:16 How To Count The No Of  Same key And Value From Two HashMap.


import java.util.HashMap;
import java.util.Map.Entry;

public class CompareKeyValue
{
int count=0;
boolean flag=false;
public int  commonKeyValuePairs(HashMap<String,String> hm1,HashMap<String,String> hm2)
{
for (Entry<String, String> entry : hm1.entrySet())
 {

for (Entry<String, String> entry1 : hm2.entrySet())
{
              String key=entry1.getKey();
              String key1=entry.getKey();
              String values=entry.getValue();
              String values1=entry1.getValue();
             
             flag=key.equals(key1)&&(values.equals(values1));
             if(flag==true)
               count++;
   
   }



 }
 
return count;
}
public static void main(String[] args)
{

  HashMap<String, String> hm1=new HashMap<>();
hm1.put("abs","salmasn");
hm1.put("abc", "salmdan");
hm1.put("sam","more");
HashMap<String, String> hm2=new HashMap<>();
hm2.put("abs","salman");
hm2.put("abc", "salman");
hm2.put("sam","more");
CompareKeyValue m=new CompareKeyValue();
int count=m. commonKeyValuePairs(hm1, hm2);
System.out.println("The same no.of key and value btween two hashmap are "+count);

}

}

Wednesday, 12 April 2017

q-15 What is output of the following program?
class ReturnValue
{
    public static void main(String[] args)
    {
        System.out.println(methodReturningValue());
    }

    static int methodReturningValue()
    {
        try
        {
            return 49;
        }
        catch (Exception e)
        {
            return 20;
        }
        finally
        {
              System.out.println("finally block");
         }
    }
}


a)Compilation fails b)runtime exception c)20 finally block    d)49 finally block



q-13 how to throw the exception explicitly?


class Test
{
public static void main(String[] args) {
explicitlyCall();
}
public static  void explicitlyCall()
{
try
{
               ArithmeticException ae=new ArithmeticException("ArithmethicException");
       throw ae;
 }
catch(ArithmeticException ae)
{
ae.printStackTrace();
}

}
}

Tuesday, 10 January 2017

Q-12:-Lambda Expression Using List(java 8)

import java.util.*;
class a
{
public static void main(String[] args)
{
List<String> list=new ArrayList<>();
list.add("salman");
list.add("sam");
list.add("karthik");
list.forEach(Items->System.out.println(Items));

}