Returns the keys from the map as a list. - Java java.util

Java examples for java.util:List Element

Description

Returns the keys from the map as a list.

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class Main {
    /**/*  w ww .  j  a  v a 2  s.c  o  m*/
     * Returns the keys from the map as a list. Most useful if the map's keys
     * are ordered, for instance if it is a <code>LinkedHashMap</code>.
     * 
     * @param map
     * @return
     */
    public static <T, U> List<T> getKeyList(LinkedHashMap<T, U> linkedMap) {
        List<T> keyList = new ArrayList<T>();
        for (T key : linkedMap.keySet()) {
            keyList.add(key);
        }
        return keyList;
    }
}

Related Tutorials