Reputation: 11
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
Reputation: 72
Try this
ExcelData.forEach(item ->
item.forEach((key, value) -> System.out.println(key + " -> " + value))
);
Upvotes: 0
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