abhinavrathore019
abhinavrathore019

Reputation: 6635

Best Practice to store KEY-VALUE pairs in JAVA

I want to store following data for comparison in Code

Cardinal Direction Degree Direction

N                348.75 - 11.25
NNE              11.25 - 33.75
NE               33.75 - 56.25
ENE              56.25 - 78.75
E                78.75 - 101.25
ESE              101.25 - 123.75
SE               123.75 - 146.25
SSE              146.25 - 168.75
S                168.75 - 191.25
SSW              191.25 - 213.75
SW               213.75 - 236.25
WSW              236.25 - 258.75
W                258.75 - 281.25
WNW              281.25 - 303.75
NW               303.75 - 326.25
NNW              326.25 - 348.75

OR

500   chance_of_rain
600   chance_of_snow
960   chance_of_storm
801   cloudy
615   flurries
741   fog
721   haze
611   icy
701   mist
804   mostly_cloudy
800   mostly_sunny
803   partly_cloudy
500   rain
711   smoke
601    snow   
960   storm
800   sunny
200   thunderstorm

What is the best way to store such 2-D Array data information in android ? either as res or src

NOTES: -

I am just curious whats the best way to store such constant data?

Upvotes: 0

Views: 981

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109547

private static final String[] DIRECTIONS = {
    "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
    "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
};

String degreeToName(double degree) {
    int corner = (int)((degree + 11.25) / 22.5) % 16; // Or % DIRECTIONS.length
    return DIRECTIONS[corner];
}

String degreeToRange(double degree) {
    int corner = (int)((degree + 11.25) / 22.5) % 16; // Or % DIRECTIONS.length
    double lowerBound = corner * 22.5 - 11.25 + (corner == 0 ? 360 : 0);
    double upperBound = corner * 22.5 + 11.25;
    return String.format("%.2f - %.2f", lowerBound, upperBound);
}

A calculation seems best suited.

Under circumstances harden the code with:

while (degree < 0) degree += 360;

For completeness sake added degreeToRange.

Upvotes: 2

user370305
user370305

Reputation: 109237

I don't know exact but you can use something like,

NavigableMap from Java 6. Which has ceilingEntry() and floorEntry() functions.

Upvotes: 2

Related Questions