Skip to content

Commit

Permalink
streams
Browse files Browse the repository at this point in the history
  • Loading branch information
sks40gb committed Mar 14, 2021
1 parent 0d78c4e commit 84aabca
Show file tree
Hide file tree
Showing 15 changed files with 323 additions and 50 deletions.
50 changes: 0 additions & 50 deletions concept/core/src/com/core/overriding/ExceptionApp.java

This file was deleted.

16 changes: 16 additions & 0 deletions concept/core/src/com/core/stream/a_little_example/Sample.java
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);
}
}
21 changes: 21 additions & 0 deletions concept/core/src/com/core/stream/b_gone_wrong/Sample.java
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 concept/core/src/com/core/stream/c_the_right_way/Sample.java
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);
}
}
21 changes: 21 additions & 0 deletions concept/core/src/com/core/stream/d_toset/Sample.java
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 concept/core/src/com/core/stream/e_to_unmodifiable/Sample.java
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);
}
}
13 changes: 13 additions & 0 deletions concept/core/src/com/core/stream/f_partition/Sample.java
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)));
}
}
24 changes: 24 additions & 0 deletions concept/core/src/com/core/stream/g_joining/Person.java
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);
}
}

29 changes: 29 additions & 0 deletions concept/core/src/com/core/stream/g_joining/Sample.java
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(", ")));
}
}
24 changes: 24 additions & 0 deletions concept/core/src/com/core/stream/h_grouping/Person.java
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);
}
}

29 changes: 29 additions & 0 deletions concept/core/src/com/core/stream/h_grouping/Sample.java
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 concept/core/src/com/core/stream/i_and_mapping/Person.java
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 concept/core/src/com/core/stream/i_and_mapping/Sample.java
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 concept/core/src/com/core/stream/j_collectingandthen/Person.java
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 concept/core/src/com/core/stream/j_collectingandthen/Sample.java
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);
}
}

0 comments on commit 84aabca

Please sign in to comment.