HashMap "String, color"

edited August 2014 in Questions about Code

Hi All!

I get unexpected token: > at line 3. Why?

class Mix {

  private HashMap<String, color> hm = new HashMap<String, color>();

  hm.put("910", color(232, 200, 95));
  hm.put("930", color(227, 189, 96));
  hm.put("960", color(212, 131, 92));

}

PS: is there an escape character to use greater/less than symbols in the question title?

Answers

  • Answer ✓

    color is Processing specific and when converting to java code it is replaced in the preprocessor by int, which is a primitive dataype, not an Object. You need an Object type, so in this case that would be Integer. The corrected line is thus:

    HashMap <String, Integer> hm = new HashMap <String, Integer> ();

    Then to answer your next question: "but now I get unexpected token: ( at line 5. Why?". You can't call methods there. You should format your class properly, by using variables, a constructor and methods. The corrected class is:

    class Mix {
      HashMap <String, Integer> hm;
    
      Mix() {
        hm = new HashMap <String, Integer> ();
        hm.put("910", color(232, 200, 95));
        hm.put("930", color(227, 189, 96));
        hm.put("960", color(212, 131, 92));
      }
    }
    
  • edited August 2014 Answer ✓

    A Java's Collection, like Map for example, can't use primitive types!
    In your case, color is a syntactic sugar for primitive int! And its corresponding wrap class is Integer:
    http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html

    final HashMap<String, Integer> hm = new HashMap();

    There's another bug: in order to run statements outside methods, we gotta place them inside curly braces:

    //forum.processing.org/two/discussion/6843/hashmap-string-color
    
    import java.util.Map;
    
    class Mix {
      final Map<String, Integer> hm = new HashMap();
      {
        hm.put("910", color(232, 200, 95));
        hm.put("930", color(227, 189, 96));
        hm.put("960", color(212, 131, 92));
      }
    }
    

    Careful that doing so fails in JS Mode, unless it's not a nested class! And Mix is a nested inner class btW! :-SS

    Alternatively, you can use the Processing 2's new class: IntDict, which accepts primitive types as values:
    http://processing.org/reference/IntDict.html

    Although it still uses a HashMap<String, Integer> internally called indices: 8-X
    https://github.com/processing/processing/blob/master/core/src/processing/data/IntDict.java#L26

    //forum.processing.org/two/discussion/6843/hashmap-string-color
    
    class Mix {
      final IntDict hm = new IntDict
        (new String[] {
        "910", "930", "960"
      }
      , new color[] {
        color(232, 200, 95), 
        color(227, 189, 96), 
        color(212, 131, 92)
      }
      );
    }
    

    Is there an escape character to use greater/less than symbols...

    Well, I use &lt; for "less-than" myself! ;;)

    P.S.: An excellent explanation about data-types. Worth the reading: :D
    docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

  • edited August 2014

    Those keys "910", "930", "960" can be represented as numbers perfectly.
    So why do you wanna them to be String btW? /:) Isn't an Integer key enough?! L-)

    //forum.processing.org/two/discussion/6843/hashmap-string-color
    
    import java.util.Map;
    
    class Mix {
      final Map<Integer, Integer> hm = new HashMap();
      {
        hm.put(910, color(232, 200, 95));
        hm.put(930, color(227, 189, 96));
        hm.put(960, color(212, 131, 92));
      }
    }
    
  • edited August 2014

    BtW, I've found this explanation of mine in some old thread of yours: ;;)
    http://forum.processing.org/two/discussion/6717/passing-variable-name-to-method

    • Java's Collection containers, for example an ArrayList, can only store object references.
    • We can't store primitives like int or float in them.
    • Unless we use their corresponding primitive wrapper: Integer or Float, etc.
    • Unless it's an immutable class, there's no much logic filling up a Collection w/ the same reference.
    • Before populating a Collection, we gotta add() objects to it. Since it starts off empty!
  • edited August 2014

    All right! Thanks a lot, guys.

    amnon:

    You should format your class properly

    Of course. I forgot you must initilize variables in the constructor.

    GoToLoop:

    Those keys "910", "930", "960" can be represented as numbers perfectly.

    Yes, perfectly true, but these are names in a standard a color model, some of which contain letters. The fact that only names made of digits appear in my example is simple awkwardness on my part for the post.

    GoToLoop:

    I've found this explanation of mine in some old thread of yours

    You bet. That's the thread that got the HashMap thing rolling! I didn't know what that type did before.

    Wrap class... Is a wrap class the one another extends from?

    EDIT: OK I read this and I've understood it. Is this what a wrapper class is also called?

  • edited August 2014

    Of course. I forgot you must...

    "Must" is too strong of a word which rarely happens to be true in programming and anything else btW! :>
    Generally, fields are initialized either "in situ" or further ahead inside a constructor or a delegated method for it.

    However, when a field happens to refer to a data container, it's not such a bad idea to have an initialization block right next to it. Keeping both declarations & initializations close together makes code easier to study too! O:-)

  • Wrap class... Is a wrap class the one another extends from?

    A wrapper class is 1 that encapsulates some data in order to be used in some place which otherwise would be incompatible if used "in natura"!

    More specifically I've meant Java's 8 primitive wrapper classes:
    https://en.wikipedia.org/wiki/Primitive_wrapper_class

Sign In or Register to comment.