Home > other >  How to convert a List of Map(string,string) to a List of strings with only a specific key (groovy)
How to convert a List of Map(string,string) to a List of strings with only a specific key (groovy)

Time:12-24

What I currently have is list of maps

[{"title":"Witcher","desc":"456"},{"title":"Harry Potter","desc":123},{"title":"ozark","desc":"879"}]

I want to end up with is a list of titles.

["Witcher","Harry Potter","ozark"]

My current code somewhat like this

ListMap.stream().filter({ map -> map.containsKey("Title") }).collect(Collectors.toList())

CodePudding user response:

Hope this will help:

  def map = [["title":"Witcher","desc":"456"],["title":"Harry Potter","desc":123],["title":"ozark","desc":"879"]]
  
  def names = map.collect{entry -> entry.title}
  
  println(names)
  • Related