Java Map to List mapToList(final TreeMap> map)

Here you can find the source of mapToList(final TreeMap> map)

Description

map To List

License

MIT License

Parameter

Parameter Description
map a parameter

Return

List>

Declaration

static <T> List<List<T>> mapToList(final TreeMap<Integer, List<T>> map) 

Method Source Code

//package com.java2s;
/*/*from   w ww  .  j av a  2  s  . c o m*/
 * libChEBIj (c) University of Manchester 2015
 *
 * libChEBIj is licensed under the MIT License.
 * 
 * To view a copy of this license, visit <http://opensource.org/licenses/MIT/>.
 */

import java.util.*;

public class Main {
    /**
     * 
     * @param map
     * @return List<Collection<T>>
     */
    static <T> List<List<T>> mapToList(final TreeMap<Integer, List<T>> map) {
        final int lastIndex = map.lastKey().intValue();
        final int capacity = lastIndex + 1;
        final List<List<T>> list = new ArrayList<>(capacity);

        for (int i = 0; i < capacity; i++) {
            list.add(new ArrayList<T>());
        }

        for (Map.Entry<Integer, List<T>> entry : map.entrySet()) {
            list.set(entry.getKey().intValue(), entry.getValue());
        }

        return list;
    }

    /**
     * 
     * @param id
     * @param map
     * @param object
     */
    static <T> void add(final Integer id, final TreeMap<Integer, List<T>> map, T object) {
        List<T> list = map.get(id);

        if (list == null) {
            list = new ArrayList<>();
            map.put(id, list);
        }

        list.add(object);
    }
}

Related

  1. mapToList(Map map)
  2. mapToList(Map map)