nithinc
nithinc

Reputation: 11

Getting Keys from a List of Map of Strings

How to get the keys from a list of Map of Strings? I have a list of map of strings

      List<Map<String, String>> ExcelData = new ArrayList<>();
      Map<String,String> excelMap = new HashMap<>(); 
      excelMap.put("Flower","lily");
      excelMap.put("Fruit","banana");
      ExcelData.add(excelMap);

Is there a way to get the keys of this list map in an string array? Thanks

Upvotes: 1

Views: 1479

Answers (2)

Josue Muleshi
Josue Muleshi

Reputation: 72

Try this

ExcelData.forEach(item -> 
        item.forEach((key, value) -> System.out.println(key + " -> " + value))
);

Upvotes: 0

Eritrean
Eritrean

Reputation: 16498

In addition to @Rogue's comment you can use method reference to make it readable:

String[] keys     = ExcelData.stream().map(Map::keySet).flatMap(Set::stream).toArray(String[]::new);
List<String> keyz = ExcelData.stream().map(Map::keySet).flatMap(Set::stream).collect(Collectors.toList());

Upvotes: 1

Related Questions