Search code examples
javanetbeansstructuremultimapfile-io

Formula solution program


Below is an example of the input I am dealing with:

A linkedHashMap with String,Double as parameters

NumericMap: { p=20.0, r=5.0, i=2.0}

And file input:

p = i * i * r

What I now need to do is take the file input and store it in the program as a data structure that allows duplicates and maintains the order of the items from the file, based on insertion. From there I need to be able to apply the doubles in the relevant order into the file input, resulting in:

20 = 2 * 2 * 5

From here I need to say:

If(the file input structure .contains("*"){
Double1 = the double in front of the *
Double2 = the double after the *
Answer1 = Double1 * Double2
}

So far I have managed to achieve all of this by putting the file input into a linkedHashMap as well, but linkedHashMaps do not allow for duplicate entries. Someone suggested to me to use a multiMap like so:

Multimap map = ArrayListMultimap.create();
map.put(key, value1);
map.put(key, value2);
List values = map.get(key);

But unfortunately this just returns errors. I have also attempted to do this with arrayLists, but obviously arrayLists do not maintain there order once adjusted.

Any ideas on what I can use for my file input to obtain all of the above and how I should go about doing it.

Regards


Solution

  • You can code your own multiple map class. Here is one of the implementation:

    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    
    
    public class MultiMap<K, V> {
        Map<K, List<V>> map = new HashMap<K, List<V>>();
    
        public void put(K key, V value) {
            List<V> values = null;
            if(!map.containsKey(key)) {
                values = new LinkedList<V>();
                map.put(key, values);
            } else {
                values = map.get(key);
            }
    
            assert(values != null);
            values.add(value);
        }
    
        public List<V> get(K key) {
            return map.get(key);
        }
    }