Sunday, August 20, 2017

Java stream: Logging before filter

While iterating in Java Streams, we may need to log all the items processed by the stream. Especially when using filter in the stream. We can use Peek api for achieving the same.

The following ready to run code will help try out.

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LogStream {
    public static void main(String[] args) {
        System.out.println("Log");
        List result = data().stream()
                .peek(e -> System.out.println(e.getSalary()))
                .filter(e -> e.getSalary()
                        .doubleValue() < 2)
                .collect(Collectors.toList());
        System.out.println("Result");
        result.forEach(e -> System.out.println(e.getSalary()));
    }
    
    static class Emp {
        public Emp(String name, String dept, BigDecimal salary) {
            super();
            this.name = name;
            this.dept = dept;
            this.salary = salary;
        }
        
        private String name;
        private String dept;
        private BigDecimal salary;
        
        public String getName() {
            return name;
        }
        
        public String getDept() {
            return dept;
        }
        
        public BigDecimal getSalary() {
            return salary;
        }
    }
    
    private static List data() {
        Emp e1 = new Emp("e1", "dept1", BigDecimal.valueOf(1.1));
        Emp e2 = new Emp("e2", "dept1", BigDecimal.valueOf(1.1));
        Emp e3 = new Emp("e3", "dept2", BigDecimal.valueOf(2.1));
        Emp e4 = new Emp("e4", "dept2", BigDecimal.valueOf(1.9));
        return Arrays.asList(e1, e2, e3, e4);
    }
    
}


Monday, February 20, 2017

Java 8 stream - GroupingBy sum BigDecimal

Java 8 stream - GroupingBy and summing a BigDecimal field

 import java.math.BigDecimal;  
 import java.util.Arrays;  
 import java.util.List;  
 import java.util.Map;  
 import java.util.stream.Collectors;  
 public class C1 {  
      public static void main(String[] arg){  
           Map<String, BigDecimal> result = data().stream()
               .collect(Collectors.groupingBy(Emp::getDept, 
                        Collectors.mapping(Emp::getSalary, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));  
           System.out.println(result);  
      }  
      static class Emp{  
           public Emp(String name, String dept, BigDecimal salary) {  
                super();  
                this.name = name;  
                this.dept = dept;  
                this.salary = salary;  
           }  
           private String name;  
           private String dept;  
           private BigDecimal salary;  
           public String getName() {  
                return name;  
           }  
           public String getDept() {  
                return dept;  
           }  
           public BigDecimal getSalary() {  
                return salary;  
           }  
      }  
      private static List<Emp> data(){  
           Emp e1 = new Emp("e1", "dept1", BigDecimal.valueOf(1.1));  
           Emp e2 = new Emp("e2", "dept1", BigDecimal.valueOf(1.1));  
           Emp e3 = new Emp("e3", "dept2", BigDecimal.valueOf(2.1));  
           Emp e4 = new Emp("e4", "dept2", BigDecimal.valueOf(1.9));  
           return Arrays.asList(e1, e2, e3, e4);  
      }  
 }