Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java trimming down code

Tags:

java

I have a list of checkboxes that I would like to disable. Rather than typing

c1.setEnabled(false);
c2.setEnabled(false);
c3.setEnabled(false);
c4.setEnabled(false);
c5.setEnabled(false);

How could I trim this code down by putting them in some sort of group? I have the same issue throughout a lot of my code but with different components. Thanks

like image 965
Paji.R Avatar asked Dec 18 '22 17:12

Paji.R


1 Answers

In Java 8+, you could use a lambda like

Stream.of(c1, c2, c3, c4, c5).forEach(x -> x.setEnabled(false));
like image 72
Elliott Frisch Avatar answered Jan 06 '23 02:01

Elliott Frisch