简体   繁体   中英

How to check if keys (Calendar) in hashmap are consecutive?

I have a hashmap whose key type is Calendar, such as:

HashMap <Calendar, Student> map = new HashMap <Calendar, Student> ();

And once I sort the map by dates, I want to check if each calendar (key) in the map is consecutive per student name; for example, suppose the data in map is like this:

('10-01-2015', student1), ('10-02-2015', student1), ('10-03-2015', student1), ('10-10-2015', student1), ('10-11-2015', student2), ('10-13-2015', student2)

and so on.

Then what I want to have as a result is something like below:

student1: 10-01-2015 - 10-03-2015
student1: 10-10-2015
student2: 10-11-2015
student2: 10-13-2015

I think I am going to use TreeMap to sort the map, but I am not sure how to group the consecutive keys per value.
How would you approach?

You can do a loop and inside of it do something like this..

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(YOURKEY/DATE));
Calendar pd  = c;//previous Date
c.add(Calendar.DATE, 1);  // number of days to add

and now check if c.getTime = dt.getTime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM