-
Notifications
You must be signed in to change notification settings - Fork 0
/
P20.java
45 lines (38 loc) · 1.07 KB
/
P20.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package lists;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.stream.Collectors;
import static java.util.stream.IntStream.rangeClosed;
/**
* Remove the n-th item from a list.
* <p>
* Example:
* ?- remove_at(X,[a,b,c,d],2,R).
* X = b
* R = [a,c,d]
*/
public final class P20 {
private P20() {
}
/**
* Remove the n-th element.
*
* @param list input list of items
* @param i index of item which should be removed
* @param <T> type of item
* @return list without element
*/
public static <T> Object[] removeAt(final List<T> list, final int i) {
Object[] objects = new Object[2];
if (Objects.requireNonNull(list).isEmpty()) {
throw new NoSuchElementException("Empty list is not supported");
}
objects[1] = list.get(i - 1);
objects[0] = rangeClosed(0, list.size() - 1)
.filter(j -> j != i - 1)
.mapToObj(list::get)
.collect(Collectors.toList());
return objects;
}
}