1

If there any way we can call the method of an object inside a Hashset which in itself is a value in a Hashmap without making an iterator or a .forEach(lambda) or in any way going through each object sequentially?

Consider we have a Hashmap like this. Map<Boolean,Set<Place>> selectedMap

Consider Place extends JComponent and has a boolean value representing if the user had selected the object with the mouse, the map contains a set with all selected and all unselected objects.

If we for example want to call the remove-method of all selected objects, is there a way of doing that in the manner outlined above?

VICWICIV
  • 57
  • 6

1 Answers1

2

If you want to call a method for all the elements in a Set, you must iterate over the elements of that Set.

That said, instead of an explicit loop, in Java 8 you can use the forEach method:

selectedMap.get(true).forEach(Place::remove);

or

selectedMap.get(true).forEach(place -> place.remove(...));

in case the remove() method requires some arguments.

Eran
  • 387,369
  • 54
  • 702
  • 768