简体   繁体   中英

Keeping most recent date from a list of objects

I have an ordered list which is ordered by date, I want to keep the most recent date for each name in the list, for example there may be multiple entries for a name with different dates i want to keep only the most recent for that name,

how can i achieve this for the following list?

I mean a TreeMap which is ordered which maps dates to names, how can i remove for example, there is "Sun Feb 21 00:35:54 GMT 2016=Finley" then there is another "Sun Feb 21 01:03:41 GMT 2016=Finley" at a later date and so on, how could i remove all ones at later dates and only keep the recent ones? but for each name?

Sharks: {Sun Feb 21 00:27:53 GMT 2016=Freo, Sun Feb 21 00:35:54 GMT 2016=Finley, Sun Feb 21 01:03:41 GMT 2016=Finley, Sun Feb 21 01:09:28 GMT 2016=Finley, Sun Feb 21 01:20:19 GMT 2016=Buddy , Sun Feb 21 01:35:51 GMT 2016=Finley, Sun Feb 21 01:53:30 GMT 2016=Buddy , Sun Feb 21 02:01:18 GMT 2016=Finley, Sun Feb 21 02:28:46 GMT 2016=Freo, Sun Feb 21 02:59:50 GMT 2016=Finley, Sun Feb 21 05:10:09 GMT 2016=Maroochy, Sun Feb 21 05:14:11 GMT 2016=Maroochy, Sun Feb 21 08:42:36 GMT 2016=Buddy , Mon Feb 22 01:54:28 GMT 2016=Finley, Mon Feb 22 02:31:59 GMT 2016=Buddy , Mon Feb 22 02:34:21 GMT 2016=Catalina, Mon Feb 22 02:45:31 GMT 2016=Buddy , Mon Feb 22 02:45:47 GMT 2016=Finley, Mon Feb 22 03:13:26 GMT 2016=Buddy , Mon Feb 22 05:05:20 GMT 2016=Maroochy, Mon Feb 22 05:15:45 GMT 2016=Freo, Mon Feb 22 07:51:13 GMT 2016=Mary Lee, Mon Feb 22 07:53:44 GMT 2016=Jax, Mon Feb 22 08:21:42 GMT 2016=Finley, Mon Feb 22 09:24:41 GMT 2016=Jax, Mon Feb 22 09:25:08 GMT 2016=Mary Lee, Tue Feb 23 00:58:46 GMT 2016= Katharine, Tue Feb 23 01:20:20 GMT 2016=Catalina, Tue Feb 23 02:23:06 GMT 2016=Katharine, Tue Feb 23 02:35:47 GMT 2016=Cathy , Tue Feb 23 02:37:01 GMT 2016=Mary Lee, Tue Feb 23 02:58:57 GMT 2016=Katharine, Tue Feb 23 03:17:37 GMT 2016=Katharine, Tue Feb 23 03:21:20 GMT 2016=Katharine, Tue Feb 23 03:39:36 GMT 2016=Cathy , Tue Feb 23 04:23:30 GMT 2016=Maroochy, Tue Feb 23 08:07:00 GMT 2016=Finley, Tue Feb 23 09:12:31 GMT 2016=Finley, Wed Feb 24 00:21:19 GMT 2016=Katharine, Wed Feb 24 01:10:39 GMT 2016=Freo, Wed Feb 24 01:12:05 GMT 2016=Buddy , Wed Feb 24 01:18:03 GMT 2016=Katharine, Wed Feb 24 02:25:28 GMT 2016=Buddy , Wed Feb 24 03:51:03 GMT 2016=Katharine, Wed Feb 24 04:50:55 GMT 2016=Katharine, Wed Feb 24 07:33:22 GMT 2016=Katharine}

I would put the data into a HashMap (name->date). If an entry for the name already exists, overwrite it if the date is more recent.

In case you have trouble comparing the dates, use DateFormat to convert the strings into Date objects which you can compare.

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