DEV Community

Discussion on: The Java Preferences API Is a Little Thing That is a Huge Benefit

Collapse
 
easygoing1 profile image
Michael Sims

Hi, you said, "You can only associate 1 name-value pair with each node (no Collections-based values are allowed)" And I'm wondering if this is still considered a 1 name-value pair?

Where you can combine two values under one key and recall them like this:

int x, y;
...
prefs.put(POSITION, x + "," + y);

static int X_DEFAULT = 50, Y_DEFAULT = 25;
void baz() {
    String position = prefs.get(POSITION, X_DEFAULT + "," + Y_DEFAULT);
    int x, y;
    try {
        int i = position.indexOf(',');
        x = Integer.parseInt(coordinates.substring(0, i));
        y = Integer.parseInt(position.substring(i + 1));
    } catch(Exception e) {
        // Value was corrupt, just use defaults
        x = X_DEFAULT;
        y = Y_DEFAULT;
    }
    ...
}
Enter fullscreen mode Exit fullscreen mode