Skip to content

Commit c19aaa9

Browse files
committed
Added list-ops exercise to the Java track
1 parent a6b7fe2 commit c19aaa9

8 files changed

Lines changed: 562 additions & 1 deletion

File tree

‎config.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"all-your-base",
5454
"custom-set",
5555
"wordy",
56-
"palindrome-products"
56+
"palindrome-products",
57+
"list-ops"
5758
],
5859
"exercises": [
5960
{
@@ -305,6 +306,11 @@
305306
"slug": "palindrome-products",
306307
"difficulty": 1,
307308
"topics": []
309+
},
310+
{
311+
"slug": "list-ops",
312+
"difficulty": 1,
313+
"topics": []
308314
}
309315
],
310316
"deprecated": [
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<gradle-project-properties>
3+
<!--DO NOT EDIT THIS FILE! - Used by the Gradle plugin of NetBeans.-->
4+
<auxiliary>
5+
<editor-bookmarks lastBookmarkId="0" xmlns="http://www.netbeans.org/ns/editor-bookmarks/2"/>
6+
</auxiliary>
7+
</gradle-project-properties>

‎exercises/list-ops/build.gradle‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.Collection;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.function.BiFunction;
7+
import java.util.function.BinaryOperator;
8+
import java.util.function.Predicate;
9+
import java.util.function.UnaryOperator;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
12+
13+
public class ListOps {
14+
15+
private ListOps() {
16+
}
17+
18+
public static <T> int length(final List<T> list) {
19+
return list.size();
20+
}
21+
22+
public static <T> List<T> reverse(final List<T> list) {
23+
List<T> result = new ArrayList(list);
24+
Collections.reverse(result);
25+
return result;
26+
}
27+
28+
public static <T> List<T> map(final List<T> list,
29+
UnaryOperator<T> mapper) {
30+
return list.stream().map(mapper).collect(Collectors.toList());
31+
}
32+
33+
public static <T> List<T> filter(final List<T> list,
34+
Predicate<T> predicate) {
35+
return list.stream().filter(predicate).collect(Collectors.toList());
36+
}
37+
38+
public static <U, T> U reduce(final List<T> list,
39+
U identity,
40+
BiFunction<U, T, U> accumulator,
41+
BinaryOperator<U> combiner) {
42+
return list.stream().reduce(identity, accumulator, combiner);
43+
}
44+
45+
public static <T> boolean append(final List<T> listTo,
46+
List<T> listFrom) {
47+
return listTo.addAll(listFrom);
48+
}
49+
50+
public static <T> List<T> concat(final List<T>... lists) {
51+
return Stream.of(lists)
52+
.flatMap(Collection::stream)
53+
.collect(Collectors.toList());
54+
}
55+
56+
}

‎exercises/list-ops/src/main/java/.keep‎

Whitespace-only changes.

0 commit comments

Comments
 (0)