Java: How to convert List to Map


Recently I have conversation with a colleague about what would be the optimal way to convert List to Map in Java and if there any specific benefits of doing so.

I want to know optimal conversion approach and would really appreciate if any one can guide me.

Is this good approach:

List<Object[]> results;
Map<Integer, String> resultsMap = new HashMap<Integer, String>();
for (Object[] o : results) {
    resultsMap.put((Integer) o[0], (String) o[1]);

All Answers
  • Translate
    List<Item> list;
    Map<Key,Item> map = new HashMap<Key,Item>();
    for (Item i : list) map.put(i.getKey(),i);

    Assuming of course that each Item has a getKey() method that returns a key of the proper type.

  • Translate

    With , you'll be able to do this in one line using streams, and the Collectors class.

    Map<String, Item> map =, item -> item));

    Short demo:

    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    public class Test{
        public static void main (String [] args){
            List<Item> list = IntStream.rangeClosed(1, 4)
                                       .collect(Collectors.toList()); //[Item [i=1], Item [i=2], Item [i=3], Item [i=4]]
            Map<String, Item> map = 
      , item -> item));
            map.forEach((k, v) -> System.out.println(k + " => " + v));
    class Item {
        private final int i;
        public Item(int i){
            this.i = i;
        public String getKey(){
            return "Key-"+i;
        public String toString() {
            return "Item [i=" + i + "]";


    Key-1 => Item [i=1]
    Key-2 => Item [i=2]
    Key-3 => Item [i=3]
    Key-4 => Item [i=4]

    As noted in comments, you can use Function.identity() instead of item -> item, although I find i -> i rather explicit.

    And to be complete note that you can use a binary operator if your function is not bijective. For example let's consider this List and the mapping function that for an int value, compute the result of it modulo 3:

    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6);
    Map<String, Integer> map = -> String.valueOf(i % 3), i -> i));

    When running this code, you'll get an error saying java.lang.IllegalStateException: Duplicate key 1. This is because 1 % 3 is the same as 4 % 3 and hence have the same key value given the key mapping function. In this case you can provide a merge operator.

    Here's one that sum the values; (i1, i2) -> i1 + i2; that can be replaced with the method reference Integer::sum.

    Map<String, Integer> map = -> String.valueOf(i % 3), 
                                       i -> i, 

    which now outputs:

    0 => 9 (i.e 3 + 6)
    1 => 5 (i.e 1 + 4)
    2 => 7 (i.e 2 + 5)

    Hope it helps! :)

  • Translate

    Just in case this question isn't closed as a duplicate, the right answer is to use Google Collections:

    Map<String,Role> mappedRoles = Maps.uniqueIndex(yourList, new Function<Role,String>() {
      public String apply(Role from) {
        return from.getName(); // or something else

  • Translate

    Since Java 8, the answer by @ZouZou using the Collectors.toMap collector is certainly the idiomatic way to solve this problem.

    And as this is such a common task, we can make it into a static utility.

    That way the solution truly becomes a one-liner.

     * Returns a map where each entry is an item of {@code list} mapped by the
     * key produced by applying {@code mapper} to the item.
     * @param list the list to map
     * @param mapper the function to produce the key from a list item
     * @return the resulting map
     * @throws IllegalStateException on duplicate key
    public static <K, T> Map<K, T> toMapBy(List<T> list,
            Function<? super T, ? extends K> mapper) {
        return, Function.identity()));

    And here's how you would use it on a List<Student>:

    Map<Long, Student> studentsById = toMapBy(students, Student::getId);

  • Translate

    Using Java 8 you can do following :

    Map<Key, Value> result= results

    Value can be any object you use.

  • Translate

    A List and Map are conceptually different. A List is an ordered collection of items. The items can contain duplicates, and an item might not have any concept of a unique identifier (key). A Map has values mapped to keys. Each key can only point to one value.

    Therefore, depending on your List's items, it may or may not be possible to convert it to a Map. Does your List's items have no duplicates? Does each item have a unique key? If so then it's possible to put them in a Map.

  • Translate

    There is also a simple way of doing this using Maps.uniqueIndex(...) from Google libraries

  • Translate

    Alexis has already posted an answer in Java 8 using method toMap(keyMapper, valueMapper). As per doc for this method implementation:

    There are no guarantees on the type, mutability, serializability, or thread-safety of the Map returned.

    So in case we are interested in a specific implementation of Map interface e.g. HashMap then we can use the overloaded form as:

    Map<String, Item> map2 =
          , //key for map
                            Function.identity(),    // value for map
                            (o,n) -> o,             // merge function in case of conflict with keys
                            HashMap::new));         // map factory - we want HashMap and not any Map implementation

    Though using either Function.identity() or i->i is fine but it seems Function.identity() instead of i -> i might save some memory as per this related answer.

  • Translate

    Universal method

    public static <K, V> Map<K, V> listAsMap(Collection<V> sourceList, ListToMapConverter<K, V> converter) {
        Map<K, V> newMap = new HashMap<K, V>();
        for (V item : sourceList) {
            newMap.put( converter.getKey(item), item );
        return newMap;
    public static interface ListToMapConverter<K, V> {
        public K getKey(V item);

  • Translate

    Without java-8, you'll be able to do this in one line Commons collections, and the Closure class

    List<Item> list;
    Map<Key, Item> map  = new HashMap<Key, Item>>(){{
        CollectionUtils.forAllDo(list, new Closure() {
            public void execute(Object input) {
                Item item = (Item) input;
                put(i.getKey(), item);

  • Translate

    Many solutions come to mind, depending on what you want to achive:

    Every List item is key and value

    for( Object o : list ) {

    List elements have something to look them up, maybe a name:

    for( MyObject o : list ) {

    List elements have something to look them up, and there is no guarantee that they are unique: Use Googles MultiMaps

    for( MyObject o : list ) {

    Giving all the elements the position as a key:

    for( int i=0; i<list.size; i++ ) {


    It really depends on what you want to achive.

    As you can see from the examples, a Map is a mapping from a key to a value, while a list is just a series of elements having a position each. So they are simply not automatically convertible.

  • Translate

    Here's a little method I wrote for exactly this purpose. It uses Validate from Apache Commons.

    Feel free to use it.

     * Converts a <code>List</code> to a map. One of the methods of the list is called to retrive
     * the value of the key to be used and the object itself from the list entry is used as the
     * objct. An empty <code>Map</code> is returned upon null input.
     * Reflection is used to retrieve the key from the object instance and method name passed in.
     * @param <K> The type of the key to be used in the map
     * @param <V> The type of value to be used in the map and the type of the elements in the
     *            collection
     * @param coll The collection to be converted.
     * @param keyType The class of key
     * @param valueType The class of the value
     * @param keyMethodName The method name to call on each instance in the collection to retrieve
     *            the key
     * @return A map of key to value instances
     * @throws IllegalArgumentException if any of the other paremeters are invalid.
    public static <K, V> Map<K, V> asMap(final java.util.Collection<V> coll,
            final Class<K> keyType,
            final Class<V> valueType,
            final String keyMethodName) {
        final HashMap<K, V> map = new HashMap<K, V>();
        Method method = null;
        if (isEmpty(coll)) return map;
        notNull(keyType, Messages.getString(KEY_TYPE_NOT_NULL));
        notNull(valueType, Messages.getString(VALUE_TYPE_NOT_NULL));
        notEmpty(keyMethodName, Messages.getString(KEY_METHOD_NAME_NOT_NULL));
        try {
            // return the Method to invoke to get the key for the map
            method = valueType.getMethod(keyMethodName);
        catch (final NoSuchMethodException e) {
            final String message =
            logger.error(message, e);
            throw new IllegalArgumentException(message, e);
        try {
            for (final V value : coll) {
                Object object;
                object = method.invoke(value);
                final K key = (K) object;
                map.put(key, value);
        catch (final Exception e) {
            final String message =
            logger.error(message, e);
            throw new IllegalArgumentException(message, e);
        return map;

  • Translate

    You can leverage the streams API of Java 8.

    public class ListToMap {
      public static void main(String[] args) {
        List<User> items = Arrays.asList(new User("One"), new User("Two"), new User("Three"));
        Map<String, User> map = createHashMap(items);
        for(String key : map.keySet()) {
          System.out.println(key +" : "+map.get(key));
      public static Map<String, User> createHashMap(List<User> items) {
        Map<String, User> map =, Function.identity()));
        return map;

    For more details visit:

  • Translate

    like already said, in java-8 we have the concise solution by Collectors:

    and also, you can nest multiple group passing an other groupingBy method as second parameter:
             groupingBy(Item::getKey, groupingBy(Item::getOtherKey))

    In this way, we'll have multi level map, like this: Map<key, Map<key, List<Item>>>

  • Translate

    A Java 8 example to convert a List<?> of objects into a Map<k, v>:

    List<Hosting> list = new ArrayList<>();
    list.add(new Hosting(1, "", new Date()));
    list.add(new Hosting(2, "", new Date()));
    list.add(new Hosting(3, "", new Date()));
    //example 1
    Map<Integer, String> result1 =
        Collectors.toMap(Hosting::getId, Hosting::getName));
    System.out.println("Result 1 : " + result1);
    //example 2
    Map<Integer, String> result2 =
        Collectors.toMap(x -> x.getId(), x -> x.getName()));

    Code copied from:

  • Translate

    I like Kango_V's answer, but I think it's too complex. I think this is simpler - maybe too simple. If inclined, you could replace String with a Generic marker, and make it work for any Key type.

    public static <E> Map<String, E> convertListToMap(Collection<E> sourceList, ListToMapConverterInterface<E> converterInterface) {
        Map<String, E> newMap = new HashMap<String, E>();
        for( E item : sourceList ) {
            newMap.put( converterInterface.getKeyForItem( item ), item );
        return newMap;
    public interface ListToMapConverterInterface<E> {
        public String getKeyForItem(E item);

    Used like this:

            Map<String, PricingPlanAttribute> pricingPlanAttributeMap = convertListToMap( pricingPlanAttributeList,
                    new ListToMapConverterInterface<PricingPlanAttribute>() {
                        public String getKeyForItem(PricingPlanAttribute item) {
                            return item.getFullName();
                    } );

  • Translate

    Apache Commons MapUtils.populateMap

    If you don't use Java 8 and you don't want to use a explicit loop for some reason, try MapUtils.populateMap from Apache Commons.


    Say you have a list of Pairs.

    List<ImmutablePair<String, String>> pairs = ImmutableList.of(
        new ImmutablePair<>("A", "aaa"),
        new ImmutablePair<>("B", "bbb")

    And you now want a Map of the Pair's key to the Pair object.

    Map<String, Pair<String, String>> map = new HashMap<>();
    MapUtils.populateMap(map, pairs, new Transformer<Pair<String, String>, String>() {
      public String transform(Pair<String, String> input) {
        return input.getKey();

    gives output:

    {A=(A,aaa), B=(B,bbb)}

    That being said, a for loop is maybe easier to understand. (This below gives the same output):

    Map<String, Pair<String, String>> map = new HashMap<>();
    for (Pair<String, String> pair : pairs) {
      map.put(pair.getKey(), pair);