2

I'm trying to put some Strings to a HashMap, but they wont add. My code looks like this and I can't seem to understand why they won't add. Can someone help me and give me an explanation to what I'm doing wrong?

HashMap <String, String> akro = new HashMap <String, String>();

public void lesFil() {
    try {
        BufferedReader br = new BufferedReader(new FileReader("akronymer.txt"));
        String line;

        while((line = br.readLine()) != null) {
            if(!line.contains(" ")) {
                continue;
            }
            String[] linje = line.split("\\s+", 2);

            String akronym = linje[0];
            String betydning = linje[1];

//              System.out.println(a + " || " + b);
            akro.put(akronym, betydning);
        }
    } catch(Exception e) {
        System.out.println("Feilen som ble fanget opp: " + e);
    }
}

When I'm removing "//", both akronym and betydning prints out fine.

I tried to add this method to test the HashMap but nothing prints out and the size = 0

public void skrivUt() {
    for(Map.Entry <String, String> entry : akro.entrySet()) {
        System.out.print("Key: " + entry.getKey());
        System.out.println(", Value: " + entry.getValue());

    }
    System.out.println("Antall akronymer: " + akro.size());
}   

Part of the file I'm reading from(txt file):

...
CS        Chip Select  
CS Clear to Send  
CS Code Segment
C/S       Client/Server
...
4
  • 1
    Please show us where lesFil() and skrivUt() are being called from. Commented Nov 26, 2013 at 21:18
  • 1
    Are you sure that this is the same akro? Try printing out akro itself (or its size) right after the put. Commented Nov 26, 2013 at 21:18
  • 1
    Holy f...Just figuered out the problem. Um, kinda embarrassing. Enough coding for today I guess, time for bed.. Commented Nov 26, 2013 at 21:22
  • Use MultiMap .... stackoverflow.com/questions/18704043/hashmap-from-list-of-files/… Commented Nov 26, 2013 at 21:36

1 Answer 1

3

Remember that a Map in Java maps one key to one value. In the sample data you provide, it seems that you have multiple values ("Chip Select", "Clear to Send", "Code Segment") for one key ("CS").

You can solve this by either picking a structure other than a Map, changing what you want to store, or changing the value of the Map to a List<String>.

For example:

List<String> values = akro.get(akronym);
if(values == null) {
   values = new LinkedList<String>();
   akro.put(akronym, values);
}
values.add(betydning);
Sign up to request clarification or add additional context in comments.

3 Comments

I think it does. You can have a key point to an empty list. This also goes by the name of multimap if you're looking for a library solution.
@duffymo If you were to add multiple Key, Value pairs (even with the same Key) to the same HashMap, the size would not be 0.
Sorry, I'm not following you. The Map only allows one value per key. If I make that value a List type, I can add associate multiple values to a single key. I don't understand why you're banging on about size being zero. If I add a single key with an empty list value the size isn't zero - so what?

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.