akash
akash

Reputation: 105

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

Upvotes: 0

Views: 4499

Answers (3)

Pieter
Pieter

Reputation: 903

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

Upvotes: 0

Bhoot
Bhoot

Reputation: 2633

Use:

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

Upvotes: 0

Rahul
Rahul

Reputation: 45060

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.

Upvotes: 1

Related Questions