Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable keys - fixed length map in Java

Is there a way in Java to create a collection (map) with fixed size and length? I.e., I would like to initialize it with K constant keys (e.g. strings) but still want to be able to change the values.

Edit: The test case has a fixed number of objects, each one corresponds to a number (float). Each time a specific event in the application occurs, I would like to multiply all the numbers in the collection, except the number that corresponds to the object that "caused" the event. The number is not logically an attribiute of the object.

like image 726
DsCpp Avatar asked Sep 03 '18 16:09

DsCpp


People also ask

What is an immutable map in Java?

ImmutableMap, as suggested by the name, is a type of Map which is immutable. It means that the content of the map are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the Map, UnsupportedOperationException is thrown.

Is HashMap immutable?

yes because it is unchangeable. Show activity on this post. If you object is immutable and implements hashcode/equals correctly, you are fine to use them as keys in a hashmap.

Is HashMap fixed size?

Fixed-Size: The maximum amount of items that can be added to the hashmap is fixed by the constructor and the size of the internal hashmap array is also fixed. This means no resizing or rehashing of items.

Is map mutable in Java?

Mutable maps supports modification operations such as add, remove, and clear on it. Unmodifiable Maps are “read-only” wrappers over other maps. They do not support add, remove, and clear operations, but we can modify their underlying map.


1 Answers

I suggest you first look at Mike's answer to get an idea of how to go about solving this problem, then make some changes to the code he provided so it will work in your situation:

import java.util.HashMap;

public class InstrumentedHashMap<K> extends HashMap<K, Float> {

    private static final long serialVersionUID = 1L;
    private int MAX;

    public InstrumentedHashMap(int capacity) {
        super();
        MAX = capacity;
    }

    @Override
    public Float put(K key, Float value) {
        if (super.size() >= MAX && !super.containsKey(key)) {
            return null;
        } else {
            super.put(key, value);
            return value;
        }
    }

    public void event(K trigger, int multiplyAmount, float subtractAmount) {
        super.entrySet().stream().forEach(e -> {
            if (!e.getKey().equals(trigger))
                e.setValue(e.getValue() * multiplyAmount);
            else
                e.setValue(e.getValue() - subtractAmount);
        });
    }
}

You can use the InstrumentedHashMap#event method to handle your "specific event", with the multiplyAmount parameter being the value that you want to multiply your floats by.

like image 183
Cardinal System Avatar answered Oct 14 '22 20:10

Cardinal System