1 Reply Latest reply: Nov 26, 2008 12:14 PM by 843785 RSS

    Compare HashMap values from 2 Separate files using equals()

    843785
      Hi,

      I am having a tough time getting this to work. I am trying to compare the values of 2 separate hashmaps. I want to say - If 'mother' in bridesfamily file and 'mother' in groomsfamily are equal (value =1) - the offspring are at greater risk of having a disease. What I have actually written is below.

      BridesFamily


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

      //import java.util.Iterator;

      public class BridesFamily
      {
           public static void main(String args[] )
           {
           //     private Map< String, Integer > map;

           //constructs a new hashMap
           //HashMap hashMap = new HashMap();

           HashMap<String, Integer> brideFamily = new HashMap<String, Integer> ();

           //adding values to the hashMap
           brideFamily.put("Mother", new Integer(1) );
           brideFamily.put("Father", new Integer(0) );
           {
                for (Map.Entry book : brideFamily.entrySet() )
                     System.out.println(book.getKey() + ": " + book.getValue() );


           }
      /*
                {System.out.println("The value for \"Mother\" key = "+hashMap.get ( "Mother" ) );
                System.out.println("The value for \"Father\" key = "+hashMap.get ( "Father" ) );

                }
      */
           //System.out.println("Grooms Family History: \nKey\t\tValue" );
           }
      }


      =========================================================================
      Grooms Family

      import java.util.HashMap;
      //import java.util.Iterator;
      public class GroomsFamily
      {

           public static void main(String args[] )
           {
           //constructs a new hashMap
           HashMap hashMap = new HashMap();

           //adding values to the hashMap
           hashMap.put("Mother", new Integer(1) );
           hashMap.put("Father", new Integer(0) );

           {

                System.out.println("The value for \"Mother\" key = "+hashMap.get ( "Mother" ) );
                System.out.println("The value for \"Father\" key = "+hashMap.get ( "Father" ) );
           
                }
           //System.out.println("Grooms Family History: \nKey\t\tValue" );
           }
      }

      ==================================================================
      BrideAndGroomFamily


      import java.util.Map;
      import java.util.HashMap;
      import java.lang.String;



      public class BrideAndGroomFamily
      {
      public static void main( String args[] )

      {
      //declare files to be compared
      BridesFamilyHx myBridesFamily = new BridesFamily();

      GroomsFamilyHx myGroomsFamily = new GroomsFamily();


      // Check if two object values are the same
      if (BridesFamily("Mother").equals(BridesFamily("Mother")));
      System.out.println( "Yes" );

      }
      }

      ===============================================
      Many thanks,

      Rinkie