0

I have created a class HidDevice to form objects which concatenates input from each HIDDevice separately until a special key (ENTER) is pressed. So each instance of HidDevice have fields like int deviceId, String concatedInput.

public class HidDevice  {

    private int deviceId = -1;
    private String concatedInput;

    public HidDevice (int id) {
        this.deviceId = id;
        this.concatedInput = null;
    }

    public keypress (Event event) {
        this.concatedInput += event.code;
        // ....
    }

 //....
}

But I don't how I can store these hidDevice instances. There seems to be a lot of possibilities in Java, I'm not sure which to use is the best.

On each keyEvent in my Android activity, I want to call hidProcessInput.myKeyEvent(event) which checks event.getDeviceId() and adds the input to the object with this id. If there is no object for this id, it have to be created.

public class HidProcessInput {

    // Wrong for my case
    private static List<HidDevice> hidDevices = new ArrayList<>();

    // a singleton class
    private static HidProcessInput instance = null;
    public static HidProcessInput getInstances() {
        if (instance == null) {
            instance = new HidProcessInput();
            init();
        }
        return instance;
    }

    public boolean myKeyEvent (KeyEvent event) {
        int deviceId = event.getDeviceId();

        // here create new hidDevice instance if this id not still exist
        /* if (!hidDevices.contains(deviceId)) {
            HidDevice hidDevice = new HidDevice(deviceId);
            hidDevices.add(id,hidDevice);
           }
        */

        // add the character to the the correct hidDevice instance
        /* hidDevices.get(id).keypress(event);
        */
}

So what do I use to store the objects, to be able to access these objects using the deviceId? I tried i.e. ArrayList, but I noticed indexes here can not be freely chosen.

I found things like TreeSet, HashSet or Map, but I'm not sure which to use in such a case?

1

Within the Android environment, the answer is the SparseArray that is an optimized structure for the <Integer, Object> type structure. The primary advantages of are the lack of boxing (all keys for a Map must be Objects rather than primitive) and the lack of the additional Object that is the key. A key reason for the creation of this is its lower memory footprint at the cost of a bit of speed.

There are some caveats that come with the SparseArray in that it is slower than a HashMap for insertion and deleting, though this is not something that one would observe at small sized lists. If this is to store hundreds of values and is identified as a hot spot when profiling, reconsider the other structures.


Within pure Java, you would probably be looking at one of the Map implementations.

HashMap will provide the fastest insertion and lookup, but the slowest iteration over the entries.

LinkedHashMap will provide slightly slower insertion and lookup than HashMap, but its iteration will be faster at the cost of more memory (it maintains a linked list of entries).

TreeMap provides the slowest insertion and lookup, but will give you fast and ordered iteration.

The exact choice between these depends on the expected usage of the structure and desired performance. Note that for small numbers of keys for the maps (up to a few hundred), the performance differences are probably not noticeable.


The general solution is to write a sparse array of your own (wikipedia). This task will occasionally be found in algorithmic interview questions.

  • Looks like SparseArray is the right for me. My array will only need to hold something like 0 - 5 elements. And SparseArray have also a method get(int key) to retrieve my objects in a simple way. I will try it with this. thanks. – Lutz Jul 24 '16 at 13:09
0

You'd use a map if you want to choose the index. Something along the lines of Map exampleMap = new HashMap<String, HidDevice>; This would allow you to use a string as a key to obtain the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.