Caldehyde Caldehyde - 3 months ago 24
Javascript Question

Equivalent data structure in Java?

I came across some javascript containing an interesting "data structure" (I'm not quite sure if that's really what this is).

myApp.controller('MyCtrl', ['$scope', function($scope) {
var dustToLevel = [
{"dust":200, "candy":1, "pkmnLevel": [1, 1.5, 2, 2.5]},
{"dust":400, "candy":1, "pkmnLevel": [3, 3.5, 4, 4.5]},
{"dust":600, "candy":1, "pkmnLevel": [5, 5.5, 6, 6.5]},
{"dust":800, "candy":1, "pkmnLevel": [7, 7.5, 8, 8.5]},
{"dust":1000, "candy":1, "pkmnLevel": [9, 9.5, 10, 10.5]},
{"dust":1300, "candy":2, "pkmnLevel": [11, 11.5, 12, 12.5]},
{"dust":1600, "candy":2, "pkmnLevel": [13, 13.5, 14, 14.5]},
{"dust":1900, "candy":2, "pkmnLevel": [15, 15.5, 16, 16.5]},
{"dust":2200, "candy":2, "pkmnLevel": [17, 17.5, 18, 18.5]},
{"dust":2500, "candy":2, "pkmnLevel": [19, 19.5, 20, 20.5]},
{"dust":3000, "candy":3, "pkmnLevel": [21, 21.5, 22, 22.5]},
{"dust":3500, "candy":3, "pkmnLevel": [23, 23.5, 24, 24.5]},
{"dust":4000, "candy":3, "pkmnLevel": [25, 25.5, 26, 26.5]},
{"dust":4500, "candy":3, "pkmnLevel": [27, 27.5, 28, 28.5]},
{"dust":5000, "candy":3, "pkmnLevel": [29, 29.5, 30, 30.5]},
{"dust":6000, "candy":4, "pkmnLevel": [31, 31.5, 32, 32.5]},
{"dust":7000, "candy":4, "pkmnLevel": [33, 33.5, 34, 34.5]},
{"dust":8000, "candy":4, "pkmnLevel": [35, 35.5, 36, 36.5]},
{"dust":9000, "candy":4, "pkmnLevel": [37, 37.5, 38, 38.5]},
{"dust":10000, "candy":4, "pkmnLevel": [39, 39.5, 40, 40.5]} ];


I was curious if there was an equivalent that could be used in java. Or something like a hashmap that would allow me to associate multiple values with a key.

I'm looking more so for an answer to the question and not reasons why I should already know the answer by the way, thanks.

Answer

i would suggest you creating a class for the structure:

public class DustToLevel
{
    public DustToLevel(int dust, int candy, float[] pkmnLevel)
    {
        this.dust = dust;
        this.candy = candy;
        this.pkmnLevel = pkmnLevel;
    }

    public int dust;
    public int candy;
    public float[] pkmnLevel;
}

your implementation would then:

List<DustToLevel> dustToLevels = new ArrayList<DustToLevel>();
dustToLevels.Add(new DustToLevel(200, 1, new float[] {1.5f, 2.0f, 3.5f}));