import java.util.*; import java.util.stream.*; public class Kata { public static int[] Remove(int[] integerList, int[] valuesList) { if (integerList == null) return new int[]{}; if (valuesList == null) return integerList; List<Integer> list = toList(integerList); list.removeAll(toList(valuesList)); return list.stream().mapToInt(i->i).toArray(); } private static List<Integer> toList(int[] arr) { return IntStream.of(arr).boxed().collect(Collectors.toList()); } }
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class Kata{public static int[] Remove(int[] integerList,int[] valuesList){if (integerList==null||integerList.length<1) return new int[0];if (valuesList==null||valuesList.length<1) return integerList;List<Integer> list= Arrays.stream(valuesList).boxed().collect(Collectors.toList());return Arrays.stream(integerList).filter(i->!list.contains(i)).toArray();}- import java.util.*;
- import java.util.stream.*;
- public class Kata {
- public static int[] Remove(int[] integerList, int[] valuesList) {
- if (integerList == null) return new int[]{};
- if (valuesList == null) return integerList;
- List<Integer> list = toList(integerList);
- list.removeAll(toList(valuesList));
- return list.stream().mapToInt(i->i).toArray();
- }
- private static List<Integer> toList(int[] arr) {
- return IntStream.of(arr).boxed().collect(Collectors.toList());
- }
- }
Another variant using IntStream. Probably much slower than using a for-loop.
import java.util.stream.IntStream; public class Kata { public static int findIndex(int[] array, int target) { return IntStream.range(0, array.length).filter(i -> array[i] == target).findFirst().orElse(-1); } }
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;- import java.util.stream.IntStream;
- public class Kata {
public static int findIndex (int[] array, int target) {return Arrays.stream(array).boxed().collect(Collectors.toList()).indexOf(target);- public static int findIndex(int[] array, int target) {
- return IntStream.range(0, array.length).filter(i -> array[i] == target).findFirst().orElse(-1);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class SolutionTest { int[] array = {1, 2, 5, 7,10 }; @Test void test_01() { assertEquals(0, Kata.findIndex(array, 1)); } @Test void test_02() { assertEquals(1, Kata.findIndex(array, 2)); } @Test void test_05() { assertEquals(2, Kata.findIndex(array, 5)); } @Test void test_07() { assertEquals(3, Kata.findIndex(array, 7)); } @Test void test_10() { assertEquals(4, Kata.findIndex(array, 10)); } @Test void test_target_not_found() { var i = Kata.findIndex(array, -1); assertTrue( i<0 || i>= array.length ); } }
import static org.junit.jupiter.api.Assertions.assertEquals;- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.Test;
- class SolutionTest
- {
- int[] array = {1, 2, 5, 7,10 };
- @Test void test_01() { assertEquals(0, Kata.findIndex(array, 1)); }
- @Test void test_02() { assertEquals(1, Kata.findIndex(array, 2)); }
- @Test void test_05() { assertEquals(2, Kata.findIndex(array, 5)); }
- @Test void test_07() { assertEquals(3, Kata.findIndex(array, 7)); }
- @Test void test_10() { assertEquals(4, Kata.findIndex(array, 10)); }
- @Test void test_target_not_found() { var i = Kata.findIndex(array, -1); assertTrue( i<0 || i>= array.length ); }
- }