user2761895
user2761895

Reputation: 1541

BiConsumer or Consumer in Java8

I'm trying to understand/learn Consumer/BiConsumer in Java8. test1 and test2 methods are working fine.

But if I tried to use the old fashion way by implementing BiConsumer in a class in test3 method. And then override accept method in the class, str.substring method cannot resolve the method substring.

Can't I use the old fashion way in @FunctionalInterface or did I do something wrong in the code?

public class BiConsumerTest {
  static void test1(String name, Integer count) {
    method1(name, count, (str, i) -> {
      System.out.println(str.substring(i));
    });
  }

  static void test2(String name, Integer count) {
    BiConsumer<String, Integer> consumer = (str, i) -> {
      System.out.println(str.substring(i));
    };

    method1(name, count, consumer);
  }

  private static void method1(String name, Integer count, BiConsumer<String, Integer> consumer) {
    consumer.accept(name, count);
  }

  private void test3(String name, Integer count) {
    BiConsumer<String, Integer>  consumer = new ConsumerImpl<String, Integer>();
    consumer.accept(name, count);
  }

  class ConsumerImpl<String, Integer> implements BiConsumer<String, Integer> {
    @Override
    public void accept(String str, Integer count) {
      str.substring(count);     // str cannot find substring method !!!
    }
  }

  public static void main(String[] args) {
    String name = "aaa bbb ccc";
    Integer count = 6;

    test1(name, count);
    test2(name, count);
  }
}

Upvotes: 0

Views: 2419

Answers (1)

Ashutosh A
Ashutosh A

Reputation: 1025

You cannot define a class with known types as type-parameters (so this is incorrect - ConsumerImpl<String, Integer>). Plus there were few other syntactical mistakes. Below works -

import java.util.function.BiConsumer;

public class TestClass {

    static void test1(String name, Integer count) {
        method1(name, count, (str, i) -> {
            System.out.println(str.substring(i));
        });
    }

    static void test2(String name, Integer count) {
        BiConsumer<String, Integer> consumer = (str, i) -> {
            System.out.println(str.substring(i));
        };

        method1(name, count, consumer);
    }

    private static void method1(String name, Integer count, BiConsumer<String, Integer> consumer) {
        consumer.accept(name, count);
    }

    static void test3(String name, Integer count) {
        BiConsumer<String, Integer> consumer = new ConsumerImpl();
        consumer.accept(name, count);
    }

    static class ConsumerImpl implements BiConsumer<String, Integer> {
        @Override
        public void accept(String str, Integer count) {
            System.out.println(str.substring(count));     // str cannot find substring method !!!
        }
    }

    public static void main(String[] args) {
        String name = "aaa bbb ccc";
        Integer count = 6;

        test1(name, count);
        test2(name, count);
        test3(name, count);
    }
}

Upvotes: 1

Related Questions