Here is my code. I'd like to achieve the output without using for loop. Tried different ways in Java with help of lambda expression but it didn't work. Any help/suggestions would be very helpful. Thank you!
public class Hobbies {
private final HashMap<String, String[]> hobbies = new HashMap<String, String[]>();
public void add(String hobbyist, String... hobbies) {
this.hobbies.put(hobbyist, hobbies);
}
public List<String> findAllHobbyists(String hobby) {
List<String> hobbyyist = new ArrayList<String>();
for (Entry<String, String[]> entry : hobbies.entrySet()) {
String name = entry.getKey();
String[] list = entry.getValue();
for(String s: list) {
if(s.equalsIgnoreCase(hobby))
hobbyyist.add(name);
}
}
return hobbyyist;
}
public static void main(String[] args) {
Hobbies hobbies = new Hobbies();
hobbies.add("Steve", "Fashion", "Piano", "Reading");
hobbies.add("Patty", "Drama", "Magic", "Pets");
hobbies.add("Chad", "Puzzles", "Pets", "Yoga");
System.out.println(Arrays.toString(hobbies.findAllHobbyists("Yoga").toArray()));
}
}