Examples for java stream api

This commit is contained in:
NKaushik89
2018-11-29 22:46:51 +05:30
parent 3763010afc
commit 6d89df562e
5 changed files with 219 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
import java.util.ArrayList;
import java.util.List;
public class ExampleParallelStream {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
intList.add(3);
intList.add(4);
intList.add(5);
intList.parallelStream().
filter(e -> {
System.out.println("filter " + e);
return true;
}).
forEach(e -> System.out.println("ForEach " + e));
}
}

View File

@@ -0,0 +1,40 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ExampleStreamConversion {
public static void main(String[] args) {
//stream to list
//1
Stream<Integer> numStream = Stream.of(1,2,3,4,5,6,7);
List<Integer> numList = numStream.collect(Collectors.toList());
System.out.println(numList);
//2
numStream = Stream.of(1,2,3,4,5,6,7);
List<Integer> numList1 = numStream.collect(Collectors.toCollection(ArrayList::new));
//3
Stream<Integer> numStream2 = Stream.of(1,2,3,4,5,6,7);
List<Integer> numList2 = new ArrayList<>();
numStream2.forEach(numList::add);
//stream to map
Stream<String[]> mapStream = Stream.of(new String[][]{{"1", "one"}, {"2", "two"}});
Map<String, String> finalMap = mapStream.collect(Collectors.toMap(e -> e[0], e -> e[1]));
System.out.println(finalMap);
//stream to array
//1
Stream<Integer> intStream = Stream.of(1, 2, 3, 4, 5, 6);
Integer[] intArray = intStream.toArray(Integer[]::new);
System.out.println(Arrays.toString(intArray));
//2
Stream<Integer> intStream2 = Stream.of(1, 2, 3, 4, 5, 6);
int[] intArray2 = intStream2.mapToInt(i -> i).toArray();
}
}

View File

@@ -0,0 +1,75 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class ExampleStreamCreation {
public static void main(String[] args) {
//Stream from values
Stream stream = Stream.of(1);
Stream<Character> charStream = Stream.of('a', 'b', 'c');
Stream<String> strStream = Stream.of("Hello", "World");
//Stream from a string
IntStream intStream = "Hello World !!".chars();
IntStream intStream1 = "Hello World !!".codePoints();
//stream from pattern
Pattern p = Pattern.compile(",");
StringBuilder stringBuilder = new StringBuilder("1,2,3,4,5");
Stream<String> patternStream = p.splitAsStream(stringBuilder);
//stream from array
Stream<Integer> intStream2 = Stream.of(new Integer[]{1, 2, 3, 4, 5});
IntStream intStream3 = Arrays.stream(new int[]{1, 2, 3, 4});
int[] mainArray = new int[]{1, 2, 3, 4, 5, 6, 7};
IntStream s = Arrays.stream(mainArray, 2, 5);
//stream from collection
List<String> wordList = new ArrayList<>();
wordList.add("Hello");
wordList.add("World");
Stream wordStream = wordList.stream();
Stream parallelWordStream = wordList.parallelStream();
//from a set
Set set = new HashSet();
set.add("First element");
set.add("Second element");
set.add("Third element");
Stream setStream = set.stream();
//empty stream
Stream emptyStream = Stream.empty();
Stream emptyStream1 = Stream.of();
//from generate
Stream.generate(new Random()::nextInt).limit(6).forEach(System.out::println);
//from iterate
Stream.iterate(3, n -> n + 1).limit(6).forEach(System.out::println);
//from file
try {
Stream<String> fileStream = Files.lines(Paths.get("C:\\sample.txt"));
fileStream.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
try {
Optional<String> lineWithStreamWord = Files.lines(Paths.get("C:\\sample.txt")).filter(s1 -> s1.contains("stream")).findFirst();
if (lineWithStreamWord.isPresent()) {
System.out.println(lineWithStreamWord.get());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,67 @@
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ExampleStreamOperation {
public static void main(String[] args) {
// filter()
Stream<Integer> intStream = Stream.of(1, 2, 3, 4, 5, 6);
Stream<Integer> evenStream = intStream.filter(e -> (e % 2 == 0));
evenStream.forEach(e -> System.out.print(e+" "));
//map()
Stream<Integer> intStream1 = Stream.of(1, 2, 3, 4, 5, 6);
Stream<Integer> mapStream = intStream1.map(e -> e * 10);
mapStream.forEach(e -> System.out.print(e + " "));
//sorted()
Stream<Integer> intStream3 = Stream.of(4,22,1,7,5,10);
Stream<Integer> sortedStream = intStream3.sorted();
sortedStream.forEach(e -> System.out.print(e + " "));
//foreach()
Stream<Integer> intStream4 = Stream.of(1, 2, 3, 4, 5, 6, 7);
intStream4.forEach(e -> System.out.print(e * 3 + " "));
//using a map :
Map<String,Integer> map = new HashMap<>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",4);
map.forEach((key,value) -> System.out.println("Key: "+key+" Value: "+value));
//reduce
Stream<Integer> intStream5 = Stream.of(1, 2, 3, 4);
//reduce will add each element of the stream
Optional<Integer> optionalResult = intStream5.reduce((i, j) -> i + j);
System.out.println(optionalResult.get());
//count
Stream<Integer> intStream6 = Stream.of(1, 2, 3, 4);
System.out.println("Total elements "+intStream6.count());
//collect()
Stream<Integer> intStream7 = Stream.of(1, 2, 3, 4, 5, 6, 7);
List<Integer> intList = intStream7.collect(Collectors.toList());
System.out.print(intList);
//match()
List<String> strList = new ArrayList<>();
strList.add("Sun");
strList.add("Mon");
strList.add("Tues");
//check if any value in the list starts with 'T'
System.out.println(strList.stream().anyMatch(e -> e.startsWith("T")));
//check if all values in the list start with 'T'
System.out.println(strList.stream().allMatch(e -> e.startsWith("T")));
//check if none of the values in the list starts with 'X'
System.out.println(strList.stream().noneMatch(e -> e.startsWith("X")));
}
}

View File

@@ -0,0 +1,15 @@
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
public class ExampleStreamSequence {
public static void main(String[] args) {
Set<Integer> intSet = new TreeSet<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
Object[] array = intSet.stream().parallel().limit(8).toArray();
Object[] array1 = intSet.stream().unordered().parallel().limit(8).toArray();
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(array1));
}
}