Results 1 to 6 of 6
  1. #1
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default how to get the values from hashmap

    Java Code:
     Set<String> dates = new HashSet<String>();
             ArrayList<Integer> l=new ArrayList<Integer>();
             ArrayList<Integer> m=new ArrayList<Integer>();
             
             Map <String, Long> datesAndTotal = new HashMap<String, Long>();
             DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
     while ( rs.next() )
             {
    
                String gpstime = rs.getString("GpsTime");
                //System.out.println("Time in minutes: " + diffSeconds+ " seconds.");
    
    
                if (dates.add(gpstime) )
                {
                   //System.out.println(rs.getTimestamp(1));
                   Date d2 = rs.getDate(1);
                   //System.out.print(d2);
                   t2 =   rs.getTimestamp(1);
    
                   count++;
                   if (t1 == null )
                   {
                      int nano = t2.getSeconds();
                      //System.out.println(nano);
                      System.out.println(nano);
                      System.out.println(t2);
                      System.out.println(count);
                   }
    
                   else
                   {
    
                      mill= (t1.getTime() - t2.getTime())/-1;
    
                      //System.out.println(d1);
                      diffSeconds = mill / 1000;
                      diffMinutes = mill / (60 * 1000);
                      long diffHours = mill / (60 * 60 * 1000);
                      long diffDays = mill / (24 * 60 * 60 * 1000);
                      //System.out.println("\nThe Date Different");
                      //System.out.println("Time in milliseconds: " + mill+ " milliseconds.");
                      System.out.println("GPSTime " +gpstime+ " Time in seconds: " + diffSeconds+ " seconds.");
                      //System.out.println("Time in minutes: " + diffMinutes+ " minutes.");
                      //System.out.println("Time in hours: " + diffHours+ " hours.");
                      //System.out.println("Time in days: " + diffDays+ " days.");
    
    
                      totalTimeInMillis = (t2.getTime() - t1.getTime()) / 1000;
    
                      //System.out.println(totalTimeInMillis);
    
                       String dateString = df.format(d2);
    
                      if (datesAndTotal.containsKey(dateString))
                      {
                          m.add((totalTimeInMillis < 80) ? (int) totalTimeInMillis : (int) (totalTimeInMillis % 60));
    
                          for (int y=0; y<m.size();)
                          {
                          sum+=m.get(y);
                          y++;
                        }
                             // System.out.println(sum);
                          
                          sum+= datesAndTotal.get(dateString)/-1;
                      }
                        
    
                      datesAndTotal.put(dateString, sum);
                 
                    
                   }
                   t1 = t2;
                   l.add((diffSeconds < 80) ? (int) diffSeconds : (int) (diffMinutes % 60));
                   
                }
             }
             System.out.println(datesAndTotal);
    output

    {2010-05-12=17575, 2010-05-11=17030, 2010-05-13=31027}

    how do i get the values of {}

    like this
    2010-05-12 17575
    2010-05-11 17030
    2010-05-13 31027

    i need output like
    2010-05-11 (17030)->output of 11 th date
    17575-17030 (545)->output of 12 th date
    31027-17575 (13452)->output of 13th date


    how do i change the give code to get above output

    please help anybody having idea

    Thanks in advance
    Last edited by baktha.thalapathy; 05-25-2010 at 09:48 AM. Reason: change

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Take a counter which works like a key for the map and concatenate sum with dateString to make the value.
    Swastik

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by baktha.thalapathy View Post
    how do i change the give code to get above output

    i couldn't run your code because a lot of classes were missing. here is a small example how to iterate through a hashmap

    Java Code:
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    public class HashMapExample {
    	public static void main(String[] args) {
    		Map h = new HashMap();
    		// Pflege der Aliase
    		h.put("john", "f.mueller@test.de");
    		h.put("fred", "fk@b-blabla.com");
    		h.put("paula", "paula@gmx.de");
    		h.put("lisa", "user789@yahoo.com");
    
    		// to get the iterator, you must first get all
    		// pairs with entrySet()
    		Iterator it = h.entrySet().iterator();
    		while (it.hasNext()) {
    			// the iterator gives back an object
    			// of type Map.Entry, so you must cast
    			// the elements with Map.Entry
    			Map.Entry entry = (Map.Entry) it.next();
    			// your data are in the object entry and you can
    			// get the key with getKey() and the value with getValue()
    			System.out.println((String) entry.getKey() + " -> "
    					+ (String) entry.getValue());
    		}
    	}
    }

    so the output is


  4. #4
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default

    thanks for your reply

    i am getting the values like this

    2010-05-11 -> 17030
    2010-05-12 -> 17575
    2010-05-13 -> 31027
    2010-05-14 -> 42599
    2010-05-15 -> 53155
    2010-05-16 -> 62191



    now i need take the diff values from
    2010-05-11 -> 17030->this is output of 2010-05-11
    (2010-05-12 -> 17575)-(2010-05-11 -> 17030)-->545-this is output of 2010-05-12
    (2010-05-13 -> 31027)-(2010-05-12 -> 17575)-->13452-this is the output of 2010-05-13
    (2010-05-14 -> 42599)-(2010-05-13 -> 31027)-->11572-this is the output of 2010-05-14

    i need this type of output which one i need to change to get the above output

    thanks in advance

  5. #5
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Java Code:
    Iterator it = datesAndTotal.entrySet().iterator();
    		String prevKey="";
    		while (it.hasNext()) {
    			Map.Entry entry = (Map.Entry) it.next();
    			String presentKey=(String)entry.getKey();
    			if(prevKey.length()==0){
    				System.out.println(presentKey+"->"+entry.getValue());
    			}
    			else{
    				Long present=(Long)entry.getValue();
    				Long prev=datesAndTotal.get(prevKey);
    				long result=present-prev;
    				System.out.println("("+presentKey+")-("+prevKey+")->"+result);
    			}
    			prevKey=(String)presentKey;
    		}
    Swastik

  6. #6
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default

    now i got the exact output

    (2010-05-12)-(2010-05-11)->545
    (2010-05-13)-(2010-05-12)->13452
    (2010-05-14)-(2010-05-13)->11572
    (2010-05-15)-(2010-05-14)->10556
    (2010-05-16)-(2010-05-15)->9036

    thanks for reply

Similar Threads

  1. Replies: 5
    Last Post: 07-28-2009, 10:22 PM
  2. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  3. HashMap: Obtaining all values in a collision?
    By markus-sukram in forum New To Java
    Replies: 2
    Last Post: 03-29-2008, 10:25 PM
  4. How to make a hashmap to allow duplicate values?
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 12:35 PM
  5. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •