Ad
Code
Diff
  • import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class Kata {
        public static int findIndex (int[] array, int target) {
          return Arrays.stream(array).boxed().collect(Collectors.toList()).indexOf(target);
        }
    }
    • import org.apache.commons.lang3.ArrayUtils;
    • import java.util.Arrays;
    • import java.util.List;
    • import java.util.stream.Collectors;
    • public class Kata {
    • public static int findIndex (int[] array, int target) {
    • return ArrayUtils.indexOf(array, target);
    • return Arrays.stream(array).boxed().collect(Collectors.toList()).indexOf(target);
    • }
    • }
Arrays
Data Types

Given a 2D integer matrix, format it to print the matrix with each character occupying a minimum width.

import java.lang.StringBuilder;
import java.util.Arrays;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;

public class Kata {
  public static String stringify2dArray(int[][] array, int width) {
    String pattern = "%" + width + "s";
    return Arrays.stream(array)
        .map(formatRow(pattern))
        .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
        .toString();
  }

  private static Function<? super int[], ? extends String> formatRow(String pattern) {
    return row -> Arrays.stream(row)
        .mapToObj(formatCell(pattern))
        .collect(Collectors.joining(" ")) + "\n";
  }

  private static IntFunction<? extends String> formatCell(String pattern) {
    return cell -> String.format(pattern, cell);
  }
}