more examples

parent f008ace6
...@@ -15,57 +15,142 @@ Terminal Operations: ...@@ -15,57 +15,142 @@ Terminal Operations:
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class StreamImpl { public class StreamImpl {
public static void main(String[] args) { public static void main(String[] args) {
//
// filter() method. //// filter() method.
// Example 1 //// Example 1
// int[] numbers = {1,2,3,4,5,6}; // int[] numbers = {1,2,3,4,5,6};
// primitive to wrapper type is boxing. //// primitive to wrapper type is boxing.
// wrapper to primitive is unboxing. //// wrapper to primitive is unboxing.
// Arrays.stream(numbers).boxed().filter(x -> x != 3).forEach(System.out::println); // Arrays.stream(numbers).boxed().filter(x -> x != 3).forEach(System.out::println);
//
// int[] myNumbers = IntStream.range(1,9).filter(x -> x != 3).toArray(); // int[] myNumbers = IntStream.range(1,9).filter(x -> x != 3).toArray();
// System.out.println(Arrays.toString(myNumbers)); // System.out.println(Arrays.toString(myNumbers));
//
// // Example 2 Convert an array of string type numbers to int type. // // Example 2 Convert an array of string type numbers to int type.
// String[] stringNumbers = {"1","2","3","4","5","6"}; // String[] stringNumbers = {"1","2","3","4","5","6"};
// int[] intNumbers = Arrays.stream(stringNumbers).mapToInt(Integer::parseInt).toArray(); // int[] intNumbers = Arrays.stream(stringNumbers).mapToInt(Integer::parseInt).toArray();
// System.out.println(Arrays.toString(intNumbers)); // System.out.println(Arrays.toString(intNumbers));
//
//
// // Example 3 Convert array of primitive integers to List of Integers. // // Example 3 Convert array of primitive integers to List of Integers.
// int[] primitiveNumbers = {1,2,3,4,5,6}; // int[] primitiveNumbers = {1,2,3,4,5,6};
// List<Integer> wrapperIntegers = Arrays.stream(primitiveNumbers).boxed().collect(Collectors.toList()); // List<Integer> wrapperIntegers = Arrays.stream(primitiveNumbers).boxed().collect(Collectors.toList());
// System.out.println(wrapperIntegers); // System.out.println(wrapperIntegers);
//
// // Example 4 Mapping an array of string, replacing an element with its integer length. // // Example 4 Mapping an array of string, replacing an element with its integer length.
// String[] words = {"Qadeer", "Naveed", "Adam", "Sam"}; // String[] words = {"Qadeer", "Naveed", "Adam", "Sam"};
// int[] wordLength = Arrays.stream(words).mapToInt(String::length).toArray(); // int[] wordLength = Arrays.stream(words).mapToInt(String::length).toArray();
// System.out.println(Arrays.toString(wordLength)); // System.out.println(Arrays.toString(wordLength));
//
// // Example 5 Filter elements greater than a particular length. // // Example 5 Filter elements greater than a particular length.
// String[] words = {"Qadeer", "Naveed", "Adam", "Sam"}; // String[] word1 = {"Qadeer", "Naveed", "Adam", "Sam"};
// Object[] filteredWords = Arrays.stream(words).filter(s -> s.length() > 5).toArray(); // Object[] filteredWords = Arrays.stream(words).filter(s -> s.length() > 5).toArray();
// System.out.println(Arrays.toString(filteredWords)); // System.out.println(Arrays.toString(filteredWords));
//
// // Example 6 Convert array of primitive type to a List of Wrapper type. // // Example 6 Convert array of primitive type to a List of Wrapper type.
// int[] array = {1,2,3,4,5,6}; // int[] array = {1,2,3,4,5,6};
// List<Integer> integerStream = IntStream.of(array).boxed().collect(Collectors.toList()); // List<Integer> integerStream = IntStream.of(array).boxed().collect(Collectors.toList());
// System.out.println(integerStream); // System.out.println(integerStream);
//
// // Example 7 // // Example 7 list to array
// List<Integer> number = Arrays.asList(1,2,3,4,5); // List<Integer> number = Arrays.asList(1,2,3,4,5);
// int[] newNum = number.stream().mapToInt(Integer::intValue).toArray(); // int[] newNum = number.stream().mapToInt(Integer::intValue).toArray();
// System.out.println(Arrays.toString(newNum)); // System.out.println(Arrays.toString(newNum));
//
// // Example 8 // // Example 8 using filter() method
// String[] words = {"Qadeer", "Naveed", "Bilal", "Ben", "Qabeer", "Qad", "steer", "butter"}; // String[] wordss = {"Qadeer", "Naveed", "Bilal", "Ben", "Qabeer", "Qad", "steer", "butter"};
// Object[] filteredWords = Arrays.stream(words).filter(s -> s.startsWith("Qa")).filter(s -> s.endsWith("er")).toArray(); // Object[] filteredWordss = Arrays.stream(words).filter(s -> s.startsWith("Qa")).filter(s -> s.endsWith("er")).toArray();
// System.out.println(Arrays.toString(filteredWords)); // System.out.println(Arrays.toString(filteredWords));
//
// // Example 9 Count the number of elements in an array.
// int[] num = {1,2,3,4,5,6};
// int count = (int) Arrays.stream(num).count();
// System.out.println(count);
//
// // Example 10 Count all even numbers
// int[] nums = {1,2,5,6,7,8,9,1,2,4,100,200,231,432,345,211,223};
// int counts = (int) Arrays.stream(nums).filter(x -> x % 2 == 0).count();
// System.out.println(counts);
// // Example 11 Count all odd numbers
// int[] nums = {1,2,5,6,7,8,9,1,2,4,100,200,231,432,345,211,223};
// int oddCount = (int) Arrays.stream(nums).filter(x -> x % 2 !=0).count();
// System.out.println(oddCount);
// // Example 12 Debug a stream
// Arrays.stream(new int[]{5,4,3,1,2})
// .boxed()
// .sorted()
// .map(x -> x * 2)
// .forEach(System.out::println);
// String[] argss = {"20", "20"};
// IntStream.iterate(1, n -> n + 1)
// .skip(Integer.parseInt(argss[0]))
// .limit(Integer.parseInt(argss[1]))
// .filter(x -> x % 2 == 0)
// .forEach(System.out::println);
// // Example 13
// Map<String, String> books = new HashMap<>();
// books.put(
// "978-0201633610", "Design patterns : elements of reusable object-oriented software");
// books.put(
// "978-1617291999", "Java 8 in Action: Lambdas, Streams, and functional-style programming");
// books.put("978-0134685991", "Effective Java");
//
// Optional<String> isbn = books.entrySet().stream()
// .filter(e -> "Effective Java".equals(e.getValue()))
// .map(Map.Entry::getKey)
// .findFirst();
//
// isbn.ifPresent(System.out::println);
//
// Optional<String> secondIsbn = books.entrySet().stream()
// .filter(e -> "This does not exist in the map".equals(e.getValue()))
// .map(Map.Entry::getKey).findFirst();
// secondIsbn.ifPresent(System.out::println);
//
// List<Long> thirdIsbn = books.keySet().stream()
// .filter(s -> s.startsWith("978"))
// .map(s -> s.replace("-", ""))
// .mapToLong(Long::parseLong)
// .boxed()
// .collect(Collectors.toList());
// System.out.println(thirdIsbn);
//
// List<Long> isbn = books.keySet().stream()
// .filter(s -> s.startsWith("978"))
// .map(s -> s.replace("-", ""))
// .mapToLong(Long::parseLong)
// .boxed()
// .collect(Collectors.toList());
// // Example 14
// // 0 + 1 * 2
// // 2 + 2 * 2
// // 6 + 3 * 2
// // 12 + 4 * 2
// // 20
// // a = running sum
// // b = numbers[i]
// // reduce(initial value of a, (a, b) -> {logic})
// List<Integer> numbers = Arrays.asList(1,2,3,4);
// Integer product = numbers.stream().reduce(0, (a, b) -> {
// return a + (b * 2);
// });
// System.out.println(product);
// Example 15
List<Integer> numbers = Arrays.asList(-1,2,3,4, 41, 12, 32, 14, 51 ,16);
Integer product = numbers.stream().reduce(Integer.MIN_VALUE, (a, b) -> a > b ? a : b);
System.out.println(product);
}
public static class MathOperations {
public static int add(int a, int b) {
return a + b;
}
} }
} }
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
public class Testing {
public static void main(String[] args) {
// AtomicInteger sums = new AtomicInteger();
//int sums = 0;
int[] numbers = {1,2,3,4,5,6};
int sums = Arrays.stream(numbers).reduce(0, Integer::sum);
System.out.println(sums);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment