fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.List;
  7. import java.util.function.Function;
  8.  
  9. class Ideone {
  10. public static void main(String[] args) {
  11. MyRow row1 = new MyRow(BigDecimal.ONE);
  12. MyRow row2 = new MyRow(BigDecimal.TEN);
  13. List<MyRow> rows = Arrays.asList(row1, row2);
  14.  
  15. rows.stream().map(x -> x.getValue()).reduce(BigDecimal.ZERO, BigDecimal::add);
  16. //calcSum(rows, (MyRow x) -> x.getValue());
  17. }
  18.  
  19. public static BigDecimal calcSum(Collection c , Function<?, BigDecimal> mapFunc) {
  20. return c.stream().map(mapFunc).reduce(BigDecimal.ZERO, BigDecimal::add);
  21. }
  22.  
  23. }
  24.  
  25. class MyRow {
  26. private BigDecimal value;
  27.  
  28. public MyRow(BigDecimal value) {
  29. this.value = value;
  30. }
  31.  
  32. public BigDecimal getValue() {
  33. return value;
  34. }
  35.  
  36. public void setValue(BigDecimal value) {
  37. this.value = value;
  38. }
  39. }
Compilation error #stdin compilation error #stdout 0.16s 2184192KB
stdin
Standard input is empty
compilation info
Main.java:20: error: incompatible types: invalid method reference
        return c.stream().map(mapFunc).reduce(BigDecimal.ZERO, BigDecimal::add);
                                                               ^
    no suitable method found for add(Object,Object)
        method BigDecimal.add(BigDecimal,MathContext) is not applicable
          (argument mismatch; Object cannot be converted to BigDecimal)
        method BigDecimal.add(long,long) is not applicable
          (argument mismatch; Object cannot be converted to long)
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
stdout
Standard output is empty