File tree Expand file tree Collapse file tree 5 files changed +183
-0
lines changed
Chapter09/BONUS_1_ArrayToStreamAndBack
src/main/java/modern/challenge Expand file tree Collapse file tree 5 files changed +183
-0
lines changed Original file line number Diff line number Diff line change 1+ # Map a stream
2+ Write a helper method that uses ** Stream#map()** to convert ** List< P >** in ** List< Q >** . Write a heper method that does the same thing for arrays.
Original file line number Diff line number Diff line change 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 >BONUS_1_ArrayToStreamAndBack</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 >BONUS_1_ArrayToStreamAndBack</name >
14+ </project >
Original file line number Diff line number Diff line change 1+ package modern .challenge ;
2+
3+ import java .util .Arrays ;
4+ import java .util .stream .IntStream ;
5+ import java .util .stream .Stream ;
6+
7+ public final class Convertors {
8+
9+ private Convertors () {
10+ throw new AssertionError ("Cannot be instantiated" );
11+ }
12+
13+ // Convert array of T to stream via Arrays#stream()
14+ public static <T > Stream <T > toStream1 (T [] arr ) {
15+
16+ if (arr == null ) {
17+ throw new IllegalArgumentException ("Inputs cannot be null" );
18+ }
19+
20+ return Arrays .stream (arr );
21+ }
22+
23+ // Convert array of T to stream via Stream#of()
24+ public static <T > Stream <T > toStream2 (T [] arr ) {
25+
26+ if (arr == null ) {
27+ throw new IllegalArgumentException ("Inputs cannot be null" );
28+ }
29+
30+ return Stream .of (arr );
31+ }
32+
33+ // Convert array of T to stream via List#stream()
34+ public static <T > Stream <T > toStream3 (T [] arr ) {
35+
36+ if (arr == null ) {
37+ throw new IllegalArgumentException ("Inputs cannot be null" );
38+ }
39+
40+ return Arrays .asList (arr ).stream ();
41+ }
42+
43+ // Convert array of primitves (int) to stream via Arrays#stream()
44+ public static IntStream toStream4 (int [] arr ) {
45+
46+ if (arr == null ) {
47+ throw new IllegalArgumentException ("Inputs cannot be null" );
48+ }
49+
50+ return Arrays .stream (arr );
51+ }
52+
53+ // Convert array of primitves (int) to stream via IntStream#of()
54+ public static IntStream toStream5 (int [] arr ) {
55+
56+ if (arr == null ) {
57+ throw new IllegalArgumentException ("Inputs cannot be null" );
58+ }
59+
60+ return IntStream .of (arr );
61+ }
62+ }
Original file line number Diff line number Diff line change 1+ package modern .challenge ;
2+
3+ public class Main {
4+
5+ public static void main (String [] args ) {
6+
7+ Melon [] melons = {
8+ new Melon ("Gac" , 1200 ),
9+ new Melon ("Hemi" , 2300 ),
10+ new Melon ("Apollo" , 3400 )
11+ };
12+
13+ int [] weights = {1200 , 2300 , 3400 };
14+
15+ System .out .println ("Array of melons to stream (toStream1()):" );
16+ Convertors .toStream1 (melons )
17+ .forEach (System .out ::println );
18+
19+ System .out .println ("\n \n Array of melons to stream (toStream2()):" );
20+ Convertors .toStream2 (melons )
21+ .forEach (System .out ::println );
22+
23+ System .out .println ("\n \n Array of melons to stream (toStream3()):" );
24+ Convertors .toStream3 (melons )
25+ .forEach (System .out ::println );
26+
27+ System .out .println ("\n \n Array of integers to stream (toStream4()):" );
28+ Convertors .toStream4 (weights )
29+ .forEach (System .out ::println );
30+
31+ System .out .println ("\n \n Array of integers to stream (toStream5()):" );
32+ Convertors .toStream5 (weights )
33+ .forEach (System .out ::println );
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package modern .challenge ;
2+
3+ import java .util .Objects ;
4+
5+ public class Melon {
6+
7+ private String type ;
8+ private int weight ;
9+
10+ public Melon (String type , int weight ) {
11+ this .type = type ;
12+ this .weight = weight ;
13+ }
14+
15+ public String getType () {
16+ return type ;
17+ }
18+
19+ public int getWeight () {
20+ return weight ;
21+ }
22+
23+ public void setType (String type ) {
24+ this .type = type ;
25+ }
26+
27+ public void setWeight (int weight ) {
28+ this .weight = weight ;
29+ }
30+
31+ @ Override
32+ public String toString () {
33+ return type + "(" + weight + "g)" ;
34+ }
35+
36+ @ Override
37+ public int hashCode () {
38+ int hash = 7 ;
39+ hash = 37 * hash + Objects .hashCode (this .type );
40+ hash = 37 * hash + this .weight ;
41+ return hash ;
42+ }
43+
44+ @ Override
45+ public boolean equals (Object obj ) {
46+
47+ if (this == obj ) {
48+ return true ;
49+ }
50+
51+ if (obj == null ) {
52+ return false ;
53+ }
54+
55+ if (getClass () != obj .getClass ()) {
56+ return false ;
57+ }
58+
59+ final Melon other = (Melon ) obj ;
60+ if (this .weight != other .weight ) {
61+ return false ;
62+ }
63+
64+ if (!Objects .equals (this .type , other .type )) {
65+ return false ;
66+ }
67+ return true ;
68+ }
69+
70+ }
You can’t perform that action at this time.
0 commit comments