Skip to content

Commit d8ebf49

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents 0d45b71 + 6563595 commit d8ebf49

208 files changed

Lines changed: 4563 additions & 599 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎core-java-8/src/main/java/com/baeldung/AdderImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
public class AdderImpl implements Adder {
77

88
@Override
9-
public String addWithFunction(Function<String, String> f) {
9+
public String addWithFunction(final Function<String, String> f) {
1010
return f.apply("Something ");
1111
}
1212

1313
@Override
14-
public void addWithConsumer(Consumer<Integer> f) {
14+
public void addWithConsumer(final Consumer<Integer> f) {
1515
}
1616

1717
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.baeldung.doublecolon;
2+
3+
public class Computer {
4+
5+
private Integer age;
6+
private String color;
7+
private Integer healty;
8+
9+
public Computer(final int age, final String color) {
10+
this.age = age;
11+
this.color = color;
12+
}
13+
14+
public Computer(final Integer age, final String color, final Integer healty) {
15+
this.age = age;
16+
this.color = color;
17+
this.healty = healty;
18+
}
19+
20+
public Computer() {
21+
}
22+
23+
public Integer getAge() {
24+
return age;
25+
}
26+
27+
public void setAge(final Integer age) {
28+
this.age = age;
29+
}
30+
31+
public String getColor() {
32+
return color;
33+
}
34+
35+
public void setColor(final String color) {
36+
this.color = color;
37+
}
38+
39+
public Integer getHealty() {
40+
return healty;
41+
}
42+
43+
public void setHealty(final Integer healty) {
44+
this.healty = healty;
45+
}
46+
47+
public void turnOnPc() {
48+
System.out.println("Computer turned on");
49+
}
50+
51+
public void turnOffPc() {
52+
System.out.println("Computer turned off");
53+
}
54+
55+
public Double calculateValue(Double initialValue) {
56+
return initialValue / 1.50;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}';
62+
}
63+
64+
@Override
65+
public boolean equals(final Object o) {
66+
if (this == o) {
67+
return true;
68+
}
69+
if (o == null || getClass() != o.getClass()) {
70+
return false;
71+
}
72+
73+
final Computer computer = (Computer) o;
74+
75+
if (age != null ? !age.equals(computer.age) : computer.age != null) {
76+
return false;
77+
}
78+
return color != null ? color.equals(computer.color) : computer.color == null;
79+
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
int result = age != null ? age.hashCode() : 0;
85+
result = 31 * result + (color != null ? color.hashCode() : 0);
86+
return result;
87+
}
88+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.doublecolon;
2+
3+
import com.baeldung.doublecolon.function.ComputerPredicate;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class ComputerUtils {
9+
10+
public static final ComputerPredicate after2010Predicate = (c) -> (c.getAge() > 2010);
11+
public static final ComputerPredicate blackPredicate = (c) -> "black".equals(c.getColor());
12+
13+
public static List<Computer> filter(final List<Computer> inventory, final ComputerPredicate p) {
14+
15+
final List<Computer> result = new ArrayList<>();
16+
inventory.stream().filter(p::filter).forEach(result::add);
17+
18+
return result;
19+
}
20+
21+
public static void repair(final Computer computer) {
22+
if (computer.getHealty() < 50) {
23+
computer.setHealty(100);
24+
}
25+
}
26+
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.doublecolon;
2+
3+
import java.util.function.Function;
4+
5+
public class MacbookPro extends Computer {
6+
7+
public MacbookPro(int age, String color) {
8+
super(age, color);
9+
}
10+
11+
public MacbookPro(Integer age, String color, Integer healty) {
12+
super(age, color, healty);
13+
}
14+
15+
@Override
16+
public void turnOnPc() {
17+
System.out.println("MacbookPro turned on");
18+
}
19+
20+
@Override
21+
public void turnOffPc() {
22+
System.out.println("MacbookPro turned off");
23+
}
24+
25+
@Override
26+
public Double calculateValue(Double initialValue) {
27+
28+
Function<Double, Double> function = super::calculateValue;
29+
final Double pcValue = function.apply(initialValue);
30+
System.out.println("First value is:" + pcValue);
31+
return pcValue + (initialValue / 10);
32+
33+
}
34+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.doublecolon.function;
2+
3+
import com.baeldung.doublecolon.Computer;
4+
5+
@FunctionalInterface
6+
public interface ComputerPredicate {
7+
8+
boolean filter(Computer c);
9+
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.doublecolon.function;
2+
3+
import java.util.Objects;
4+
import java.util.function.Function;
5+
6+
@FunctionalInterface
7+
public interface TriFunction<A, B, C, R> {
8+
9+
R apply(A a, B b, C c);
10+
11+
default <V> TriFunction<A, B, C, V> andThen(final Function<? super R, ? extends V> after) {
12+
Objects.requireNonNull(after);
13+
return (final A a, final B b, final C c) -> after.apply(apply(a, b, c));
14+
}
15+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.baeldung.doublecolon;
2+
3+
import com.baeldung.doublecolon.function.TriFunction;
4+
import org.junit.After;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.*;
10+
import java.util.function.BiFunction;
11+
12+
import static com.baeldung.doublecolon.ComputerUtils.*;
13+
14+
public class TestComputerUtils {
15+
16+
@Before
17+
public void setup() {
18+
}
19+
20+
@After
21+
public void tearDown() {
22+
}
23+
24+
@Test
25+
public void testConstructorReference() {
26+
27+
Computer c1 = new Computer(2015, "white");
28+
Computer c2 = new Computer(2009, "black");
29+
Computer c3 = new Computer(2014, "black");
30+
31+
BiFunction<Integer, String, Computer> c4Function = Computer::new;
32+
Computer c4 = c4Function.apply(2013, "white");
33+
BiFunction<Integer, String, Computer> c5Function = Computer::new;
34+
Computer c5 = c5Function.apply(2010, "black");
35+
BiFunction<Integer, String, Computer> c6Function = Computer::new;
36+
Computer c6 = c6Function.apply(2008, "black");
37+
38+
List<Computer> inventory = Arrays.asList(c1, c2, c3, c4, c5, c6);
39+
40+
List<Computer> blackComputer = filter(inventory, blackPredicate);
41+
Assert.assertEquals("The black Computers are: ", blackComputer.size(), 4);
42+
43+
List<Computer> after2010Computer = filter(inventory, after2010Predicate);
44+
Assert.assertEquals("The Computer bought after 2010 are: ", after2010Computer.size(), 3);
45+
46+
List<Computer> before2011Computer = filter(inventory, c -> c.getAge() < 2011);
47+
Assert.assertEquals("The Computer bought before 2011 are: ", before2011Computer.size(), 3);
48+
49+
inventory.sort(Comparator.comparing(Computer::getAge));
50+
51+
Assert.assertEquals("Oldest Computer in inventory", c6, inventory.get(0));
52+
53+
}
54+
55+
@Test
56+
public void testStaticMethodReference() {
57+
58+
Computer c1 = new Computer(2015, "white", 35);
59+
Computer c2 = new Computer(2009, "black", 65);
60+
TriFunction<Integer, String, Integer, Computer> c6Function = Computer::new;
61+
Computer c3 = c6Function.apply(2008, "black", 90);
62+
63+
List<Computer> inventory = Arrays.asList(c1, c2, c3);
64+
inventory.forEach(ComputerUtils::repair);
65+
66+
Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty());
67+
}
68+
69+
@Test
70+
public void testInstanceMethodArbitraryObjectParticularType() {
71+
72+
Computer c1 = new Computer(2015, "white", 35);
73+
Computer c2 = new MacbookPro(2009, "black", 65);
74+
List<Computer> inventory = Arrays.asList(c1, c2);
75+
inventory.forEach(Computer::turnOnPc);
76+
77+
}
78+
79+
@Test
80+
public void testSuperMethodReference() {
81+
82+
final TriFunction<Integer, String, Integer, MacbookPro> integerStringIntegerObjectTriFunction = MacbookPro::new;
83+
final MacbookPro macbookPro = integerStringIntegerObjectTriFunction.apply(2010, "black", 100);
84+
Double initialValue = new Double(999.99);
85+
final Double actualValue = macbookPro.calculateValue(initialValue);
86+
Assert.assertEquals(766.659, actualValue, 0.0);
87+
}
88+
89+
}

‎core-java-8/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public void givenListContainsNulls_whenFilteringSerial_thenCorrect() {
3030
assertThat(listWithoutNulls, hasSize(3));
3131
}
3232

33+
@Test
34+
public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() {
35+
final List<Integer> listWithoutNulls = Lists.newArrayList(null, 1, 2, null, 3, null);
36+
listWithoutNulls.removeIf(p -> p == null);
37+
38+
assertThat(listWithoutNulls, hasSize(3));
39+
}
40+
3341
@Test
3442
public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() {
3543
final List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.baeldung.java;
2+
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.IntStream;
11+
import java.util.stream.Stream;
12+
13+
import org.junit.Test;
14+
15+
import com.google.common.collect.Lists;
16+
17+
public class CollectionJavaPartitionUnitTest {
18+
19+
// java8 groupBy
20+
@Test
21+
public final void givenList_whenParitioningIntoNSublistsUsingGroupingBy_thenCorrect() {
22+
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
23+
24+
final Map<Integer, List<Integer>> groups = intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3));
25+
final List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
26+
27+
// When
28+
final List<Integer> lastPartition = subSets.get(2);
29+
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
30+
assertThat(subSets.size(), equalTo(3));
31+
assertThat(lastPartition, equalTo(expectedLastPartition));
32+
33+
// intList.add(9);
34+
// System.out.println(groups.values());
35+
}
36+
37+
// java8 partitionBy
38+
@Test
39+
public final void givenList_whenParitioningIntoSublistsUsingPartitionBy_thenCorrect() {
40+
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
41+
42+
final Map<Boolean, List<Integer>> groups = intList.stream().collect(Collectors.partitioningBy(s -> s > 6));
43+
final List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
44+
45+
// When
46+
final List<Integer> lastPartition = subSets.get(1);
47+
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
48+
assertThat(subSets.size(), equalTo(2));
49+
assertThat(lastPartition, equalTo(expectedLastPartition));
50+
51+
// intList.add(9);
52+
// System.out.println(groups.values());
53+
}
54+
55+
// java8 split by separator
56+
@Test
57+
public final void givenList_whenSplittingBySeparator_thenCorrect() {
58+
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6, 0, 7, 8);
59+
60+
final int[] indexes = Stream.of(IntStream.of(-1), IntStream.range(0, intList.size()).filter(i -> intList.get(i) == 0), IntStream.of(intList.size())).flatMapToInt(s -> s).toArray();
61+
final List<List<Integer>> subSets = IntStream.range(0, indexes.length - 1).mapToObj(i -> intList.subList(indexes[i] + 1, indexes[i + 1])).collect(Collectors.toList());
62+
63+
// When
64+
final List<Integer> lastPartition = subSets.get(2);
65+
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
66+
assertThat(subSets.size(), equalTo(3));
67+
assertThat(lastPartition, equalTo(expectedLastPartition));
68+
}
69+
70+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name":
3+
{
4+
"first": "Tatu",
5+
"last": "Saloranta"
6+
},
7+
8+
"title": "Jackson founder",
9+
"company": "FasterXML"
10+
}

0 commit comments

Comments
 (0)