Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
Code
Diff
  • import java.util.stream.IntStream;
    
    public class Area {
    
      public static long[] workOutArea(final long[] values){
        return values.length % 2 != 0? null: IntStream.range(0, values.length).filter(x -> x % 2 == 0).mapToLong(x -> values[x] * values[x+1]).toArray();
      }
    
    }
  • 1+import java.util.stream.IntStream;
    2+
    11 public class Area {
    22
    33 public static long[] workOutArea(final long[] values){
    4- if (values.length % 2 != 0) {
    5- return null; // This is ugly!
    6- } else {
    7- final long[] areas = new long[values.length / 2];
    8- for (int i = 0; i < values.length; i += 2) {
    9- areas[i / 2] = values[i] * values[i + 1];
    10- }
    11- return areas;
    12- }
    6+ return values.length % 2 != 0? null: IntStream.range(0, values.length).filter(x -> x % 2 == 0).mapToLong(x -> values[x] * values[x+1]).toArray();
    1313 }
    1414
    1515 }