Search code examples
javahashmapsublist

Map with HashMap inside sublist


How I can get sublist from?

Map<String, HashMap<String, String>>

I have tried:

Collections.list(Collections.enumeration(myMap.values())).subList(from,too)

UPD: Method returns List<HashMap<String, String>> only. But I need List<String, HashMap<String,String>>


Solution

  • You probably want to use Map.entrySet(), not Map.values(). It will provide a set of key-value pairs, which is what you want.

    If you are expecting the entries to be in the order you inserted them with, use LinkedHashMap instead of HashMap.

    Also, unimportantly, consider getting rid of the enumeration, and just use new ArrayList(myMap.entrySet()).