|
| 1 | +package lambda; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.CoreMatchers.*; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.hamcrest.CoreMatchers; |
| 12 | +import org.hamcrest.MatcherAssert; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +public class LambdaExpressionTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void shouldRunUsingAnonymousClass() throws Exception { |
| 19 | + Runnable runnable = new Runnable() { |
| 20 | + |
| 21 | + @Override |
| 22 | + public void run() { |
| 23 | + System.out.println("Yes, anonymous object here "); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + runnable.run(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void shouldRunRunnableObjectWithoutAnonymousClass() throws Exception { |
| 32 | + Runnable runnable = () -> System.out.println("Awesome! Lambda Expression here!"); |
| 33 | + |
| 34 | + runnable.run(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void shouldOrderTheListOfNamesByUsingAnonymousClass() throws Exception { |
| 39 | + List<String> craftCoderGuides = Arrays.asList("Mockito", "CDI", "JUnit", "Hibernate", "Spring"); |
| 40 | + |
| 41 | + Collections.sort(craftCoderGuides, new Comparator<String>() { |
| 42 | + |
| 43 | + @Override |
| 44 | + public int compare(String firstGuide, String secondGuide) { |
| 45 | + return firstGuide.compareTo(secondGuide); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + assertThat(craftCoderGuides, CoreMatchers.hasItems("CDI", "Hibernate", "JUnit", "Mockito", "Spring")); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void shouldOrderTheListOfNamesByLambdaExpression() throws Exception { |
| 54 | + List<String> craftCoderGuides = Arrays.asList("Mockito", "CDI", "JUnit", "Hibernate", "Spring"); |
| 55 | + |
| 56 | + |
| 57 | + Collections.sort(craftCoderGuides, (String firstGuide, String secondGuide) -> { |
| 58 | + return firstGuide.compareTo(secondGuide); |
| 59 | + }); |
| 60 | + |
| 61 | + assertThat(craftCoderGuides, CoreMatchers.hasItems("CDI", "Hibernate", "JUnit", "Mockito", "Spring")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void shouldOrderTheListOfNamesByLambdaExpressionWithoutBracesAndReturnKeyword() throws Exception { |
| 66 | + List<String> craftCoderGuides = Arrays.asList("Mockito", "CDI", "JUnit", "Hibernate", "Spring"); |
| 67 | + |
| 68 | + Collections.sort(craftCoderGuides, (String firstGuide, String secondGuide) -> firstGuide.compareTo(secondGuide)); |
| 69 | + |
| 70 | + assertThat(craftCoderGuides, CoreMatchers.hasItems("CDI", "Hibernate", "JUnit", "Mockito", "Spring")); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void shouldOrderTheListOfNamesByLambdaExpressionWithoutParameterTypes() throws Exception { |
| 75 | + List<String> craftCoderGuides = Arrays.asList("Mockito", "CDI", "JUnit", "Hibernate", "Spring"); |
| 76 | + |
| 77 | + Collections.sort(craftCoderGuides, (firstGuide, secondGuide) -> firstGuide.compareTo(secondGuide)); |
| 78 | + |
| 79 | + assertThat(craftCoderGuides, CoreMatchers.hasItems("CDI", "Hibernate", "JUnit", "Mockito", "Spring")); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments