-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
323 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
concept/core/src/com/core/stream/a_little_example/Sample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.core.stream.a_little_example; | ||
|
||
import java.util.*; | ||
|
||
public class Sample { | ||
public static void main(String[] args) { | ||
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
|
||
//Print the double of event numbers | ||
|
||
numbers.stream() | ||
.filter(e -> e % 2 == 0) | ||
.map(e -> e * 2) | ||
.forEach(System.out::println); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.core.stream.b_gone_wrong; | ||
|
||
import java.util.*; | ||
|
||
public class Sample { | ||
public static void main(String[] args) { | ||
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
|
||
//Create a list of double of even numbers | ||
|
||
List<Integer> doubleOfEven = new ArrayList<>(); | ||
|
||
numbers.stream() | ||
.filter(e -> e % 2 == 0) | ||
.map(e -> e * 2) | ||
.forEach(e -> doubleOfEven.add(e)); //Don't do this. Shared mutabilty is evil. | ||
//This code can't ever be paralleized (well it will misbehave if we do). | ||
|
||
System.out.println(doubleOfEven); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
concept/core/src/com/core/stream/c_the_right_way/Sample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.core.stream.c_the_right_way; | ||
|
||
import java.util.*; | ||
import static java.util.stream.Collectors.*; | ||
|
||
public class Sample { | ||
public static void main(String[] args) { | ||
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
|
||
//Create a list of double of even numbers | ||
|
||
//The right way, delegate, be declarative, leave it to the APIs | ||
|
||
List<Integer> doubleOfEven = numbers.stream() | ||
.filter(e -> e % 2 == 0) | ||
.map(e -> e * 2) | ||
.collect(toList()); | ||
|
||
System.out.println(doubleOfEven); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.core.stream.d_toset; | ||
|
||
import java.util.*; | ||
import static java.util.stream.Collectors.*; | ||
|
||
public class Sample { | ||
public static void main(String[] args) { | ||
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
|
||
//Create a list of double of even numbers | ||
|
||
//The right way, delegate, be declarative, leave it to the APIs | ||
|
||
Set<Integer> doubleOfEven = numbers.stream() | ||
.filter(e -> e % 2 == 0) | ||
.map(e -> e * 2) | ||
.collect(toSet()); | ||
|
||
System.out.println(doubleOfEven); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
concept/core/src/com/core/stream/e_to_unmodifiable/Sample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.core.stream.e_to_unmodifiable; | ||
|
||
import java.util.*; | ||
import static java.util.stream.Collectors.*; | ||
|
||
public class Sample { | ||
public static void main(String[] args) { | ||
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
|
||
//Create a list of double of even numbers | ||
|
||
List<Integer> doubleOfEven = numbers.stream() | ||
.filter(e -> e % 2 == 0) | ||
.map(e -> e * 2) | ||
.collect(toUnmodifiableList()); | ||
|
||
System.out.println(doubleOfEven); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.core.stream.f_partition; | ||
|
||
import java.util.*; | ||
import static java.util.stream.Collectors.*; | ||
|
||
public class Sample { | ||
public static void main(String[] args) { | ||
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
|
||
System.out.println(numbers.stream() | ||
.collect(partitioningBy(e -> e % 2 == 0))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.core.stream.g_joining; | ||
|
||
public class Person { | ||
private final String name; | ||
private final int age; | ||
|
||
public Person(String theName, int theAge) { | ||
name = theName; | ||
age = theAge; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public String toString() { | ||
return String.format("%s -- %d", name, age); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.core.stream.g_joining; | ||
|
||
import java.util.*; | ||
import static java.util.stream.Collectors.*; | ||
|
||
public class Sample { | ||
public static List<Person> createPeople() { | ||
return List.of( | ||
new Person("Sara", 20), | ||
new Person("Sara", 22), | ||
new Person("Bob", 20), | ||
new Person("Paula", 32), | ||
new Person("Paul", 32), | ||
new Person("Jack", 2), | ||
new Person("Jack", 72), | ||
new Person("Jill", 12) | ||
); | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<Person> people = createPeople(); | ||
|
||
System.out.println( | ||
people.stream() | ||
.map(Person::getName) | ||
.map(String::toUpperCase) | ||
.collect(joining(", "))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.core.stream.h_grouping; | ||
|
||
public class Person { | ||
private final String name; | ||
private final int age; | ||
|
||
public Person(String theName, int theAge) { | ||
name = theName; | ||
age = theAge; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public String toString() { | ||
return String.format("%s -- %d", name, age); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.core.stream.h_grouping; | ||
|
||
import java.util.*; | ||
import static java.util.stream.Collectors.*; | ||
|
||
public class Sample { | ||
public static List<Person> createPeople() { | ||
return List.of( | ||
new Person("Sara", 20), | ||
new Person("Sara", 22), | ||
new Person("Bob", 20), | ||
new Person("Paula", 32), | ||
new Person("Paul", 32), | ||
new Person("Jack", 2), | ||
new Person("Jack", 72), | ||
new Person("Jill", 12) | ||
); | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<Person> people = createPeople(); | ||
|
||
Map<String, List<Person>> byName = | ||
people.stream() | ||
.collect(groupingBy(person -> person.getName())); | ||
|
||
System.out.println(byName); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
concept/core/src/com/core/stream/i_and_mapping/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.core.stream.i_and_mapping; | ||
|
||
public class Person { | ||
private final String name; | ||
private final int age; | ||
|
||
public Person(String theName, int theAge) { | ||
name = theName; | ||
age = theAge; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public String toString() { | ||
return String.format("%s -- %d", name, age); | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
concept/core/src/com/core/stream/i_and_mapping/Sample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.core.stream.i_and_mapping; | ||
|
||
import java.util.*; | ||
import static java.util.stream.Collectors.*; | ||
|
||
public class Sample { | ||
public static List<Person> createPeople() { | ||
return List.of( | ||
new Person("Sara", 20), | ||
new Person("Sara", 22), | ||
new Person("Bob", 20), | ||
new Person("Paula", 32), | ||
new Person("Paul", 32), | ||
new Person("Jack", 2), | ||
new Person("Jack", 72), | ||
new Person("Jill", 12) | ||
); | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<Person> people = createPeople(); | ||
|
||
Map<String, List<Integer>> byName = | ||
people.stream() | ||
.collect(groupingBy(Person::getName, mapping(Person::getAge, toList()))); | ||
|
||
System.out.println(byName); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
concept/core/src/com/core/stream/j_collectingandthen/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.core.stream.j_collectingandthen; | ||
|
||
public class Person { | ||
private final String name; | ||
private final int age; | ||
|
||
public Person(String theName, int theAge) { | ||
name = theName; | ||
age = theAge; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public String toString() { | ||
return String.format("%s -- %d", name, age); | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
concept/core/src/com/core/stream/j_collectingandthen/Sample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.core.stream.j_collectingandthen; | ||
|
||
import java.util.*; | ||
import static java.util.stream.Collectors.*; | ||
|
||
public class Sample { | ||
public static List<Person> createPeople() { | ||
return List.of( | ||
new Person("Sara", 20), | ||
new Person("Sara", 22), | ||
new Person("Bob", 20), | ||
new Person("Paula", 32), | ||
new Person("Paul", 32), | ||
new Person("Jack", 2), | ||
new Person("Jack", 72), | ||
new Person("Jill", 12) | ||
); | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<Person> people = createPeople(); | ||
|
||
Map<String, Integer> byName = | ||
people.stream() | ||
.collect(groupingBy(Person::getName, collectingAndThen(counting(), Long::intValue))); | ||
|
||
System.out.println(byName); | ||
} | ||
} |