Results 1 to 6 of 6
- 05-25-2010, 09:43 AM #1
Member
- Join Date
- May 2010
- Posts
- 22
- Rep Power
- 0
how to get the values from hashmap
outputJava 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);
{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 advanceLast edited by baktha.thalapathy; 05-25-2010 at 09:48 AM. Reason: change
- 05-25-2010, 09:51 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Take a counter which works like a key for the map and concatenate sum with dateString to make the value.
Swastik
- 05-25-2010, 10:17 AM #3
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
- 05-25-2010, 12:56 PM #4
Member
- Join Date
- May 2010
- Posts
- 22
- Rep Power
- 0
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
- 05-25-2010, 01:43 PM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
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
- 05-25-2010, 02:12 PM #6
Member
- Join Date
- May 2010
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Problems with hashMap, has values in it but can't find them?
By mainy in forum New To JavaReplies: 5Last Post: 07-28-2009, 10:22 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
HashMap: Obtaining all values in a collision?
By markus-sukram in forum New To JavaReplies: 2Last Post: 03-29-2008, 10:25 PM -
How to make a hashmap to allow duplicate values?
By Preethi in forum New To JavaReplies: 0Last Post: 02-08-2008, 12:35 PM -
how to return values from hashmap
By oregon in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks