5

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

1
  • 1
    This is a much better first post than many I have seen, keep up the good work Commented Oct 21, 2014 at 4:00

2 Answers 2

2
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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Right algorithm, but method should be static, and should pass the testMap as a parameter (if accessing from the main method).
Yeah. I just don't care ... :)
Guava is a very fat library to include for only this one feature. I use this method
1

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 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.

2 Comments

I remember Google Multimap used to be quite popular. Not sure if still being used now as I have a strong tendency to choose simple solutions unless the cost to write it up doesn't justify...
I was simply offering another solution for reference purposes. Personally, I'd choose your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.