12 Replies Latest reply: May 7, 2008 2:05 AM by 800282 RSS

    Elements of hashmap against each other

    807601
      Hi folks,

      I am beginner in java.I need a favor from u guys.I need to add the elements to the hashmap (which may contain duplicate keys which had values as dates).I need to check the two hashmap entries which is more than 30days and element of hashmap against eachother which be shld again more than 30 days.pls help.I am struggling

      import java.util.*;
      public class TestHashMap{
      
       public static int daysBetween2Dates(String date1,String date2){
      
                      long difference=0;
                      Calendar c1 = Calendar.getInstance();   //new GregorianCalendar();
                      Calendar c2 = Calendar.getInstance();   //new GregorianCalendar();
                      c1.set(Integer.parseInt(date1.split("/")[2]),Integer.parseInt(date1.split("/")[1]),Integer.parseInt(date1.split("/")[0]));
                      c2.set(Integer.parseInt(date2.split("/")[2]),Integer.parseInt(date2.split("/")[1]),Integer.parseInt(date2.split("/")[0]));
                      difference=(c1.getTime().getTime() - c2.getTime().getTime())/(24*3600*1000);
                      return (int)difference;
       }
      
      
       public static void main(String[] args){
      
        HashMap hm = new HashMap();
        HashMap hm1 = new HashMap();
        String str;
        String song="date";
        int noofdays=0;
        String date="2007/11/26"
      
        AddDuplicatesKeysToList(hm, "date1", "2007/12/01");
        AddDuplicatesKeysToList(hm, "date", "2007/11/21");
        AddDuplicatesKeysToList(hm1, "date", "2008/04/11");
        AddDuplicatesKeysToList(hm1, "date", "2008/13/01");
      
          if(hm.containsKey(song)){
                       List ls= (List)previousMonthMap.get(song);
                       for (Iterator lstitr=ls.iterator(); lstitr.hasNext(); ) {
                                Object prvelement = lstitr.next();
                                String previousmonthDate=prvelement.toString();
                                noofdays=daysBetween2Dates(date,previousmonthDate);
                                System.out.println(noofdays);
                                if(noofdays<30){
                                      //add the rsult to vector
                                }
      
          }
      
      
      } // End of main.
      
      
         private static List AddDuplicatesKeysToList(Map m, String key, String value) {
              List vals = (List)m.get(key);
              if (vals == null) {
                 vals = new LinkedList();
                 m.put(key, vals);
              }
              vals.add(value);
              return vals;
         }
      
      }
        • 1. Re: Elements of hashmap against each other
          807601
          I had pasted the code with errors,here is the one without bug
          import java.util.*;
          public class TestHashMap{
          
           public static int daysBetween2Dates(String date1,String date2){
          
                          long difference=0;
                          Calendar c1 = Calendar.getInstance();   //new GregorianCalendar();
                          Calendar c2 = Calendar.getInstance();   //new GregorianCalendar();
                          c1.set(Integer.parseInt(date1.split("/")[2]),Integer.parseInt(date1.split("/")[1]),Integer.parseInt(date1.split("/")[0]));
                          c2.set(Integer.parseInt(date2.split("/")[2]),Integer.parseInt(date2.split("/")[1]),Integer.parseInt(date2.split("/")[0]));
                          difference=(c1.getTime().getTime() - c2.getTime().getTime())/(24*3600*1000);
                          return (int)difference;
           }
          
          
           public static void main(String[] args){
          
            HashMap hm = new HashMap();
            HashMap hm1 = new HashMap();
            String str;
            String song="date";
            int noofdays=0;
            String date="2007/11/26";
          
            AddDuplicatesKeysToList(hm, "date1", "2007/12/01");
            AddDuplicatesKeysToList(hm, "date", "2007/11/21");
            AddDuplicatesKeysToList(hm1, "date", "2008/04/11");
            AddDuplicatesKeysToList(hm1, "date", "2008/13/01");
          
              if(hm.containsKey(song)){
                           List ls= (List)hm.get(song);
                           for (Iterator lstitr=ls.iterator(); lstitr.hasNext(); ) {
                                    Object prvelement = lstitr.next();
                                    String previousmonthDate=prvelement.toString();
                                    noofdays=daysBetween2Dates(date,previousmonthDate);
                                    System.out.println(noofdays);
                                    if(noofdays<30){
                                          //add the rsult to vector
                                    }
          
                           }
               }
           // End of main.
          
          
             private static List AddDuplicatesKeysToList(Map m, String key, String value) {
                  List vals = (List)m.get(key);
                  if (vals == null) {
                     vals = new LinkedList();
                     m.put(key, vals);
                  }
                  vals.add(value);
                  return vals;
             }
          
          }
          • 2. Re: Elements of hashmap against each other
            807601
            finishit wrote:
            Hi folks,

            I am beginner in java.I need a favor from u guys.I need to add the elements to the hashmap (which may contain duplicate keys which had values as dates).
            A Hashmap cannot contain multiple entries with the same key.
            • 3. Re: Elements of hashmap against each other
              807601
              Thats y i try to add the duplicate values to the list and add to the same key.


              I want to check the elements of second map against each other to check for 30 days gap between dates.


              Pls help
              • 4. Re: Elements of hashmap against each other
                807601
                I think the way you have use a List within a HashMap is correct. (have not looked into it very closely)

                But there is definitely a problem in daysBetween2Dates(String date1,String date2) method.

                The set method of the Calendar class is set(int year, int month, int date) . But you have got the values of the year, month and date mixed up and so the difference that you calculate between dates is wrong.

                try this as I have corrected the order
                c1.set( Integer.parseInt(date1.split("/")[0]), 
                                Integer.parseInt(date1.split("/")[1]),
                                Integer.parseInt(date1.split("/")[2]));
                        
                        c2.set(Integer.parseInt(date2.split("/")[0]), 
                                Integer.parseInt(date2.split("/")[1]), 
                                Integer.parseInt(date2.split("/")[2]));
                hope this helps...

                ---------------

                deepak@1your.com
                • 5. Re: Elements of hashmap against each other
                  807601
                  can anyone help
                  • 6. Re: Elements of hashmap against each other
                    800282
                    finishit wrote:
                    Thats y i try to add the duplicate values to the list and add to the same key.


                    I want to check the elements of second map against each other to check for 30 days gap between dates.


                    Pls help
                    The latest code you posted still has an error in it. But, what is the problem exactly? The adding of multiple values to a single (unique) key? Or has your problem to do with dates? You will need to be more precise if you want to get help.

                    Oh, and please take the time to spell words properly instead of writing in those annoying SMS abbreviations:
                    pls = please
                    y = why
                    • 7. Re: Elements of hashmap against each other
                      807601
                      Actually i find problem in comparing 2 dates in different hashmap(hm1,hm2).

                      I also want to compare date value of hm1 also against each other and it should be above 15 days.

                      Really sorry for annoying acronyms.

                      P.S have to compare hm1 and hm dates which should be above 30 days and hm1 against itself.

                      any help will be appreciated.


                      Thanks for the same
                      • 8. Re: Elements of hashmap against each other
                        800282
                        finishit wrote:
                        Actually i find problem in comparing 2 dates in different hashmap(hm1,hm2).

                        I also want to compare date value of hm1 also against each other and it should be above 15 days.

                        Really sorry for annoying acronyms.
                        No problem.
                        \\
                        \\
                        P.S have to compare hm1 and hm dates which should be above 30 days and hm1 against itself.

                        any help will be appreciated.


                        Thanks for the same
                        Could you give an example of 2 maps filled with dates and what information should be retrieved?
                        • 9. Re: Elements of hashmap against each other
                          807601
                          AddDuplicatesKeysToList(hm, "date1", "2007/12/01");
                          AddDuplicatesKeysToList(hm, "date", "2008/03/21");
                          AddDuplicatesKeysToList(hm1, "date", "2008/04/11");
                          AddDuplicatesKeysToList(hm1, "date", "2008/13/01");


                          Like these i add dates dynamically in loop.


                          key for hm and hm1 is date which refer to dates 2008/03/21 and 2008/04/11 which is less than 30 days so i have to add to vector.

                          the hashmap keys values of hm1 refer to 14 and 4 of same month which is less than 15 days so i have add the values to vectot
                          • 10. Re: Elements of hashmap against each other
                            800282
                            finishit wrote:
                            AddDuplicatesKeysToList(hm, "date1", "2007/12/01");
                            AddDuplicatesKeysToList(hm, "date", "2008/03/21");
                            AddDuplicatesKeysToList(hm1, "date", "2008/04/11");
                            AddDuplicatesKeysToList(hm1, "date", "2008/13/01");


                            Like these i add dates dynamically in loop.


                            key for hm and hm1 is date which refer to dates 2008/03/21 and 2008/04/11 which is less than 30 days so i have to add to vector.

                            the hashmap keys values of hm1 refer to 14 and 4 of same month which is less than 15 days so i have add the values to vectot
                            If that is the best explanation you can give, then I can't help you since I really have no idea what it is you want.
                            Perhaps someone else can.

                            Good luck though.
                            • 11. Re: Elements of hashmap against each other
                              807601
                              As I have a multiple values associated with a key ,i am adding those values to list and adding the list to the particular key.Suppose a book is taken from a library at a particular date,it can be taken anytime next 30 days.previousMonthMap contains keys and asscoiated values for last month fetched books.currentmonthmap contains this month keys and values.keys being book name and values being different date fetched.If a book is fetched it can be free for next 30 days ,n number of times.i have to calculate usage for book gone out
                              Video fetched in current month also have to in 30 day barrier as days between 1 and 31 is 30days.
                              If the book is fetched on 12th of last month,18 of last month and 5th of current month,have to calculate diffrence between 12 of last month and 5th.
                              pls help.
                              import java.util.*;
                              public class TestHashMap{
                               
                               public static int daysBetween2Dates(String date1,String date2){
                               
                                              long difference=0;
                                              Calendar c1 = Calendar.getInstance();   //new GregorianCalendar();
                                              Calendar c2 = Calendar.getInstance();   //new GregorianCalendar();
                                              c1.set(Integer.parseInt(date1.split("/")[2]),Integer.parseInt(date1.split("/")[1]),Integer.parseInt(date1.split("/")[0]));
                                              c2.set(Integer.parseInt(date2.split("/")[2]),Integer.parseInt(date2.split("/")[1]),Integer.parseInt(date2.split("/")[0]));
                                              difference=(c1.getTime().getTime() - c2.getTime().getTime())/(24*3600*1000);
                                              return (int)difference;
                               }
                               
                               
                               public static void main(String[] args){
                               
                                HashMap previousMonthMap = new HashMap();
                                previousMonthMap.clear();
                                HashMap currentMonthMap  = new HashMap();
                                currentMonthMap.clear();
                                String str;
                                String song="date";
                                int noofdays=0;
                                String date="2007/11/26";
                               
                                AddDuplicatesKeysToList(previousMonthMap,"date1","15/02/2008");
                                AddDuplicatesKeysToList(currentMonthMap,"date","15/03/2008");
                                AddDuplicatesKeysToList(currentMonthMap,"date","19/03/2008");
                                AddDuplicatesKeysToList(currentMonthMap,"date","25/03/2008");
                               
                                                      Iterator currentIterator = currentMonthMap.keySet().iterator();
                                                      while(currentIterator.hasNext()){
                                                              String currentFileName = currentIterator.next().toString();
                                                              if(previousMonthMap.containsKey(currentFileName)){
                                                                      System.out.println("did dddI");
                                                                      List prevlist = (List)previousMonthMap.get(currentFileName);
                                                                      List currlist = (List)currentMonthMap.get(currentFileName);
                                                                      //Taking the first date element boz if the video is downloaded at 13 and 28 of previous month and 5th of current month,getting the first element in the list ll get 13th and difference is calculated between the two
                                                                      String [] prevfirstelement=(String[])prevlist.toArray(new String[prevlist.size()]);
                                                                      String [] currfirstelement=(String[])currlist.toArray(new String[currlist.size()]);
                                                                      noofdays= daysBetween2Dates(currfirstelement[0].split("\\$")[2],prevfirstelement[0]);
                                                                      System.out.println(noofdays);
                                                                      if(noofdays>30)
                                                                      logvideofile.add("rat"+currentMonthMap.get(currentFileName).toString());
                                                              }
                                                              else if(currentMonthMap.containsKey(currentFileName)){
                                                                      List currlist = (List)currentMonthMap.get(currentFileName);
                                                                      System.out.println("list Size"+currlist.size());
                                                                      if(currlist.size()>1){
                                                                              System.out.println("30 day barrier");
                                                                              String [] currelement=(String[])currlist.toArray(new String[currlist.size()]);
                                                                              System.out.println(currelement[0].split("\\$")[2]);
                                                                              System.out.println(currelement[1].split("\\$")[2]);
                              
                                                                              noofdays=daysBetween2Dates(currelement[0].split("\\$")[2],currelement[1].split("\\$")[2]);
                                                                              System.out.println("30 day barrier"+noofdays);
                                                                              if(noofdays>30)
                                                                              logvideofile.add("hh"+currentMonthMap.get(currentFileName).toString());
                                                                      }
                                                                      else{
                                                                              logvideofile.add("dd"+currentMonthMap.get(currentFileName).toString());
                                                                      }
                                                              }
                                                      }
                              
                               // End of main.
                               
                               
                                 private static List AddDuplicatesKeysToList(Map m, String key, String value) {
                                      List vals = (List)m.get(key);
                                      if (vals == null) {
                                         vals = new LinkedList();
                                         m.put(key, vals);
                                      }
                                      vals.add(value);
                                      return vals;
                                 }
                               
                              }
                              • 12. Re: Elements of hashmap against each other
                                800282
                                Calculate the difference between two java.util.Date's:
                                [http://forum.java.sun.com/thread.jspa?threadID=630625&messageID=3633434]

                                Parsing a date represented as a String to a java.util.Date:
                                [http://www.exampledepot.com/egs/java.text/ParseDate.html]