1

I am using a file that consists of:

"word","wordtype","definition"

"Base","n.","The lower part of a robe or petticoat."

"Base","n.","An apron."

The output is as follows:

key: "base" value: ["word""wordtype""definition", "Base""n.""The lower part of a robe or petticoat.", "Base""n.""An apron."] key: "word" value: ["word""wordtype""definition", "Base""n.""The lower part of a robe or petticoat.", "Base""n.""An apron."]

Desired outcome:

key: "base" value: [ "Base""n.""The lower part of a robe or petticoat.", "Base""n.""An apron."] key: "word" value: ["word""wordtype""definition"]

Can someone point me in the right direction?

BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(file)));

    String line = null;

    TreeMap<String, List<String>> def = new TreeMap<String, List<String>>();
    List<String> values = new ArrayList<String>();

        try {
            while ((line = br.readLine()) != null) {
                String []parts =line.split(",");
                String key = null;
                for (int i = 0; i < parts.length; i++){
                    key = parts[0];
                }

                values.add(parts[0] + parts[1] + parts[2]);
                def.put(key.toLowerCase(), values);

            }
2
  • For starters: key = parts[0] shouldn't be in a loop; values should be a new variable defined inside your while loop, and you should add each part you want added to the values list one at a time, not concatenating them together with +. Commented Apr 3, 2016 at 1:04
  • You can't use a Map for this. One key must correspond to a single (or no) value. If you want similar functionality; you could use a List<Map.Entry<String, String>>. Commented Apr 3, 2016 at 1:17

1 Answer 1

1

A Map cannot work as you request. Any key can only be mapped to a single value.

If you want something akin to what you're doing, but where you can have multiple values for a given key, you could do something like:

List<Map.Entry<String, List<String>>> def = new ArrayList<>();

Map.Entry<String, List<String>> entry = new AbstractMap.SimpleEntry<>(key, list);
def.add(entry);

Then iterate through your def:

for (Map.Entry<String, List<String>> entry : def) {
    System.out.println(String.format("Key: %s.  Values: %s",
            entry.getKey(),
            Arrays.toString(entry.getValue().toArray())));
}

Edit:

For your comment: If you want that, you can always roll your own type to store in the Map (or List if you still need duplicate keys):

class WordDescription {
    final String wordType;
    final String definition;

    WordDescription(String wordType, String definition) {
        this.wordType = wordType;
        definition = definition;
    }

    String getWordType() {
        return wordType;
    }

    String getDefinition() {
        return definition;
    }
}

And use that in a List<Map.Entry<String, WordDescription>>. You can make wordType an enum if there's a pre-defined set of them (adjective, noun, etc.).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, ideally I want the key as the word, and value as the key wordtype and definition, but you cant have duplicate keys in maps
@b.d Edited answer to give further help to what you're looking for.
His edited response is really what you want here. You want to store String->YourCustomObject. Personally I'd add the actual word to your WordDescription object. While you may think it is duplicative because it's already the key to your map you'd end up getting better resuability & encapsulation out of the object in the future. Say you return a WordDescription and it gets passed to another part of your program or stored in another data structure; if you lose the map key, you no longer have any context about what word your object represents.
@dev.glitch Ya I think that is exactly what I need,Thanks

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.