Java 8 - Methods in Streams API
Methods in Stream:
1. collect()
It is used to collect element from the stream and add the object into specified collection.
2. count()
It is used to count the element from the stream and return the count.
long count = l.stream().map(I -> I*2).count();
3. sorted()
* Used to sort the element from the stream.
* Default sorting & our own custom sorting.
Types:
a. sorted -> Default sorting order.
Eg. List<Integer> l2 = l.stream().sorted().collect(Collectors.toList());
b. sorted(Comparator c) -> Custom sorting order
Eg. List<Integer> l2 = l.stream().sorted((i1, i2) -> -i1.compareTo(i2)).collect(Collectors.toList());
when you using (-) then reverse sorting.
4. min() & max()
Note: Please sort the collection before using this.
min():
* If you want to take the min value from the collection then you have to go for this.
Eg.
Integer min = l.stream().min((i1,i2) -> i1.compareTo(i2)).get();
System.out.println(min);
max():
* If you want to take the max value from the collection then you have to go for this.
Eg.
Integer max = l.stream().max((i1,i2) -> i1.compareTo(i2)).get();
System.out.println(max);
5. forEach()
* This method can take lambda expression as argument, and apply that lambda expression for each element present in steam.
* This method can't return anything.
Eg.
l.stream().forEach(s -> System.out.println(s));
or
l.stream().forEach(System.out::println);
6. toArray()
* If you want to convert all the object into array from the stream.
Eg.
Integer[] i = l.stream().toArray(Integer[]::new);
7. stream.Of()
* We can apply stream for group of values and arrays.
a. For Group of values,
Stream<Integer> s = Stream.of(9,99,999,9999,99999);
s.forEach(System.out::println);
Output:
9
99
999
9999
99999
b. For Arrays,
Double[] d = {10.0,10.1,10.2,10.3,10.4}
Stream<Dream> s1 =Stream.of(d);
s1.forEach(System.out::println);
1. collect()
It is used to collect element from the stream and add the object into specified collection.
2. count()
It is used to count the element from the stream and return the count.
long count = l.stream().map(I -> I*2).count();
3. sorted()
* Used to sort the element from the stream.
* Default sorting & our own custom sorting.
Types:
a. sorted -> Default sorting order.
Eg. List<Integer> l2 = l.stream().sorted().collect(Collectors.toList());
b. sorted(Comparator c) -> Custom sorting order
Eg. List<Integer> l2 = l.stream().sorted((i1, i2) -> -i1.compareTo(i2)).collect(Collectors.toList());
when you using (-) then reverse sorting.
4. min() & max()
Note: Please sort the collection before using this.
min():
* If you want to take the min value from the collection then you have to go for this.
Eg.
Integer min = l.stream().min((i1,i2) -> i1.compareTo(i2)).get();
System.out.println(min);
max():
* If you want to take the max value from the collection then you have to go for this.
Eg.
Integer max = l.stream().max((i1,i2) -> i1.compareTo(i2)).get();
System.out.println(max);
5. forEach()
* This method can take lambda expression as argument, and apply that lambda expression for each element present in steam.
* This method can't return anything.
Eg.
l.stream().forEach(s -> System.out.println(s));
or
l.stream().forEach(System.out::println);
6. toArray()
* If you want to convert all the object into array from the stream.
Eg.
Integer[] i = l.stream().toArray(Integer[]::new);
7. stream.Of()
* We can apply stream for group of values and arrays.
a. For Group of values,
Stream<Integer> s = Stream.of(9,99,999,9999,99999);
s.forEach(System.out::println);
Output:
9
99
999
9999
99999
b. For Arrays,
Double[] d = {10.0,10.1,10.2,10.3,10.4}
Stream<Dream> s1 =Stream.of(d);
s1.forEach(System.out::println);
Comments
Post a Comment