-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from eugenp/master
sync with origin
- Loading branch information
Showing
157 changed files
with
1,952 additions
and
915 deletions.
There are no files selected for viewing
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,3 @@ | ||
### Relevant Articles: | ||
================================ | ||
- [Building a Microservice with Apache Meecrowave](http://www.baeldung.com/apache-meecrowave) |
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
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
9 changes: 9 additions & 0 deletions
9
core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj
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,9 @@ | ||
package com.baeldung.aspect; | ||
|
||
public aspect ChangeCallsToCurrentTimeInMillisMethod { | ||
long around(): | ||
call(public static native long java.lang.System.currentTimeMillis()) | ||
&& within(user.code.base.pckg.*) { | ||
return 0; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
core-java-8/src/test/java/com/baeldung/list/AddElementsUnitTest.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,87 @@ | ||
package com.baeldung.list; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.*; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class AddElementsUnitTest { | ||
|
||
List<Flower> flowers; | ||
|
||
@Before | ||
public void init() { | ||
this.flowers = new ArrayList<>(Arrays.asList( | ||
new Flower("Poppy", 12), | ||
new Flower("Anemone", 8), | ||
new Flower("Catmint", 12))); | ||
} | ||
|
||
@Test | ||
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() { | ||
List<Flower> anotherList = new ArrayList<>(); | ||
anotherList.addAll(flowers); | ||
|
||
assertEquals(anotherList.size(), flowers.size()); | ||
Assert.assertTrue(anotherList.containsAll(flowers)); | ||
} | ||
|
||
@Test | ||
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() { | ||
List<Flower> anotherList = new ArrayList<>(); | ||
anotherList.addAll(flowers); | ||
|
||
Flower flower = anotherList.get(0); | ||
flower.setPetals(flowers.get(0).getPetals() * 3); | ||
|
||
assertEquals(anotherList.size(), flowers.size()); | ||
Assert.assertTrue(anotherList.containsAll(flowers)); | ||
} | ||
|
||
@Test | ||
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() { | ||
List<Flower> target = new ArrayList<>(); | ||
|
||
Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0)); | ||
|
||
assertEquals(target.size(), 4); | ||
} | ||
|
||
@Test | ||
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() { | ||
List<Flower> flowerVase = new ArrayList<>(); | ||
|
||
flowers.stream() | ||
.skip(1) | ||
.forEachOrdered(flowerVase::add); | ||
|
||
assertEquals(flowerVase.size() + 1, flowers.size()); | ||
assertFalse(flowerVase.containsAll(flowers)); | ||
} | ||
|
||
@Test | ||
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() { | ||
List<Flower> flowerVase = new ArrayList<>(); | ||
|
||
flowers.stream() | ||
.filter(f -> f.getPetals() > 10) | ||
.forEachOrdered(flowerVase::add); | ||
|
||
assertEquals(flowerVase.size() + 1, flowers.size()); | ||
assertFalse(flowerVase.containsAll(flowers)); | ||
} | ||
|
||
@Test | ||
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() { | ||
List<Flower> target = new ArrayList<>(); | ||
|
||
Optional.ofNullable(flowers) | ||
.ifPresent(target::addAll); | ||
|
||
assertNotNull(target); | ||
assertEquals(target.size(), 3); | ||
} | ||
} |
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
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
27 changes: 27 additions & 0 deletions
27
...a-9/src/main/java/com/baeldung/java9/language/stream/StreamsGroupingCollectionFilter.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,27 @@ | ||
package com.baeldung.java9.language.stream; | ||
|
||
import static java.util.stream.Collectors.filtering; | ||
import static java.util.stream.Collectors.groupingBy; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public class StreamsGroupingCollectionFilter { | ||
|
||
static public Map<Integer, List<Integer>> findEvenNumbersAfterGroupingByQuantityOfDigits(Collection<Integer> baseCollection) { | ||
Function<Integer, Integer> getQuantityOfDigits = item -> (int) Math.log10(item) + 1; | ||
|
||
return baseCollection.stream() | ||
.collect(groupingBy(getQuantityOfDigits, filtering(item -> item % 2 == 0, toList()))); | ||
} | ||
|
||
static public Map<Integer, List<Integer>> findEvenNumbersBeforeGroupingByQuantityOfDigits(Collection<Integer> baseCollection) { | ||
|
||
return baseCollection.stream() | ||
.filter(item -> item % 2 == 0) | ||
.collect(groupingBy(item -> (int) Math.log10(item) + 1, toList())); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectionFilterUnitTest.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,51 @@ | ||
package com.baeldung.java9.language.stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class CollectionFilterUnitTest { | ||
|
||
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8); | ||
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap(); | ||
private static Map<Integer, List<Integer>> createExpectedFilterAfterGroupingMap() { | ||
Map<Integer, List<Integer>> map = new HashMap<>(); | ||
map.put(1, Arrays.asList(8)); | ||
map.put(2, Arrays.asList(12, 56)); | ||
map.put(3, Collections.emptyList()); | ||
map.put(4, Arrays.asList(8002, 2668)); | ||
return map; | ||
|
||
} | ||
|
||
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap(); | ||
private static Map<Integer, List<Integer>> createExpectedFilterBeforeGroupingMap() { | ||
Map<Integer, List<Integer>> map = new HashMap<>(); | ||
map.put(1, Arrays.asList(8)); | ||
map.put(2, Arrays.asList(12, 56)); | ||
map.put(4, Arrays.asList(8002, 2668)); | ||
return map; | ||
|
||
} | ||
|
||
@Test | ||
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() { | ||
Map<Integer, List<Integer>> filteredAfterGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersAfterGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION); | ||
Map<Integer, List<Integer>> filteredBeforeGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersBeforeGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION); | ||
|
||
assertThat(filteredAfterGroupingMap).containsAllEntriesOf(EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP); | ||
assertThat(filteredBeforeGroupingMap).doesNotContainKey(3) | ||
.containsAllEntriesOf(EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP); | ||
} | ||
|
||
} |
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
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
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
...ertlisttomap/ConvertListToMapService.java → ...ertlisttomap/ConvertListToMapService.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
16 changes: 16 additions & 0 deletions
16
...ollections/src/main/java/com/baeldung/java/filtering/CollectionUtilsCollectionFilter.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.baeldung.java.filtering; | ||
|
||
import java.util.Collection; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.collections4.Predicate; | ||
|
||
public class CollectionUtilsCollectionFilter { | ||
|
||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) { | ||
Predicate<Integer> apacheEventNumberPredicate = item -> item % 2 == 0; | ||
|
||
CollectionUtils.filter(baseCollection, apacheEventNumberPredicate); | ||
return baseCollection; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ections/src/main/java/com/baeldung/java/filtering/EclipseCollectionsCollectionFilter.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,32 @@ | ||
package com.baeldung.java.filtering; | ||
|
||
import java.util.Collection; | ||
|
||
import org.eclipse.collections.api.block.predicate.Predicate; | ||
import org.eclipse.collections.impl.factory.Lists; | ||
import org.eclipse.collections.impl.utility.Iterate; | ||
|
||
public class EclipseCollectionsCollectionFilter { | ||
|
||
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) { | ||
Predicate<Integer> eclipsePredicate = item -> item % 2 == 0; | ||
Collection<Integer> filteredList = Lists.mutable.ofAll(baseCollection) | ||
.select(eclipsePredicate); | ||
|
||
return filteredList; | ||
} | ||
|
||
static public Collection<Integer> findEvenNumbersUsingIterate(Collection<Integer> baseCollection) { | ||
Predicate<Integer> eclipsePredicate = new Predicate<Integer>() { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public boolean accept(Integer arg0) { | ||
return arg0 % 2 == 0; | ||
} | ||
}; | ||
Collection<Integer> filteredList = Iterate.select(baseCollection, eclipsePredicate); | ||
|
||
return filteredList; | ||
} | ||
} |
Oops, something went wrong.