- What is the Difference between Map and Filter?
Both perform intermediate Operations and returns stream as output.By using map, you transform the object values.
Map returns a stream consisting of the results of applying the given function to the elements of this stream. In a simple sentence, the map returns the transformed object value.
Filter is used for filtering the data, it always returns the boolean value. If it returns true, the item is added to list else it is filtered out (ignored) - Find the Sum of Nos in List?
public class ReduceEgs { public static void main(String[] args) { List<Integer> arrNumbers = Arrays.asList(1,2,3,4,5,6,7,8,9); Integer sum = arrNumbers.stream().reduce(0, Integer::sum); Integer sum2 = arrNumbers.stream().reduce(0, (x,y)->x+y); Integer sum3 = arrNumbers.stream().reduce(0, ReduceEgs::sumNos); } public static Integer sumNos(int x, int y){ System.out.println("a -"+ x + " b - "+ y); return x+y; } }
- Find the Min and Max in Nos List?
public static void main(String[] args) { List<Integer> arrNumbers = Arrays.asList(1,2,3,4,5,-6,7,-8,9); arrNumbers = Arrays.asList(-5,-6,-8); Integer Max = arrNumbers.stream().reduce(0, (x,y)->x>y?x:y); System.out.println("Output 1 ="+ Max); //Its good to start with Identity as Negative Value in order to address negative Integer Comparison //If you start with 0 then 0 would be returned like above Integer Max2 = arrNumbers.stream().reduce(Integer.MIN_VALUE, (x,y)->x>y?x:y); System.out.println("Output 2 ="+ Max2); arrNumbers = Arrays.asList(5,6,8); Integer Min = arrNumbers.stream().reduce(0, (x,y)->x<y?x:y); System.out.println("Output 3 ="+ Min); //Its good to start with Identity as Max Positive Value in order to address Integer Comparison Integer Min2 = arrNumbers.stream().reduce(Integer.MAX_VALUE, (x,y)->x<y?x:y); System.out.println("Output 4 ="+ Min2); }
Output
Output 1 =0 Output 2 =-5 Output 3 =0 Output 4 =5
- Find the Sum of Square of Nos in List?
public class ReduceEgs { public static void main(String[] args) { List<Integer> arrNumbers = Arrays.asList(1,2,3); Integer squareSum = arrNumbers.stream().map(x->x*x).reduce(0, Integer::sum); System.out.println("Output 1 ="+ squareSum); } }
Output
Output 1 =14
- Find the Sum of Odd Nos in List?
public class ReduceEgs { public static void main(String[] args) { List<Integer> arrNumbers = Arrays.asList(1,2,3); Integer squareSum = arrNumbers.stream().filter(x-> x%2==1).reduce(0, Integer::sum); System.out.println("Output 1 ="+ squareSum); } }
Output
Output 1 =4
- Distinct elements from List?
public class ReduceEgs { public static void main(String[] args) { List<Integer> arrNumbers = Arrays.asList(1,2,3,1,4,2); System.out.println("Output 1"); arrNumbers.stream().distinct().forEach(System.out::println); } }
Output
Output 1 1 2 3 4
- Sort Elements in List?
public static void main(String[] args) { List<Integer> arrNumbers = Arrays.asList(1,2,3,1,4,2); System.out.println("Output 2"); arrNumbers.stream().sorted().forEach(System.out::println); }
Output
Output 2 1 1 2 2 3 4
- Sorting based on Natural Order, Reverse Order and Length of String?
public static void main(String[] args) { List<String> arrSkills = Arrays.asList("Java", "Python", "Ant"); System.out.println("Sorting in Natural Order"); arrSkills.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println); System.out.println("Sorting in Reverse Order"); arrSkills.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println); System.out.println("Sorting based on Length"); arrSkills.stream().sorted(Comparator.comparing(str -> str.length())).forEach(System.out::println); }
Output
Sorting in Natural Order Ant Java Python Sorting in Reverse Order Python Java Ant Sorting based on Length Ant Java Python
- Collect the Elements in the List – Collect Even Nos and Length of String ?
public static void main(String[] args) { List<Integer> arrNumbers = Arrays.asList(1,2,4,5,7,6); List<String> arrNames = Arrays.asList("Java", "Oracle", "Maven", "Ant"); System.out.println("Collect Even Nos in list"); List<Integer> arrEvenNumbers = arrNumbers.stream().filter(x->x%2==0).collect(Collectors.toList()); arrEvenNumbers.stream().forEach(System.out::println); System.out.println("Length of Elements"); List<Integer> arrEvenNumbers2 = arrNames.stream().map(str->str.length()).collect(Collectors.toList()); arrEvenNumbers2.stream().forEach(System.out::println); }
Output
Collect Even Nos in list 2 4 6 Length of Elements 4 6 5 3
- Find the Sum of Nos in List?
- Find the Sum of Nos in List?
- Find the Sum of Nos in List?