package kaoshi;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.TreeMap;
public class houqu2 {
static ArrayList<String> al = new ArrayList<>();
/**
* @param args
*/
public static void main(String[] args) {
File f = new File("F:\\黑马\\新建文件夹");
get(f);
String[] arr = al.toArray(new String[0]);
TreeMap<String,Integer> tm = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int num = o1.length() - o2.length();
return num == 0 ? -1 : num;
}
});
for (int i = 0; i < arr.length; i++) {
//System.out.println(arr[i]);
tm.put(arr[i],arr[i].length());
}
System.out.println(tm);
for (String string : tm.keySet()) {
System.out.println(tm.get(string));
}
/*for (Entry<String, Integer> s : tm.entrySet()) {
System.out.println(s.getKey());
}*/
}
public static ArrayList<String> get(File f) {
File[] list = f.listFiles();
for (File file : list) {
if (file.isFile() && file.getName().endsWith(".java")) {
al.add(file.getName());
// System.out.println(file.getName());
} else if (file.isDirectory()) {
get(file);
}
}
return al;
}
}
|
|