简体   繁体   中英

convert hashmap to lower case

I want to convert hashmap elements into lower case . In my code i have coverted retMap to lower case but its not reflecting when i am printing RetMap .Please help me where m going wrong.

public HashMap<String,String> loadHashmapFromResultset(ResultSet rs, String sKey, String sValue) throws SQLException
{
    //HashMap<String,String> myMap
    HashMap<String,String> RetMap = new HashMap<String,String>();
    while(rs.next()){
        //System.out.print(rs.getString(sKey)+rs.getString(sValue)+", " );
        RetMap.put(rs.getString(sKey.toLowerCase()), rs.getString(sValue.toLowerCase()));
    }
System.out.print(RetMap);
    return RetMap;
}

       results = stmt.executeQuery("select xyz,abc from table1");
        HashMap<String,String> INVS_VALUES = new HashMap<String,String>();
        INVS_VANITY_VALUES=util.loadHashmapFromResultset(results, "xyz", "abc");
        System.out.println("INVS_VALUES: "+INVS_VALUES);

That is because you've changed the case of only the sKey and sValue which are used as column names to fetch String from the ResultSet . You need to convert the case of actual values you're putting the map as key and value to lowercase.

RetMap.put(rs.getString(sKey.toLowerCase()).toLowerCase(), rs.getString(sValue.toLowerCase()).toLowerCase());

In case you mistakenly changed the case of they sKey and sValue , then you can simply use this.

RetMap.put(rs.getString(sKey).toLowerCase(), rs.getString(sValue).toLowerCase());

As mentioned in the comments by sura , you could add a null check before calling the toLowercase() on the Strings returned from the ResultSet to avoid NullPointerException .

采用:

RetMap.put(rs.getString(sKey).toLowerCase(), rs.getString(sValue).toLowerCase());

You lowercase the key and the value you get as parameters:

RetMap.put(rs.getString(sKey.toLowerCase()), rs.getString(sValue.toLowerCase()));

You should lowercase the actual results:

RetMap.put(rs.getString(sKey).toLowerCase(), rs.getString(sValue).toLowerCase());

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