HashMap help, Read from file print to console or JTextArea

懵懂的女人 提交于 2019-12-13 08:34:51

问题


I'm having a problem using HashMap. I have test.txt with this data:

[Computers]
Keyboard=10
Mouse=5
[Cars]
Lamborghini=6
BMW=3

where the [Computers] & [Cars] are Category, Keyboard, Mouse, Lamborghini and BMW are descriptors, and 10, 5, 6, 3 are values for each descriptor.

How can I make a HashMap that can System.out.println("Category" + "descriptor" + value) on console or in JTextArea when clicking a button?


回答1:


I think it can be done using two levels of hashmaps.

On first hashmap level, use category as your key and for its value, use another hashmap whose keys would be the descriptors.




回答2:


What you want is a multimap, a map that can have more than one value for each key, since you can have multiple (descriptor, value) pairs for each category. There are multimaps in some third party Java libraries such as Guava, but you can emulate one with the standard Java collections by using a map to a list of values, such as a Map<String, List<Descriptor>>, where a Descriptor is a type that contains a (descriptor, value) pair.

The merge method that was added to Map in Java 8 is effective for building such a map.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Map;

public class Descriptor {
    private final String descriptor;
    private final int value;

    public Descriptor(String descriptor, int value) {
        this.descriptor = Objects.requireNonNull(descriptor);
        this.value = value;
    }

    public String getDescriptor() {
        return descriptor;
    }

    public int getValue() {
        return value;
    }

    @Override public String toString() {
        return descriptor + ", " + value;
    }

    public static void main(String[] args) {
        // create map (essentially a multimap
        Map<String, List<Descriptor>> map = new HashMap<>();

        insert(map, "Computers", "Keyboard", 10);
        insert(map, "Computers", "Mouse", 5);
        insert(map, "Cars", "Lamborghini", 6);
        insert(map, "Cars", "BMW", 3);

        // display all descriptors for a category
        String category = "Cars";
        for (Descriptor desc : map.get(category)) {
            System.out.println(category + ", " + desc);
        }
    }

    public static void insert(Map<String, List<Descriptor>> map,
            String category, String descriptor, int value) {
        // insert items in the map using Map.merge
        List<Descriptor> list = new ArrayList<>();
        list.add(new Descriptor(descriptor, value));
        map.merge(category, list, Descriptor::concat);
    }

    public static List<Descriptor> concat(List<Descriptor> one,
            List<Descriptor> two) {
        one.addAll(two);
        return one;
    }
}

I assume that you can handle reading and parsing the input file yourself, or that you intent to work that out separately, since your question specifically asks about "HashMap help" and says that you have a problem using HashMap to solve this problem.



来源:https://stackoverflow.com/questions/26613602/hashmap-help-read-from-file-print-to-console-or-jtextarea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!