// "Replace with collect" "true" import java.util.ArrayList; import java.util.List; import java.util.Map; public class Main { List test(Map> map, int limit) { List list = new ArrayList<>(); for (Map.Entry> entry : map.entrySet()) { if(entry.getValue() != null) { for(String str : entry.getValue()) { if (!list.contains(str)) { list.add(str); } if (list.size() >= limit) return list; } } } return list; } }