Treeset, sorting files

Hi,

I'l looking at creating a mapped set with a filename that is a string and a date using these file names:

NALDClipsDB_backup_31032011.bak
NALDClipsDB_backup_24032011.bak
NALDClipsDB_backup_28032011.bak
NALDClipsDB_backup_29032011.bak
NALDClipsDB_backup_01042011.bak
NALDClipsDB_backup_27032011.bak
NALDClipsDB_backup_26032011.bak
NALDClipsDB_backup_25032011.bak
NALDClipsDB_backup_30032011.bak

I would extract the date out of the string into hashmap so its like key:date, value:filename

ie something like :HashMap<String,String> aList = new HashMap<String,String>();

what I would like to do is to sort the hashmap on the date, how could this be done?

Darren
LVL 13
darren-w-Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
I think you don't need to sort HashMap - you need to get all keys from
the HashMap then  sort the keys and when you then traverse HashMap
use this sorted keys, so you'll travers HashMap in certain order.
If you want to travers by date - you can have the actual Date object as the key
not teh string of the date, and then you can sort by real date.
You can of course use Collections.sort() for sorting
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
darren-w-Author Commented:
Thanks for that, I used a treemap instead of the hashmap that sorted out (excuse the pun) sorting issue here is the code i'm using:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eclipsedl;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author darren
 */
public class PurgeFiles {

    private String path = "/home/darren/testfolder/";
    private File[] listFiles;
    private TreeMap<Date, String> aList;

    public PurgeFiles() {

        File directory = new File(path);
        listFiles = directory.listFiles();
        aList = new TreeMap<Date, String>();
    }

    public void listFiles() {
        String flieext = path + "NALDClipsDB_backup_";
        Date aDate = null;
        for (File listFile : this.listFiles) {
            DateFormat df = new SimpleDateFormat("ddMMyyyy");
            System.out.println(listFile.toString().substring(flieext.length()).substring(0, 8));
            try {
                aDate = df.parse(listFile.toString().substring(flieext.length()).substring(0, 8));
            } catch (ParseException ex) {
                Logger.getLogger(PurgeFiles.class.getName()).log(Level.SEVERE, null, ex);
            }
            aList.put(aDate, listFile.toString().substring(path.length()));
        }
        Collection c = aList.values();
        Iterator itr = c.iterator();

        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}

Open in new window


Darren
0
darren-w-Author Commented:
Thanks
0
for_yanCommented:
Sure, that's even better
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.