开发者

Retrieving objects from a Map

开发者 https://www.devze.com 2023-02-10 05:36 出处:网络
Guys, this is silly, but i got to ask You have aprivate Map<String, String> dbMap = new HashMap<String, String>();

Guys, this is silly, but i got to ask

You have a private Map<String, String> dbMap = new HashMap<String, String>();

i am putting stuff in after having read the file like:

  while ((line = br.readLine()) != null) {
            ...
        int ix = line.indexOf("=");
        String key = line.substring(0, ix);
        String value = li开发者_Python百科ne.substring(ix + 1, line.length());
            ...
        dbMap.put(key, value);
        }

Once we done, you print it and your map shows what's inside

System.out.println(fc.getDbMap().keySet());

{MAX_DB_CONNECTIONS = something, DATABASE_PASSWD = something else}

when i try to get value assigned to particular key, i get null, when indeed key exists

Why?

System.out.println(fc.getDbMap().containsKey("DATABASE_PASSWD"));
false

System.out.println(fc.getDbMap().get("DATABASE_PASSWD"));
null


You are printing out keySet() and NOT the Map (as per code), so your keys look like "MAX_DB_CONNECTIONS = something" etc (or maybe the whole thing is key) -- yet you expect keys to be "MAX_DB_CONNECTIONS". So you are building your Map incorrectly (or library you are asking to fill it).

If these are read from a property file, perhaps you are using wrong separator?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号