Java 按内部值对嵌套HashMap排序
我正在尝试对嵌套HashMap进行排序, HashMap myMap=通过内部HashMap中的特定值创建新的HashMap。 程序读取包含以下值的分隔文件: 000001014 | A |哈维| T |登特| 1991年5月27日| 0902 | 000001014 | 1991年5月27日| 01/01/3000| 000001388 | A |托尼| K |斯塔克| 1992年9月19日| 0054 | 000001388 | 1992年9月19日| 01/01/3000| 10/26/1992年7月14日,10/26/1992年7月14日,10/12月14日,10/12月12日,10/12月12日,10/12/1992年10/10/12/12/12/12/12/12号号,A卡卡卡卡卡卡4 4 4 4 4 4 4 4 5 5 5号卡卡卡卡卡卡卡卡卡4 5号号,A A卡卡卡卡卡卡卡卡4号号号号号号,A A A A A号号号号,卡卡卡卡卡卡卡卡卡卡卡4号号号号号号号,卡卡卡卡卡卡卡卡卡卡卡卡卡卡卡卡号号号号号号号号,卡卡卡卡卡号号号,卡卡卡号号号,卡卡卡号,卡卡卡号,卡卡卡号,卡卡卡号号,卡号,卡号号,卡号,卡号卡卡号,卡号卡卡号卡号,卡号卡号号号号48 | 02/17/1992 | 01/01/3000 |11月10日1992年7月17日,7月17日,11月14日,10月14日,10月14日,11月14日,10月14日,10月14日,10月14日,10月14日,12月14日,10月14日1992年10月14日,10月14日,1月1/1/1/1/1/10/1992年12/12/12/12/12/10号,1992年10/12/12/12/12/12/12/12/12/12/12/12/12/12/10号,1992年12/12/12/12/12/12/12/12/12/12/12/12/12/12/12/12/12/10号,1992年12/12/12/12/12/12/12/12/12/12/12/12/12/12/12/12/12/12/10号,1992年12/12/12/12/12/12/12/12/10号,1992 2 | 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.get0.get7返回0902,myMap.get1.get7返回0054,myMap.get2.get7返回7402。但问题是,按照嵌套的HashMap值对HashMap进行排序是一个真正的难题。所以,我试图完成的是按照内部映射中的第7个元素对整个HashMap进行排序 我是否应该使用嵌套循环和二进制排序或插入排序按旧方式对myMap排序?我如何解决这个问题Java 按内部值对嵌套HashMap排序,java,Java,我正在尝试对嵌套HashMap进行排序, HashMap myMap=通过内部HashMap中的特定值创建新的HashMap。 程序读取包含以下值的分隔文件: 000001014 | A |哈维| T |登特| 1991年5月27日| 0902 | 000001014 | 1991年5月27日| 01/01/3000| 000001388 | A |托尼| K |斯塔克| 1992年9月19日| 0054 | 000001388 | 1992年9月19日| 01/01/3000| 10/26/1
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();
}
首先,对HashMap进行排序意味着什么?打印排序后的值,我猜这意味着你不会对地图本身进行排序,而是对其值的某种集合进行排序 第二件事——为什么要将这些数据保存在地图中?这听起来真是个糟糕的主意,你刚刚发现了第一个论点为什么 对我来说,你应该创建一些Row类,比如
public class Row {
private List<String> items; // for '|' splitted values in a row, maybe it should be even String[]?
...
}
现在,您可以使用Collections.sort util轻松地对文件进行排序
请注意,实现Comparator允许您创建多个版本,如SortBy6thComparator、SortBy7thComparator、SortBy8thComparator。。。。您只需使用另一个版本的排序方法:
非常感谢你为我指明了正确的方向。我想我需要建立自己的比较器,但不确定这是否是正确的方法。再次感谢你的建议。请不要叫全班同学排。如果是一份员工档案,可以给班级打电话。。。受雇者请
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));
}
}
public static <T> void sort(List<T> list, Comparator<? super T> c)