按内部值对嵌套HashMap进行排序

时间:2019-08-05 22:40:53

标签: java

我正在尝试对嵌套的HashMap进行排序, HashMap<Integer, HashMap<Integer, String>> myMap = new HashMap<>(),通过内部HashMap中的特定值。 该程序读取一个包含以下值的定界文件:

000001014 | A || Harvey | T | Dent | 05/27/1991 | 0902 | 000001014 | 05/27/1991 | 01/01/3000 | 000001388 | A || Tony | K | Stark | 09/19/1992 | 0054 | 000001388 | 09/19/1992 | 01/01/3000 | 000001395 | A || Steve | C | Rogers | 10/26/1992 | 7402 | 000001395 | 10/26/1992 | 01/01/3000 | 000001396 | A || Peter | P | Parker | 1992年11月2日| 1002 | 000001396 | 1992年11月2日| 01/01/3000 | 000011148 | I || Drax | T | Destroyer | 02/17/1992 | 7005 | 000011148 | 02/17/1992 | 01/01/3000 | 000011141 | A ||火箭| M |浣熊| 02/10/1992 | 7170 | 000011141 | 02/10/1992 | 01/01/3000 | 000001404 | A || Natasha || Romanoff | 12/28/1992 | 7240 | 00001404 | 12/28/1992 | 01/01/3000 | 000001442 | A || Bruce | T |横幅| 10/06/1993 | 7012 | 000001442 | 10/06/1993 | 01/01/3000 | 000001450 | A || Scott | L | Lang | 11/29/1993 | 0002 | 000001450 | 11/29/1993 | 01/01/3000 | 000001486 | A || Thor | J | Odinson | 07/04/1994 | 0002 | 000001486 | 07/04/1994 | 01/01/3000 |

我选择了一个嵌套HashMap,以便文件中的每一行都有自己的键,然后每一行中的每个元素都有一个键。例如,myMap.get(0).get(7)返回0902,myMap.get(1).get(7)返回0054,myMap.get(2).get(7)返回7402。但是问题是排序嵌套的HashMap值生成的HashMap确实是个悍马。因此,我要完成的工作是按照内部地图中的第7个元素对整个HashMap进行排序。

我应该使用嵌套循环和二进制排序还是插入排序以旧的方式对myMap进行排序?我该如何解决这个问题?

private static Path directory() {
    File home = FileSystemView.getFileSystemView().getHomeDirectory();
    String path = home.getAbsolutePath();
    Path dir;
    //For reference Directory 
    C:\Users\PC_USER_NAME\Desktop\Work\Person\Employees.txt
    if(getProperty("os.name").startsWith("Windows")) {//specify your 
        directory Windows
        dir = Paths.get(path + File.separator + "Work" + File.separator + "Person");
    } else {//Specify your directory Mac
        dir = Paths.get(File.separator + "Users" + File.separator + 
        getProperty("user.name") + File.separator + "Desktop" + File.separator + "Work" + File.separator + "Person");
    }
    return dir;
}

private static void readFile() {
    HashMap<Integer, HashMap<Integer, String>> myMap = new HashMap<>();
    HashMap<Integer, String> inner = new HashMap<>();
    BufferedReader reader;
    String line;
    int count = 0;
    try {
        File dir = new File(directory().toString());
        File[] files = dir.listFiles((File pathname) -> 
        pathname.getName().startsWith("Employees"));
        File lastModifiedFile = files[0];

        for (File file : files) {
            if (lastModifiedFile.lastModified() < file.lastModified()) {
                lastModifiedFile = file;
            }
        }

        reader = new BufferedReader(new FileReader(lastModifiedFile));
        //Skips the header.
        reader.readLine();
        while((line = reader.readLine()) != null) {
            String[] keyValue = line.split("\\|");

            for (int i = 0; i < keyValue.length; i++) {
                inner.put(i, keyValue[i]);
            }
            myMap.put(count, inner);
            count++;
            inner = new HashMap<>();
        }
        reader.close();
    } catch (IOException e) { e.printStackTrace(); }
    sort(myMap);
}

private static void sort(HashMap<Integer, HashMap<Integer, String>> myMap) {
    Set<Entry<Integer, HashMap<Integer, String>>> sorted = 
    myMap.entrySet();

    for(Entry<Integer, HashMap<Integer, String>> entry : sorted) {
        System.out.println(entry.getKey() + " ==> " +   entry.getValue().get(7));
    }
    //Won't add this method code for brevity sake   
    writeFile();
}

1 个答案:

答案 0 :(得分:1)

首先-排序HashMap 是什么意思?要打印排序后的值,我想这意味着您不会对Map本身进行排序,而是对其值进行某种收集

第二件事-为什么要将此类数据保留在Map中?听起来真的是个坏主意,您只是发现了第一个理由,为什么

对我来说,您应该创建某种Row类,例如

public class Row {
    private List<String> items; // for '|' splitted values in a row, maybe it should be even String[]?

    ...
}

,并将整个文件保留为List<Row>。然后,您可以创建自己的Comparator甚至使Row的实现Comparable

public class Row implements Comparable<Row>{
    private List<String> items = new ArrayList<>();

    ...

    @Override
    public int compareTo(Row that) {
        return this.items.get(8).compareTo(that.items.get(7));
    }
}

现在,您可以使用Collections.sort() util

轻松地对文件进行排序

请注意,实施Comparator允许您创建它们的许多版本(例如SortBy6thComparatorSortBy7thComparatorSortBy8thComparator ...)。您只需要使用另一种sort方法:

public static <T> void sort(List<T> list, Comparator<? super T> c)