private static Set<String> flatten(
      String parent, Map<String, Set<String>> flat, Map<String, Set<String>> data) {
    
    Set<String> countries = new LinkedHashSet<>();
    Set<String> keys = data.get(parent);
    if (keys == null) {
      return countries;
    }
    for (String region : keys) {
      if (data.containsKey(region)) {
        Set<String> children = flatten(region, flat, data);
        flat.put(region, children);
        countries.addAll(children);
      } else {
        countries.add(region);
      }
    }
    return countries;
  }
private static Set < String > flatten ( String parent , Map < String , Set < String > > flat , Map < String , Set < String > > data ) { Set < String > countries = new LinkedHashSet < > ( ) ; Set < String > keys = data . get ( parent ) ; if ( keys == null ) { return countries ; } for ( String region : keys ) { if ( data . containsKey ( region ) ) { Set < String > children = flatten ( region , flat , data ) ; flat . put ( region , children ) ; countries . addAll ( children ) ; } else { countries . add ( region ) ; } } return countries ; }
Flatten one level of the territory containment hierarchy.