java 添加到 HashMap<String, LinkedList> 中的链表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26478646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:59:24  来源:igfitidea点击:

Adding to a linkedList in a HashMap<String, LinkedList>

javalinked-listhashmap

提问by johanvdv

I want to add to a linkedlist within a hashmap. ex john:--> Hyman-->black-->crack susan:--> sally,sammy,silly ect

我想添加到哈希图中的链表。ex john:--> Hyman-->black-->crack susan:--> sally,sammy,silly ect

Im not quite sure how to do this. Do i need a new linkedList for each name and if so how do i dynamically create one. Here is some sample code i made to try.

我不太确定如何做到这一点。我是否需要为每个名称创建一个新的链接列表,如果需要,我如何动态创建一个。这是我尝试的一些示例代码。

import java.util.*;
import java.io.*;
public class test {
    public static void main(String args[]) throws FileNotFoundException{
        HashMap<String, LinkedList<String>> testMap =  new HashMap<String, LinkedList<String>>();
        File testFile = new File("testFile.txt");
        Scanner enterFile = new Scanner(testFile);
        String nextline = "";
        LinkedList<String> numberList = new LinkedList<String>();
        int x = 0;
        while(enterFile.hasNextLine()){
            nextline = enterFile.nextLine();
            testMap.put(nextline.substring(0,1),numberList);
            for(int i = 1; i < nextline.length() - 1; i++){
                System.out.println(nextline);
                testMap.put(nextline.substring(0,1),testMap.add(nextline.substring(i,i+1)));
            }
            x++;
        }
        LinkedList<String> printHashList = new LinkedList<String>();
        printHashList = testMap.get(1);
        if(printHashList.peek() != "p"){
            System.out.println(printHashList.peek());
        }
    }
}

Srry if this is not a good post this is my first one

对不起,如果这不是一个好帖子,这是我的第一个帖子

采纳答案by Alex Suo

public void putToMap(String name) {
    String firstLetter = name.substring(0, 1);

    List<String> names = testMap.get(firstLetter);
    if (names == null) {
        names = new LinkedList<String> ();
        testMap.put(firstLetter, names);
    }

    names.add(name);
}

回答by Chris

Alex's answer is the common (and most lightweight) solution to your problem, and the one you should choose as your answer(if he modifies it as per my comment), but just wanted to make you aware that another solution is to use a LinkedListMultiMap (which is a class in the Guava library).

亚历克斯的答案是您问题的常见(也是最轻量级的)解决方案,您应该选择作为答案(如果他按照我的评论对其进行修改),但只是想让您知道另一种解决方案是使用 LinkedListMultiMap (这是番石榴库中的一个类)。

LinkedListMultiMap is a simple way to deal with maps of lists (but carries an additional library overhead of the Guava library). You can add multiple values individually per key, which I believe is your desired behaviour.

LinkedListMultiMap 是一种处理列表映射的简单方法(但会带来 Guava 库的额外库开销)。您可以为每个键单独添加多个值,我相信这是您想要的行为。