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?
 
for_yanConnect With a Mentor Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.