简体   繁体   中英

Iterating through HashSet empties HashMap entry

The following method gets a "Route" (class name and class method):

public Route getRoute(final String method, final String request) {
    if (hasRoutes) {
        for (Map.Entry<Pattern, HashMap<String, String>> entry : routes) {
            Matcher match = entry.getKey().matcher(request);

            if (match.find()) {
                HashMap<String, String> methods = entry.getValue();

                // ISSUE: Returns FALSE after 1st call of Router.getRoute()
                if (methods.containsKey(method)) {
                    return new Route(match.group("interface"), "TRUE (" + method + " - " + match.group("interface") + "): " + methods.get(method));
                } else {
                    return new Route(match.group("interface"), "FALSE (" + method + " - " + match.group("interface") + "): " + methods.values().toString() + ", SIZE: " + entry.getValue().size());
                }

                //return entry.getValue().containsKey(method) ? new Route(match.group("interface"), entry.getValue().get(method)) : null;
            }
        }
    }

    return null;
}

"routes" is defined as:

private Set<Entry<Pattern, HashMap<String, String>>> routes;

It is a cached representation of a JSON configuration file that defines supported routes, eg:

{
    "^/?(?<interface>threads)/?$": {
        "GET": "list",
        "POST": "create"
    },
    "^/?(?<interface>threads)/(?<id>\\d+)/?$": {
        "GET": "get",
        "POST": "reply",
        "PUT": "edit",
        "PATCH": "edit",
        "DELETE": "delete"
    }
}

EDIT, here's how "routes" is filled from the contents of the JSON file:

    try {
        JsonParser parser = JSONFactory.createJsonParser(in);
        JsonNode root = JSONMapper.readTree(parser);
        Iterator base = root.getFieldNames();
        Iterator node;
        String match, method;
        HashMap<Pattern, HashMap<String, String>> routesMap = new HashMap();

        while (base.hasNext()) {
            match = base.next().toString();

            if (match != null) {
                node = root.get(match).getFieldNames();
                HashMap<String, String> methods = new HashMap();

                while (node.hasNext()) {
                    method = node.next().toString();

                    if (method != null) {
                        methods.put(method, root.get(match).get(method).getTextValue());
                    }
                }

                if (!methods.isEmpty()) {
                    routesMap.put(Pattern.compile(match), methods);
                }
            }
        }

        if (!routesMap.isEmpty()) {
            hasRoutes = true;
            routes = routesMap.entrySet();
        }

        // Help garbage collection
        parser = null;
        root = null;
        base = null;
        node = null;
        match = null;
        method = null;
        routesMap = null;
    } catch (Exception ex) {
    }

EDIT 2, properties in question & init() method:

public final static JsonFactory JSONFactory = new JsonFactory();
public final static ObjectMapper JSONMapper = new ObjectMapper();
public static Router router;
private final Class self = getClass();
private final ClassLoader loader = self.getClassLoader();

public void init(ServletConfig config) throws ServletException {
    super.init(config);

    router = new Router(self.getResourceAsStream("/v1_0/Routes.json"), JSONFactory, JSONMapper);
}

For some reason when accessing the servlet after the first time the HashMap is empty of values. A x.size() returns zero.

This is a rewrite of a PHP application from the ground up so I apologise in advance if the issue is something mundane.

Full source: - Router source - Route source

Iterating through HashSet empties HashMap entry

That doesn't happen. Simply iterating a HashSet has no side-effects on the set's contents.

There is something else going on here that is causing your problem.

Your getOptions() method removes every entry from this map as it iterates. So, after calling getOptions() once, the map is empty.

By the way, assigning null to variables does not "help the garbage collector." The garbage collector knows that when the scope of a variable is exited, that variable no longer references the object. You are actually slowing things down by assigning values ( null ) that can never be read (as well as cluttering your code with counterproductive noise). A good static analysis tool like FindBugs will warn you that this is bad code.

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