Skip to content

Instantly share code, notes, and snippets.

View techindepth's full-sized avatar
👋

ANISH ANTONY techindepth

👋
View GitHub Profile
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamPeekEx {
public static void main(String a[]) {
Stream.of("bus", "car", "bycle", "flight", "train")
.filter(e -> e.length() > 3)
.peek(e -> System.out.println("Filtered value: " + e))
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamPeekEx {
public static void main(String a[]) {
Stream.of("bus", "car", "bycle", "flight", "train")
.filter(e -> e.length() > 3)
.peek(e -> System.out.println("Filtered value: " + e))
public static void main(String[] args) {
System.out.println("Stream without terminal operation");
Arrays.stream(new int[] { 1, 2, 3 }).map(i -> {
System.out.println("doubling " + i);
return i * 2;
});
System.out.println("Stream with terminal operation");
Arrays.stream(new int[] { 1, 2, 3 }).map(i -> {
import java.time.LocalTime;
import java.util.Arrays;
import java.util.stream.Stream;
public class SequentialParallelComparison {
public static void main (String[] args) {
String[] strings = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
System.out.println("-------\nRunning sequential\n-------");
targetLongList =
sourceLongList.stream().
filter(l -> l > 100).
collect(Collectors.toList());
@Test
public void testflatMap() throws Exception {
List<Integer> together = Stream.of(asList(1, 2), asList(3, 4)) // Stream of List<Integer>
.flatMap(List::stream)
.map(integer -> integer + 1)
.collect(Collectors.toList());
assertEquals(asList(2, 3, 4, 5), together);
}
@Test
public void convertStringToUpperCaseStreams() {
List<String> collected = Stream.of("a", "b", "hello") // Stream of String
.map(String::toUpperCase) // Returns a stream consisting of the results of applying the given function to the elements of this stream.
.collect(Collectors.toList());
assertEquals(asList("A", "B", "HELLO"), collected);
}
Stream<String> stringStream = Stream.of("a", "b", "c");
String[] stringArray = stringStream.toArray(size -> new String[size]);
Arrays.stream(stringArray).forEach(System.out::println);
/*Output
a
b
c
*/
Map<String, Choice> result =
choices.stream().collect(Collectors.toMap(Choice::getName,
Function.identity()));
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>