I want to add to a linkedlist within a hashmap. ex john:--> jack-->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
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);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With