简体   繁体   中英

Array to Map through for loop

I know its a very trivial issue, but I still need some help. I have an array and I want to get its element into a map.

 public static Map<String, String> getSomeId() {

    Map<String, String> map = new HashMap<>();
    File folder = new File("src/main/resources/someData");
    File[] listOfFiles = folder.listFiles();
    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            map.put("someId", listOfFiles[i].getName().substring(0, 13));
        }
    }
    return map;
}

I know that the content of the map gets overwritten in every loop and my map will contains only the last element put into. How can I get all element of the array into my map?

Thank you!

那这个呢?

map.put("someId" + i, listOfFiles[i].getName().substring(0, 13));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM