# Multiple Type Dictionaries

**URL:** <https://discourse.processing.org/t/multiple-type-dictionaries/15786>\
**Category:** Beginners\
**Created:** [November 25, 2019, 12:22am UTC](https://discourse.processing.org/t/multiple-type-dictionaries/15786 "2019-11-25T00:22:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KeirMeDear](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@KeirMeDear](https://discourse.processing.org/u/KeirMeDear)\
**Post date:** [November 25, 2019, 12:22am UTC](https://discourse.processing.org/t/multiple-type-dictionaries/15786/1 "2019-11-25T00:22:26Z")

</div>

In processing, is there a way to have a dictionary with multiple types (e.g. String, int, float, etc)

I can’t seem to find if there is or not in the docu, the best I’ve found is single type ones like a float dict, int dict, etc

Many thanks

---

<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:** [November 25, 2019, 12:51am UTC](https://discourse.processing.org/t/multiple-type-dictionaries/15786/2 "2019-11-25T00:51:48Z")

</div>

> [@Mixed Datatype Arrays](https://discourse.processing.org/t/mixed-datatype-arrays/14673/5):
>
> The only way in pure Java is to go w/ datatype [Object](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html). However, in order to read each element from such container, we need to (cast) each element back to its actual datatype. Which of course is not very practical; and therefore, not recommended. The alternative is to create a [class](https://processing.org/reference/class.html) containing all the needed datatypes as fields. // https://Discourse.Processing.org/t/mixed-datatype-arrays/14673/5 // GoToLoop (2019-Oct-13) final Mixed[] mix = { new Mixed("hello", 1, 2, 3.5), new Mixed("mix…

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 29, 2019, 8:28pm UTC](https://discourse.processing.org/t/multiple-type-dictionaries/15786/3 "2019-11-29T20:28:25Z")

</div>

As @GoToLoop indicated, a HashMap with values of type `Object` will do this. It is often a bad idea – most often, wanting to do this indicates a problem with the design – but not always.

If all you need to do in the end is `print` your object then no casting is required. If you plan to operate on your objects by type, then you can use `instanceof` to find those entries with e.g. `Integer` or `String` values, and act only on those entries (or act on those values only in certain ways).

```auto
/**
 * HashMapObject - example hashmap with mixed value types
 * 2019-11-29 Processing 3.4 - Jeremy Douglass
 */
import java.util.Map;
HashMap<String, Object> hm;

void setup() {
  hm = new HashMap<String, Object>();
  hm.put("one", 1); // new Integer(1)
  hm.put("two", "two"); // new String("two")
  hm.put("three", PI); // new Float(PI)

  // print map
  // {one=1, two=two, three=3.1415927} 
  println(hm, "\n");

  // print values
  // 1 two 3.1415927  
  for (Object val : hm.values()) {
    print(val, " ");
  }
  println("\n");

  // print keys and values
  // one: 1
  // two: two
  // three: 3.1415927
  for (Map.Entry me : hm.entrySet()) {
    print(me.getKey() + ": ");
    println(me.getValue());
  }
  println("");

  // sum all Integers or Floats
  // 4.141593
  float sum = 0;
  for (Object val : hm.values()) {
    if (val instanceof Integer) sum += (Integer)val;
    if (val instanceof Float) sum += (Float)val;
  }
  println("sum of integers: ", sum, "\n");

  // get first letter of any String
  // t
  for (Object val : hm.values()) {
    if (val instanceof String) println(((String)val).charAt(0), "\n");
  }

  // get a specific key "one" and handle it based on type
  // 1.234567
  Object val = hm.get("one");
  if (val instanceof Integer) println((Integer)val + 0.234567);
  else println("not an Integer");
}

```

Again, most of the time this is a bad idea. Instead, you should be storing your data in structured ways based on type. Still, mixing types on the fly can sometimes be useful. A scenario where this might make sense is large sparse semi-structured field sets of different types – like a test bank of quiz questions with different answers (“owl”, “3.14”), or an Amazon product page, which might have (but does not necessarily have) keys for count, width, weight, expiration, ISBN, minimum age, allergy-information, nation of origin, etc. etc. – and those fields might have values of all kinds, like strings, dates, integers, floats, et cetera.

That sounds a bit like a row in a spreadsheet – as many cells as you want, each configurable to a different type. In fact, Processing’s Table (and the interface TableRow) is implemented on top of an `Object[] columns`. Depending on your needs, Table might be a great way to define and assemble lists of mixed types and then retrieve them. If you don’t really want columns, however, `HashMap<E, Object>` keeps it simple – it just requires casting to manipulate the retrieved values.
