【问题标题】:assigning List[] to HashMap in Java在 Java 中将 List[] 分配给 HashMap
【发布时间】:2020-11-05 10:03:01
【问题描述】:

我正在尝试用 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 h = new HashMap(); for(String a : tempFindings) { String[] 部分 = a.split("\."); 第 1 部分 = 部分 [0]; part2 = 部分[1];

                h.put(part2, part1);
            }

but.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"); 

如果你 .put 一个一个,它会创建一个带有多个值的 hashmap,但如果我把它放在一个循环中,它会重写自己。

【问题讨论】:

  • 只有一个问题:“为什么?”
  • 我需要根据文件扩展名对列表进行排序
  • 那么你为什么使用String[] 类型作为key?您可以使用Map&lt;String, File[]&gt; mapString - 保存扩展名类型,File[] 存储所有具有该扩展名的文件
  • 您确定要将键和值设为String[] 吗?如果是,请举例说明输入和输出。
  • 你可能想要 Map>

标签: java hashmap hashtable


【解决方案1】:

假设您有一个文件名列表,例如:

List<String> myFiles = List.of("foo.txt", "foo.sql", "foo.dat", "foo.exe");

您可以使用比较器按扩展名对列表进行排序,方法是使用String#substringString#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);
    }
}

【讨论】:

    【解决方案2】:

    首先,如果要将String 类型存储为文件扩展名,并将String[] 数组作为所有文件名,则可能需要将Map&lt;String[], String[]&gt; 更改为Map&lt;String, String[]&gt;

    但如果我理解正确,作者需要:

    我需要根据文件扩展名对列表进行排序

    我希望您需要将其存储在 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&lt;File&gt;,你可以使用这个:

    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()));
        }
    

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 2019-08-29
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 2013-02-05
      相关资源
      最近更新 更多