-1

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?

David Conrad
  • 12,745
  • 1
  • 37
  • 46

2 Answers2

0

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.

  • Compass explaind me here [link](http://stackoverflow.com/a/26611687/2958086) but i don`t know how to make the code. i`m to new to java . – Vlad Enacho Oct 28 '14 at 18:07
  • can you work in python. it can be very easily implemented in python.. or u can use pair of keys in a hashmap like this Map, V> or use Map> map = //... //... map.get(2).get(5); – Mujtaba Hasan Oct 28 '14 at 18:46
  • no. and i choosed java because i must run this program to a Windows 2000 desktop . It`s for work... , i have a camera that can measure objects, like phones,buttons,any object. and the camera choose if the distances are good keeps the object if not she throws them. And camera won`t show the Bad or Good count. It can only write to a document lines like in my example [category] bad=30 (30 are 30 bad pieces) good=10 (10 are 10 good pieces) – Vlad Enacho Oct 28 '14 at 18:52
  • Then use pair of keys in a hashmap like this Map, V> or use `Map> map =`. Second option is more suited for ur problem – Mujtaba Hasan Oct 28 '14 at 18:55
0

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.

David Conrad
  • 12,745
  • 1
  • 37
  • 46
  • honestly i don`t know. Could u show me how to read the value from the Text file? – Vlad Enacho Oct 28 '14 at 18:33
  • @Vlad That's a different question. – David Conrad Oct 28 '14 at 19:09
  • @Vlad There are lots of questions on SE about reading from a text file, [like this one](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file). Search, think, write some code, and if you can't figure it out, ask a question. – David Conrad Oct 28 '14 at 19:37