# Best and fastest way to access objects in real-time for interaction with incoming messages

**URL:** <https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107>\
**Category:** Libraries\
**Created:** [July 26, 2022, 9:31am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107 "2022-07-26T09:31:31Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedpep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedpep/32/52_2.png) [@fedpep](https://discourse.processing.org/u/fedpep)\
**Post date:** [July 26, 2022, 9:31am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/1 "2022-07-26T09:31:31Z")

</div>

Hi everybody, I recently started working on a project I had had in mind for quite some time. I have reached a good point but now that I have to integrate the interactive part, I have a doubt.

This is the idea: I’m creating objects dynamically and currently storing them in an ArrayList. My goal is to use the MIDI Bus library to animate each object based on incoming MIDI messages (Note on/Note off). For those that are not familiar with MIDI messages: the note on includes a note number (range could be: 0-127), a note velocity (0-127) and the message “On” when the note is played and “Off” when it stops.

One of the properties of the objects I created is the MIDI note to associate to each object as an integer number. This property is not set sequentially so, for example, the midi note associated with the object 0 in the array could be 24, the one associated with the object 1 could be 12 etc.

Now: since my goal is to achieve real-time interaction, what’s the best way to find the object in my ArrayList with the correct MIDI note? To continue with the example given earlier: when the incoming MIDI note is = 12, the object in the array position = 1 is animated.

I don’t think that looping through the whole array is the best solution. Could it be using Hashmaps?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 26, 2022, 9:41am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/2 "2022-07-26T09:41:27Z")

</div>

> [@fedpep](#):
>
> Could it be using Hashmaps?

Absolutely!

> [@fedpep](#):
>
> When the incoming MIDI note is = 12, the object in the array position = 1 is animated.

In a HashMap, the value 12 would be directly mapped to the “animated” object.

---

<div class="post-metadata">

**Author:** ![fedpep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedpep/32/52_2.png) [@fedpep](https://discourse.processing.org/u/fedpep)\
**Post date:** [July 26, 2022, 9:47am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/3 "2022-07-26T09:47:52Z")

</div>

Good! I’m glad it’s as I thought. I’d like to ask you one more thing: since the interactive part only concerns MIDI but for other properties I’d like the objects to also be in the ArrayList I’ve already created, does it make sense to have both the ArrayList and the HashMap working in parallel? I mean, when working with MIDI I refer to the HashMap, while for everything else I continue to use the ArrayList as I am doing now?

Could there be any possible disadvantages with this approach?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 26, 2022, 9:54am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/4 "2022-07-26T09:54:21Z")

</div>

> [@fedpep](#):
>
> since the interactive part only concerns MIDI but for other properties I’d like the objects to also be in the ArrayList I’ve already created,

If 1 MIDI from the Map would correlate to 1 object from the List you should unify them as 1 Map.

That is, you create a class which would contain both the MIDI and the other objects and have a Map container for it.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 26, 2022, 10:27am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/5 "2022-07-26T10:27:09Z")

</div>

You might consider a [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) - effectively a HashMap with a LinkedList which stores the order the objects were put in the HasHMap.

You have the speed of the map combined with a guaranteed iteration order

---

<div class="post-metadata">

**Author:** ![fedpep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedpep/32/52_2.png) [@fedpep](https://discourse.processing.org/u/fedpep)\
**Post date:** [July 26, 2022, 10:41am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/6 "2022-07-26T10:41:48Z")

</div>

> [@GoToLoop](#):
>
> If 1 MIDI from the Map would correlate to 1 object from the List you should unify them as 1 Map.
> 
> That is, you create a class which would contain both the MIDI and the other objects and have a Map container for it.

You’re right but the problem is that I am assigning the MIDI note manually _after_ the object is created with a dropdown menu created in CP5. So maybe I will have to keep both ArrayList and Hashmap. I will definitely write all the code to test my ideas and then refactor it at some point in the future.

> [@quark](#):
>
> You might consider a [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) - effectively a HashMap with a LinkedList which stores the order the objects were put in the HasHMap.
> 
> You have the speed of the map combined with a guaranteed iteration order

Thanks! Now, thanks to you, I remembered that in a past project I used LinkedHashMap but I definitely need to study the documentation again.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 26, 2022, 10:55am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/7 "2022-07-26T10:55:46Z")

</div>

> [@quark](#):
>
> You have the speed of the map combined with a guaranteed iteration order.

LinkedHashMap is a subclass which `extends` HashMap, so I’m suspicious whether it could be any speedier than its parent class.

Besides, I can’t remember seeing any sketch in this forum which required a container to have insertion order in order to work, given display coordinates are obtained from _x_ & _y_ fields, not indices or keys.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 26, 2022, 11:05am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/8 "2022-07-26T11:05:01Z")

</div>

> [@fedpep](#):
>
> … I am assigning the MIDI note manually _after_ the object is created with a dropdown menu created in CP5.

Can’t you pre-create all the MIDI notes you’re gonna need?  
If not, you can also instantiate a class which would create that MIDI note and store it in the HashMap.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 26, 2022, 11:14am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/9 "2022-07-26T11:14:24Z")

</div>

LinkedHashMap vs HashMap

1. Will be slightly slower when inserting data into the map because of the need to maintain the linkedlist
2. Will be the same speed when retrieving a map element because it doesn’t need to access the linkedlist (any difference would be the order of nanoseconds)
3. Provides a guaranteed order when iterating over the map elements which is not the case with the HashMap

> [@GoToLoop](#):
>
> Besides, I can’t remember seeing any sketch in this forum which required a container to have insertion order in order to work

I agree and I have never had the need to use one myself ,but the OP obviously has 🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 26, 2022, 11:27am UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/10 "2022-07-26T11:27:17Z")

</div>

> [@quark](#):
>
> , but the OP obviously has.

Well, I’m proposing a unified HashMap container for both the MIDI objects and the “animated” objects, which would be represented by a unified class.

Now for a dual container approach, where 1 is indexed-based and the other is key-based, still it doesn’t necessarily mean insertion order would be strictly required; although it’s a valid alternative, however a bit fragile, for coupling both containers.

---

<div class="post-metadata">

**Author:** ![fedpep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedpep/32/52_2.png) [@fedpep](https://discourse.processing.org/u/fedpep)\
**Post date:** [July 26, 2022, 12:51pm UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/11 "2022-07-26T12:51:25Z")

</div>

> [@GoToLoop](#):
>
> Can’t you pre-create all the MIDI notes you’re gonna need?  
> If not, you can also instantiate a class which would create that MIDI note and store it in the HashMap.

No, because my idea is to work on the visualisation in Processing at the same time as the music is made. So I cannot know in advance how many and which MIDI messages I will send to Processing. I will constantly tweak the sketch until I reach a satisfying result.

The sketch could also be used and tweaked in live performances

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 26, 2022, 12:58pm UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/12 "2022-07-26T12:58:05Z")

</div>

> [@fedpep](#):
>
> So I cannot know in advance how many and which MIDI messages I will send to Processing.

So there’s no clear link between an incoming MIDI note and the “animation” object.  
I suppose you have a separate algorithm that decides which “animation” to display for each MIDI note.  
In such case a 2-container approach seems appropriate.

---

<div class="post-metadata">

**Author:** ![fedpep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedpep/32/52_2.png) [@fedpep](https://discourse.processing.org/u/fedpep)\
**Post date:** [July 26, 2022, 1:24pm UTC](https://discourse.processing.org/t/best-and-fastest-way-to-access-objects-in-real-time-for-interaction-with-incoming-messages/38107/13 "2022-07-26T13:24:39Z")

</div>

> [@GoToLoop](#):
>
> So there’s no clear link between an incoming MIDI note and the “animation” object.  
> I suppose you have a separate algorithm that decides which “animation” to display for each MIDI note.  
> In such case a 2-container approach seems appropriate.

I haven’t started working on the animation yet but the idea is that the animation is “embedded” in the object itself (as a function or something similar… I was also looking at the Ani Library) and the incoming MIDI note is just a trigger. Each object has his own animation that starts when a specific midi note is received. As I said before, I’m choosing with a dropdown menu which MIDI message each object will “listen”.

This was an experiment from 2017:

[![](https://img.youtube.com/vi/eqFHk1_AN84/maxresdefault.jpg "Processing e Ableton: sincronizzare musica e visual via MIDI") ](https://www.youtube.com/watch?v=eqFHk1_AN84)

And this is a repository from a couple of year ago:

> **[GitHub - federico-pepe/Processing-and-MIDI: 🎵 Create music visualization with...](https://github.com/federico-pepe/Processing-and-MIDI)**
>
> 🎵 Create music visualization with incoming MIDI data - federico-pepe/Processing-and-MIDI
