簡體   English   中英

通過HashSet進行迭代會清空HashMap條目

[英]Iterating through HashSet empties HashMap entry

以下方法獲取“ Route”(類名和類方法):

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;
}

“路線”定義為:

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

它是JSON配置文件的緩存表示,定義了支持的路由,例如:

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

編輯,這是從JSON文件的內容填充“路線”的方式:

    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) {
    }

編輯2,有問題的屬性和init()方法:

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);
}

出於某種原因,第一次訪問HashMap時沒有值。 x.size()返回零。

這是從頭開始重寫的PHP應用程序,因此,如果問題是世俗的,我提前表示歉意。

完整來源:- 路由器來源 - 路由來源

通過HashSet進行迭代會清空HashMap條目

那不會發生。 簡單地迭代HashSet對集合的內容沒有副作用。

這里還有其他事情正在引起您的問題。

您的getOptions()方法會在迭代時從此映射中刪除所有條目。 因此,一次調用getOptions()后,映射為空。

順便說一句,為變量分配null 不會 “幫助垃圾收集器”。 垃圾收集器知道退出變量的范圍時,該變量不再引用該對象。 實際上,您通過分配永遠無法讀取的值( null )來減慢運行速度(以及使代碼產生適得其反的噪音)。 像FindBugs這樣的好靜態分析工具會警告您這是錯誤的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM