Clean code - best way to compact code in Java -


I have a code that looks like this:

 for  (int i = 0; i & lt; a; i ++) {List & lt; Integer & gt; List = element a. G (i); Some class RG = new classes (list, A, method A (I)); Int results = rg.generate (); Var + = methodA2 (i, result); } (Int i = 0; i & lt; b; i ++) {List & lt; Integer & gt; List = element B.get (i); Some class RG = new classes (list, B, method B (I)); Int results = rg.generate (); Var + = methodB2 (i, result); }   

How can I avoid this code recurrence? I can create a function that does that, but what is the different ways to do it?

with Java & lt; 8 You can create the interface (note that Java 8 already has a IntFunction interface):

  interface intefunction & lt; A & gt; {One apply}; } M (Element A, A, new interfunction and Lt; A & gt; () {Public A (Int.) {MethodA (i);}});   

and your method will look like this:

  Private zero I (collection   

(I left methodA2 for conciseness: you will need a second interface in which a is applied (int, int)

With Java 8, this cleaner becomes:

  m (element A, A, I -> method A (I)) ; // or m (element A, A, this :: method A);    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -