1

I am using queryForList to get a table from my database, which gives me List<Map<String, Object>>. I want to use the two column I select to convert this to a Map<String, Integer>.

At the moment I am doing this

List<Map<String, Object>> customers = jdbc.queryForList("SELECT id, name FROM customers");

Map<String, Integer> customerMap = new HashMap<String, Integer>();

for (Map<String, Object> each : customers)
{
    String name = ((String)each.get("name")).trim();
    Integer id = Integer.valueOf(((BigDecimal)each.get("id")).intValue());

    customerMap.put(name, id);
}

and wondered if there was a better way. Thanks

1
  • may this link help instead of Conversation cant you use map. I just guess. Commented Dec 7, 2012 at 11:59

3 Answers 3

2

This is way too late but just stumble upon this over a search and find maybe can share my code:

   private Map<String, Integer> convertMap(List<Map<String, Object>> input) {
        logger.trace("convertMap");
        Map<String, Integer> dest = new HashMap<>();
        for (Map<String, Object> next : input) {
            for (Map.Entry<String, Object> entry : next.entrySet()) {
                dest.put(entry.getKey(), (Integer) entry.getValue());
            }
        }
        return dest;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You have an unnecessary boxing in this line:

Integer id = Integer.valueOf(((BigDecimal)each.get("id")).intValue());

You should replace it by:

Integer id = ((BigDecimal) each.get("id")).intValue();

3 Comments

This gives me an boxing warning. "The expression of type int is boxed into Integer". Is it better to use Integer.valueOf() or just use @SuppressWarning?
You can store it as an int. The Java Compiler will automatically cast it to an Integer when you store it on the Map (can't test it here, but I'm pretty sure of that).
int id = ((BigDecimal) each.get("id")).intValue(); otherwise.
0

As far as I'm aware of, there isn't another way to do that. Normally I would just encapsulate that code in some entity static method that would convert the mapper to the wanted object(s), something like:

public class Person{
    //  define atributes, constructor, etc

    public static Iterable<Person> ExtractFromMap(List<Map<String, Object>> dataList){
        //  iterate over the List like the example you gave
        //  try to be efficient with your casts and conversions

        LinkedList<Person> ret = new LinkedList<Person>();
        for (Map<String, Object> data : dataList)
        {
            //  using the constructor
            //  or put the code directly here
            ret.add(new Person(data));
        }
        return ret;
    }

    //  you can also create a constructor that receives a Map<String, Object> to
    //  use in the static method
    private Person(Map<String, Object> data){
        //  extract and instantiate your attributes

        this.name = ((String)each.get("name")).trim();
        //  ...
    }
}

If you want, you can try to create a generic method that would use reflection, but to keep it simple, I think this example can give you a good idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.