简体   繁体   中英

What would be the best way to define a Map<String, Map<String, String>> map in Java?

I am porting some PHP code over to Java. I have a class that defines the following map:

class Something {
    protected $channels = array(
        'a1' => array(
            'path_key1' => 'provider_offices',
            'path_key2' => 'provider_offices',
        ),
        'a2' => array(
            'path_key1' => 'provider_users',
            'path_key2' => 'provider_users',
        ),
        'a3' => array(
            'path_key1' => 'provider_offices',
            'path_key2' => 'provider_offices',
        ),
        'a4' => array(
            'path_key1' => 'attri1',
            'path_key2' => 'attri1',
        ),
        'a5' => array(
            'path_key1' => 'attrib2',
            'path_key2' => 'attrib2',
        ),
        'a6' => array(
            'path_key1' => 'diagnosis',
            'path_key2' => 'diagnosis',
        ),
        'a7' => array(
            'path_key1' => 'meds',
            'path_key2' => 'meds',
        ),
        'a8' => array(
            'path_key1' => 'risk1',
            'path_key2' => 'risk1',
        ),
        'a9' => array(
            'path_key1' => 'risk2',
            'path_key2' => 'risk2',
        ),
        'a0' => array(
            'path_key1' => 'visits',
            'path_key2' => 'visits',
        ),
    );
}

In PHP the data was being accessed by doing something like this:

$key = 'a9';
$pathKey2Value = $this->channels[$key]['path_key2'];

I started to implement a Map<String, Map<String, String>> member in Java but it seemed overly complicated, as well as defining A1 , A2 , etc classes.

What would be the most elegant way to accomplish the same thing in Java?

enum An {a1, a2, a3, a0}

class PathKeys {
    String pathKey1;
    String pathKey2;

    PathKeys(String pathKey1, String pathKey2) {
        this.pathKey1 = pathKey1;
        this.pathKey2 = pathKey2;
    }
}

Map<An, PathKeys> channels = new EnumMap<>(An.class);

void put(An an, PathKeys pk) {
    channels.put(an, pk);
}

put(An.valueOf("a1"), new PathKeys("provider_offices", "provider_offices"));

String s = channels.get(An.a1).pathKey1;
  • An Enum is type-safe.
  • An EnumMap is actually an array.
  • PathKeys I was not sure about, but it looks like fixed fields of its own structure.

Translating from Perl, JSON, simple PHP to a typed language like java should utilize adequate data structures and type information. It is easier to afterwards lessen the restrictions, but for the time being helps in finding errors in the business logic. So save a Map+String generalisation for last.

如果可以包括第三方库,则添加Guava并使用Table代替

This do the tricks :

 Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();

 private HashMap<String, String> loadMap(String[] str){
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(str[0], str[1]);
    map.put(str[2], str[3]);
    return map;
 }

  map.put("a1", loadMap(new String[]{"path_key1", "provider_offices", "path_key2", "provider_offices"}));
  map.put("a2", loadMap(new String[]{"path_key1", "attri1", "path_key2", "attri1"}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM