8000 Stream.,map() helpers · allwaysoft/Java-Coding-Problems@671c6ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 671c6ff

Browse files
committed
Stream.,map() helpers
1 parent 4db90cd commit 671c6ff

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Map a stream
2+
Write several examples of mapping a stream via **map()** and **flatMap()**.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>coding.challenge</groupId>
5+
<artifactId>P182_MapStreamHelpers</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>13</maven.compiler.source>
11+
<maven.compiler.target>13</maven.compiler.target>
12+
</properties>
13+
<name>P182_MapStreamHelpers</name>
14+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package modern.challenge;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Function;
6+
import java.util.function.IntFunction;
7+
import java.util.stream.Collectors;
8+
9+
public final class Convertors {
10+
11+
private Convertors() {
12+
throw new AssertionError("Cannot be instantiated");
13+
}
14+
15+
// converter for lists
16+
public static <P, Q> List<Q> list(List<P> source, Function<P, Q> f) {
17+
18+
if(source == null || f == null) {
19+
throw new IllegalArgumentException("Inputs cannot be null");
20+
}
21+
22+
return source.stream()
23+
.map(f)
24+
.collect(Collectors.toList());
25+
}
26+
27+
// converter for arrays
28+
public static <P, Q> Q[] array(P[] source, Function<P, Q> f, IntFunction<Q[]> generator) {
29+
30+
if(source == null || f == null || generator == null) {
31+
throw new IllegalArgumentException("Inputs cannot be null");
32+
}
33+
34+
return Arrays.stream(source)
35+
.map(f)
36+
.toArray(generator);
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package modern.challenge;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
10+
System.out.println("Convert list of String to list of ints:");
11+
List<String> strList = Arrays.asList("11", "22", "33");
12+
List<Integer> intList = Convertors.list(strList, Integer::parseInt);
13+
System.out.println("strList: " + strList);
14+
System.out.println("intList: " + intList);
15+
16+
System.out.println("\n\nConvert array of String to array of doubles:");
17+
String[] strArr = {"11", "22", "34"};
18+
Double[] doubleArr = Convertors.array(strArr, Double::parseDouble, Double[]::new);
19+
System.out.println("strArr: " + Arrays.toString(strArr));
20+
System.out.println("doubleArr: " + Arrays.toString(doubleArr));
21+
}
22+
}

0 commit comments

Comments
 (0)
0