[英]assigning List[] to HashMap in Java
我正在尝试用String[]
填充HashMap
,但它返回 null
HashMap<String[], String[]> h = new HashMap<String[], String[]>();
String[] part2 = new String[100];
String[] part1 = new String[100];
int n = 0;
for (String a : tempFindings) {
String[] parts = a.split("\\.");
part1[n] = parts[0];
part2[n] = parts[1];
n++;
}
h.put(part2, part1);
有什么线索可以将我的所有列表拆分为 HashMap?
编辑:
我也试过
HashMap<String[], String[]> h = new HashMap<String[], String[]>(); for(String a: tempFindings) { String[] 部分 = a.split("\."); 第 1 部分 = 部分 [0]; 第 2 部分 = 部分[1];
h.put(part2, part1);
}
buth.put 每次都会自己重写。
我想知道为什么它的行为不像这个例子:
// Mapping string values to int keys
hash_map.put(10, "Geeks");
hash_map.put(15, "4");
hash_map.put(20, "Geeks");
hash_map.put(25, "Welcomes");
hash_map.put(30, "You");
如果 you.put 一个接一个,它会创建一个 hashmap 有几个 alue 但如果我把它放在一个循环中它会重写自己。
假设您有一个文件名列表,例如:
List<String> myFiles = List.of("foo.txt", "foo.sql", "foo.dat", "foo.exe");
您可以使用比较器按扩展名对列表进行排序,方法是使用String#substring
和String#lastIndexOf
提取扩展名
例子:
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<String> myFiles = List.of("foo.txt", "foo.sql", "foo.dat", "foo.exe");
Comparator<String> byExtension = (f1, f2) -> f1.substring(f1.lastIndexOf('.'))
.compareTo(f2.substring(f2.lastIndexOf('.')));
List<String> sorted = myFiles.stream().sorted(byExtension).collect(Collectors.toList());
System.out.println(myFiles);
System.out.println(sorted);
}
}
首先,您可能需要将Map<String[], String[]>
更改为Map<String, String[]>
如果您想存储String
类型 - 作为文件扩展名,而String[]
数组作为所有文件名。
但如果我理解正确,作者需要:
我需要根据文件扩展名对列表进行排序
我希望您需要将它存储在 Map 中,就像键 - 扩展名和值 - 所有名称一样。
可能是一个解决方案:
import java.io.File;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;
public class Main2 {
public static void main(String[] args) {
Map<String, List<String>> map = getAllFilesGroupedByExtension("C://");
for(String one : map.keySet()){
System.out.println(map.get(one).toString());
}
}
public static Optional<String> getExtension(String filename) {
return Optional.ofNullable(filename)
.filter(f -> f.contains("."))
.map(f -> f.substring(filename.lastIndexOf(".") + 1));
}
public static Map<String, List<String>> getAllFilesGroupedByExtension(String path){
File[] filesList = new File(path).listFiles();
Map<String, List<String>> map = new HashMap<>();
for(File file : filesList){
if (file.isFile())
if (map.containsKey(getExtension(file.getName()).orElseThrow())){
List<String> tempList = map.get(getExtension(file.getName()).orElseThrow());
tempList.add(file.getName());
map.put(getExtension(file.getName()).orElseThrow(), tempList);
} else {
List<String> tempList = new ArrayList<>();
tempList.add(file.getName());
map.put(getExtension(file.getName()).orElseThrow(), tempList);
}
}
return map;
}
}
或者如果你需要List<File>
你可以使用这个:
public static Map<String, List<File>> getAllFilesGroupedByExtension(String path){
File[] filesList = new File(path).listFiles();
return Arrays.stream(filesList)
.filter(File::isFile)
.collect(Collectors.groupingBy(file -> getExtension(file.getName()).orElseThrow()));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.