Home > front end >  Getting Keys from a List of Map of Strings
Getting Keys from a List of Map of Strings

Time:07-09

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

CodePudding user response:

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());

CodePudding user response:

Try this

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