Skip to content

Commit cff91ea

Browse files
committed
Initial import
0 parents  commit cff91ea

File tree

6 files changed

+348
-0
lines changed

6 files changed

+348
-0
lines changed

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# For STS/Eclipse
2+
.classpath
3+
.project
4+
.settings/
5+
.springBeans
6+
Servers
7+
bin
8+
9+
# IntelliJ IDEA
10+
.idea/
11+
*.iml
12+
*.iws
13+
14+
# Maven
15+
target/
16+
17+
# For Mac OS X
18+
.DS_Store
19+
20+
# Logging
21+
*.log

pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.bsnyder.java18.examples</groupId>
7+
<artifactId>java18-examples</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>java18-examples</name>
12+
<url>http://maven.apache.org</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven-compiler-plugin-version>3.2</maven-compiler-plugin-version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.12</version>
24+
<scope>test</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<pluginManagement>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<version>${maven-compiler-plugin-version}</version>
35+
<configuration>
36+
<source>1.8</source>
37+
<target>1.8</target>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
</pluginManagement>
42+
</build>
43+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.bsnyder.java18.examples;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Consumer;
6+
7+
interface Executable {
8+
int execute(int a);
9+
}
10+
11+
class Runner {
12+
public int run(Executable e) {
13+
System.out.println("Executing code block...");
14+
int x = e.execute(999);
15+
System.out.println(x);
16+
return x;
17+
}
18+
}
19+
20+
public class Java8LambdasDemo
21+
{
22+
public static void main( String[] args ) {
23+
// List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9);
24+
//
25+
// numbers.forEach(new Consumer<Integer>() {
26+
// @Override
27+
// public void accept(Integer integer) {
28+
// System.out.println(integer);
29+
// }
30+
// });
31+
32+
Runner r = new Runner();
33+
r.run(new Executable() {
34+
@Override
35+
public int execute(int a) {
36+
System.out.println("Simple Executable");
37+
return a+2;
38+
}
39+
});
40+
41+
System.out.println("==========================");
42+
43+
/*
44+
r.run(() -> {
45+
System.out.println("Testing a lambda");
46+
System.out.println();
47+
return 3;
48+
});
49+
*/
50+
51+
r.run(a -> {
52+
System.out.println("Adding a+3");
53+
return a+3;
54+
});
55+
}
56+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.bsnyder.java18.examples;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
class Person {
8+
String name;
9+
String country;
10+
int age;
11+
12+
public Person(String name, String country, int age) {
13+
this.name = name;
14+
this.country = country;
15+
this.age = age;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public String getCountry() {
23+
return country;
24+
}
25+
26+
public int getAge() {
27+
return age;
28+
}
29+
30+
public String toString() {
31+
return "{Person => name: " + name + " country: " + country + " age: " + age + "}";
32+
}
33+
}
34+
35+
public class Java8LambdasDemo2 {
36+
37+
public static void main(String[] args) {
38+
List<Person> people = Arrays.asList(
39+
new Person("Bailey", "USA", 12),
40+
new Person("Jade", "USA", 16),
41+
new Person("Janene", "Mexico", 43),
42+
new Person("Bruce", "Canada", 43),
43+
new Person("Diane", "Germany", 64),
44+
new Person("Jim", "Germany", 64),
45+
new Person("Susan", "USA", 62),
46+
new Person("John", "Canada", 64));
47+
48+
System.out.println("==== People from the US :: filter ====");
49+
people.stream()
50+
.filter(person -> person.getCountry().equals("USA"))
51+
.map((Person person) -> "name: " + person.getName() + " country: " + person.getCountry())
52+
.forEach(person -> System.out.println(person));
53+
54+
System.out.println("\n==== People not from the US :: filter ====");
55+
people.stream()
56+
.filter(person -> !person.getCountry().equals("USA"))
57+
.map((Person person) -> "name: " + person.getName() + " country: " + person.getCountry())
58+
.forEach(person -> System.out.println(person));
59+
60+
System.out.println("\n==== People from the US :: collect w/ partitioningBy ====");
61+
System.out.println(people.stream()
62+
.collect(Collectors.partitioningBy((Person p) -> p.getCountry().equals("USA"))));
63+
64+
System.out.println("\n==== People from the US :: collect w/ groupingBy ====");
65+
System.out.println(people.stream()
66+
.collect(Collectors.groupingBy((Person p) -> p.getCountry().equals("USA"))));
67+
68+
System.out.println("\n==== People from the US :: collect w/ groupingBy and counting ====");
69+
System.out.println(people.stream()
70+
.collect(Collectors.groupingBy((Person p) -> p.getCountry().equals("USA"), Collectors.counting())));
71+
72+
System.out.println("\n==== People from the US :: collect w/ groupingBy and counting ====");
73+
System.out.println(people.stream()
74+
.collect(Collectors.groupingBy((Person p) -> p.getCountry().equals("USA"), Collectors.counting())));
75+
76+
System.out.println("\n==== People in each country :: collect w/ partitioningBy and counting ====");
77+
System.out.println(people.stream()
78+
.collect(Collectors.groupingBy((Person p) -> p.getCountry())));
79+
System.out.println(people.stream()
80+
.collect(Collectors.groupingBy((Person p) -> p.getCountry(), Collectors.counting())));
81+
82+
System.out.println("\n==== People in USA and non-USA :: collect w/ partitioningBy and mapping ====");
83+
System.out.println(people.stream()
84+
.collect(Collectors.partitioningBy((Person p) -> p.getCountry().equals("USA"),
85+
Collectors.mapping(p -> p.getName().toUpperCase(),
86+
Collectors.toList()))));
87+
88+
System.out.println("\n==== People in USA and non-USA :: collect w/ groupingBy and mapping ====");
89+
System.out.println(people.stream()
90+
.collect(Collectors.groupingBy((Person p) -> p.getCountry(),
91+
Collectors.mapping(p -> p.getName().toUpperCase(),
92+
Collectors.toList()))));
93+
94+
}
95+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.bsnyder.java18.examples;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.function.Predicate;
7+
8+
class MyPredicate<T> {
9+
10+
public List<T> testPredicate(Predicate<T> p, List<T> numbers) {
11+
List<T> answer = new ArrayList<>();
12+
13+
numbers.forEach( x -> {
14+
if (p.test(x)) {
15+
answer.add(x);
16+
}
17+
});
18+
19+
return answer;
20+
}
21+
22+
public List<T> testPredicateNegate(Predicate<T> p, List<T> l) {
23+
List<T> answer = new ArrayList<>();
24+
25+
/*
26+
27+
Q: Why does external iteration not result in proper negation?
28+
29+
l.forEach( x -> {
30+
if (p.negate().test(x));
31+
answer.add(x);
32+
});
33+
*/
34+
35+
for (T t : l) {
36+
if (p.negate().test(t)) answer.add(t);
37+
}
38+
39+
return answer;
40+
}
41+
42+
public List<T> testPredicateOr(Predicate<T> p1, Predicate<T> p2, List<T> l) {
43+
List<T> answer = new ArrayList<>();
44+
45+
for( T t : l) {
46+
if (p1.or(p2).test(t)) answer.add(t);
47+
}
48+
49+
return answer;
50+
}
51+
52+
public List<T> testPredicateAnd(Predicate<T> p1, Predicate<T> p2, List<T> l) {
53+
List<T> answer = new ArrayList<>();
54+
55+
for( T t : l) {
56+
if (p1.and(p2).test(t)) answer.add(t);
57+
}
58+
59+
return answer;
60+
}
61+
62+
}
63+
64+
/**
65+
* Created by bsnyder on 1/13/15.
66+
*/
67+
public class PredicateDemo {
68+
69+
public static void main(String[] args) {
70+
List<Integer> numbers = Arrays.asList(0,1,2,3,4,5,6,7,8,9,10,11,12,13);
71+
72+
MyPredicate<Integer> intPredicate = new MyPredicate<Integer>();
73+
System.out.println(" All numbers: " + intPredicate.testPredicate(x -> true, numbers));
74+
System.out.println("Even numbers: " + intPredicate.testPredicate(x -> (x & 1) == 0, numbers));
75+
System.out.println(" Odd numbers: " + intPredicate.testPredicate(x -> (x & 1) == 1, numbers));
76+
System.out.println();
77+
System.out.println(" Negate all numbers: " + intPredicate.testPredicateNegate(x -> true, numbers));
78+
System.out.println("Negate even numbers: " + intPredicate.testPredicateNegate(x -> (x & 1) == 0, numbers));
79+
System.out.println(" Negate odd numbers: " + intPredicate.testPredicateNegate(x -> (x & 1) == 1, numbers));
80+
81+
System.out.println("###################################################");
82+
83+
MyPredicate<String> stringPredicate = new MyPredicate<>();
84+
List<String> strings = Arrays.asList("one", "two", "three", "four", "five", "six", "seven", "eight");
85+
System.out.println("All strings: " + stringPredicate.testPredicate(x -> true, strings));
86+
System.out.println("Contains ee: " + stringPredicate.testPredicate(x -> x.contains("ee"), strings));
87+
System.out.println(" Contains t: " + stringPredicate.testPredicate(x -> x.contains("t"), strings));
88+
System.out.println();
89+
System.out.println("Negate all strings: " + stringPredicate.testPredicateNegate(x -> true, strings));
90+
System.out.println("Negate contains ee: " + stringPredicate.testPredicateNegate(x -> x.contains("ee"), strings));
91+
System.out.println(" Negate contains t: " + stringPredicate.testPredicateNegate(x -> x.contains("t"), strings));
92+
93+
System.out.println("###################################################");
94+
95+
MyPredicate<Object> objPredicate = new MyPredicate<>();
96+
List<Object> objects = Arrays.asList(new String("one"), new String("two"), new StringBuilder("three"));
97+
System.out.println(" All objects: " + objPredicate.testPredicate(x -> true, objects));
98+
System.out.println(" Type String: " + objPredicate.testPredicate(x -> x instanceof String, objects));
99+
System.out.println("Type StringBuilder: " + objPredicate.testPredicate(x -> x instanceof StringBuilder, objects));
100+
System.out.println();
101+
System.out.println(" Negate all objects: " + objPredicate.testPredicateNegate(x -> true, objects));
102+
System.out.println(" Negate type String: " + objPredicate.testPredicateNegate(x -> x instanceof String, objects));
103+
System.out.println("Negate type StringBuilder: " + objPredicate.testPredicateNegate(x -> x instanceof StringBuilder, objects));
104+
105+
System.out.println("###################################################");
106+
107+
System.out.println("Even or odd numbers: " +
108+
intPredicate.testPredicateOr(x -> (x & 1) == 0, x -> (x & 1) == 1, numbers));
109+
System.out.println("Even or odd numbers: " +
110+
intPredicate.testPredicateAnd(x -> (x & 1) == 0, x -> (x & 1) == 1, numbers));
111+
}
112+
}
113+
114+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.bsnyder.java18.examples;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
/**
8+
* Unit test for simple Java8LambdasDemo.
9+
*/
10+
public class AppTest
11+
{
12+
/**
13+
* Rigourous Test :-)
14+
*/
15+
@Test
16+
public void testApp() {
17+
assertTrue( true );
18+
}
19+
}

0 commit comments

Comments
 (0)