forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-1078 Stream Indices - Update pom.xml (eugenp#2516)
* Update pom.xml * BAEL-1078 Stream indices
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 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
37 changes: 37 additions & 0 deletions
37
core-java/src/main/java/com/baeldung/stream/StreamIndices.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,37 @@ | ||
package com.baeldung.stream; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import com.codepoetics.protonpack.Indexed; | ||
import com.codepoetics.protonpack.StreamUtils; | ||
|
||
public class StreamIndices { | ||
|
||
public static List<String> getEvenIndexedStrings(String[] names) { | ||
List<String> evenIndexedNames = IntStream.range(0, names.length) | ||
.filter(i -> i % 2 == 0).mapToObj(i -> names[i]) | ||
.collect(Collectors.toList()); | ||
return evenIndexedNames; | ||
} | ||
|
||
public static List<Indexed<String>> getEvenIndexedStrings(List<String> names) { | ||
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream()) | ||
.filter(i -> i.getIndex() % 2 == 0).collect(Collectors.toList()); | ||
return list; | ||
} | ||
|
||
public static List<Indexed<String>> getOddIndexedStrings(List<String> names) { | ||
List<Indexed<String>> list = StreamUtils.zipWithIndex(names.stream()) | ||
.filter(i -> i.getIndex() % 2 == 1).collect(Collectors.toList()); | ||
return list; | ||
} | ||
|
||
public static List<String> getOddIndexedStrings(String[] names) { | ||
List<String> oddIndexedNames = IntStream.range(0, names.length) | ||
.filter(i -> i % 2 == 1).mapToObj(i -> names[i]) | ||
.collect(Collectors.toList()); | ||
return oddIndexedNames; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core-java/src/test/java/com/baeldung/stream/StreamIndicesTest.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,50 @@ | ||
package com.baeldung.stream; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import com.codepoetics.protonpack.Indexed; | ||
|
||
public class StreamIndicesTest { | ||
|
||
@Test | ||
public void givenArray_whenGetIndexedStrings_thenReturnListOfEvenIndexedStrings() { | ||
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" }; | ||
List<String> expectedResult = Arrays.asList("Afrim", "Besim", "Durim"); | ||
List<String> actualResult = StreamIndices.getEvenIndexedStrings(names); | ||
|
||
assertEquals(expectedResult, actualResult); | ||
} | ||
|
||
@Test | ||
public void givenArray_whenGetIndexedStrings_thenReturnListOfOddStrings() { | ||
String[] names = { "Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim" }; | ||
List<String> expectedResult = Arrays.asList("Bashkim", "Lulzim", "Shpetim"); | ||
List<String> actualResult = StreamIndices.getOddIndexedStrings(names); | ||
|
||
assertEquals(expectedResult, actualResult); | ||
} | ||
|
||
@Test | ||
public void givenList_whenGetIndexedStrings_thenReturnListOfEvenIndexedStrings() { | ||
List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim"); | ||
List<Indexed<String>> expectedResult = Arrays.asList(Indexed.index(0, "Afrim"), Indexed.index(2, "Besim"), Indexed.index(4, "Durim")); | ||
List<Indexed<String>> actualResult = StreamIndices.getEvenIndexedStrings(names); | ||
|
||
assertEquals(expectedResult, actualResult); | ||
} | ||
|
||
@Test | ||
public void givenList_whenGetIndexedStrings_thenReturnListOfOddIndexedStrings() { | ||
List<String> names = Arrays.asList("Afrim", "Bashkim", "Besim", "Lulzim", "Durim", "Shpetim"); | ||
List<Indexed<String>> expectedResult = Arrays.asList(Indexed.index(1, "Bashkim"), Indexed.index(3, "Lulzim"), Indexed.index(5, "Shpetim")); | ||
List<Indexed<String>> actualResult = StreamIndices.getOddIndexedStrings(names); | ||
|
||
assertEquals(expectedResult, actualResult); | ||
} | ||
|
||
} |