标签: functional-interface

我应该使用什么中间方法在流调用中的某个点执行某些操作?

我在服务中有一个方法可以从数据库中检索课程对象。在将其发送到控制器之前,我需要解压缩字段 byte[] 徽标。Logo 是一张图片,需要在渲染到前端之前从数据库中解压出来。我试图用流来做到这一点,但没有任何成功。map() 方法在 forEach() 方法之后不起作用。

public List<CourseDto> getCoureses() {

    List<Courses> courses = courseRepositoryDao.findAllByIsCourseFreeAndIsCourseActive(true, true);
    List<CourseDto> coursesNameDto = courses
            .stream()
            .forEach(course-> course.setLogo(decompressZLib(course.getLogo()))
            .map(course -> modelMapper.map(CourseMapper.toUserDtoFreeCourses(course), CourseDto.class))
            .collect(Collectors.toList());

    return coursesNameDto;
}
Run Code Online (Sandbox Code Playgroud)

java lambda functional-programming java-stream functional-interface

1
推荐指数
1
解决办法
66
查看次数

我们如何称呼存储功能接口定义并通过 lambda 定义的对象?

我们应该如何称呼存储该功能接口的实现的对象呢?将其称为匿名类是否正确?

功能接口定义

@FunctionalInterface
interface IntBinaryOperator {
  int applyAsInt(int left, int right);
} 
Run Code Online (Sandbox Code Playgroud)

用于实现功能接口的测试类

public class Lambdas {
  public static void main(String[] args) {
    IntBinaryOperator multiply = (a, b) -> a * b; // what is this object called?
    System.out.println(multiply.applyAsInt(2,3));
  }
}
Run Code Online (Sandbox Code Playgroud)

java lambda functional-programming functional-interface

1
推荐指数
1
解决办法
61
查看次数

为什么我得到的是默认方法的值而不是覆盖的方法的值?

interface MyInterface {
    default int someMethod() {
        return 0;
    }

    int anotherMethod();
}

class Test implements MyInterface {
    public static void main(String[] args) {
        Test q = new Test();
        q.run();
    }

    @Override
    public int anotherMethod() {
        return 1;
    }
    
    void run() {
        MyInterface a = () -> someMethod();
        System.out.println(a.anotherMethod());
    }
}
Run Code Online (Sandbox Code Playgroud)

执行结果将为0,虽然我期望的是1。我不明白为什么不返回重写方法的结果,而是返回默认方法的结果。

java lambda functional-interface

1
推荐指数
1
解决办法
73
查看次数

Java功能接口

我是Java8的新手,我在互联网上阅读了关于这个主题的一些内容.目前我正试图弄清楚功能接口是什么.我找到了一些例子,但我不明白为什么界面Skip是一个功能性的,因为它有2个定义的方法.我希望有人可以解释一下.代码是:

 @FunctionalInterface
 public interface Sprint 
 {
     public void sprint(Animal animal);
 }


 @FunctionalInterface
 public interface Skip extends Sprint 
 {

      public default int getHopCount() 
      {
         return 10;
      }

      public static void skip(int speed) {}
 }
Run Code Online (Sandbox Code Playgroud)

java functional-interface

0
推荐指数
1
解决办法
440
查看次数

java编译器将Function <Double,Integer>转换为DoubleToIntFunction吗?

Function<Double,Integer> f1=(d)-> {
        if(d>5)
            return 0;
        return 1;
    };
DoubleToIntFunction f2=(d)-> {
        if(d>5)
            return 1;
        return 0;
    };
double d=5.0;
f1.apply(d);
f2.applyAsInt(d);
Run Code Online (Sandbox Code Playgroud)

将f1优化为DoubleToIntFunction(有些像f2).

java java-8 functional-interface javacompiler

0
推荐指数
1
解决办法
106
查看次数

寻求对Function &lt;T,R&gt;的深入理解

下面显示的是一些使用Java Streams的示例代码。我的问题专门涉及Interface Function<T,R>接受type的输入T并返回type的东西R

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.groupingBy;

import java.util.List;
import java.util.Map;

public class Dish {

  private final String name;
  private final boolean vegetarian;
  private final String calories;

  public Dish(String name, boolean vegetarian, String calories) {
    this.name = name;
    this.vegetarian = vegetarian;
    this.calories = calories;
  }

  public String getName() {
    return name;
  }

  public boolean isVegetarian() {
    return vegetarian;
  }

  public String getCalories() {
    return calories;
  }

  @Override
  public String toString() {
    return …
Run Code Online (Sandbox Code Playgroud)

java functional-java functional-interface

0
推荐指数
1
解决办法
53
查看次数

在 Java 中,为什么 Function.identity() 是静态方法而不是其他方法?

Java 8 添加了函数式编程结构,包括Function类及其关联identity()方法。

这是此方法的当前结构:

// Current implementation of this function in the [JDK source][1]
static <T> Function<T, T> identity() {
    return t -> t;
}

// Can be used like this
List<T> sameList = list.stream().map(Function.identity()).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

但是,还有第二种构造它的方法:

// Alternative implementation of the method
static <T> T identity(T in) {
    return in;
}

// Can be used like this
List<T> sameList = list.stream().map(Function::identity).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

甚至还有第三种方式来构建它:

// Third implementation
static final Function<T, T> IDENTITY_FUNCTION = t -> t;

// Can …
Run Code Online (Sandbox Code Playgroud)

java functional-programming java-8 functional-interface

0
推荐指数
1
解决办法
423
查看次数

为什么 java 8 IntSupplier 方法名为 getAsInt() 而不是 get()?

有什么具体原因导致它不能像Supplier接口那样使用get()吗?

显然,相同的查询也适用于其他接口(DoubleSupplier、BooleanSupplier 等)。

java functional-interface

0
推荐指数
1
解决办法
306
查看次数

-1
推荐指数
1
解决办法
764
查看次数

为什么我们不能将lambda表达式直接赋给Object类型的引用变量?

我正在做一些lambda表达式的实验,看到了一些我无法理解的行为.

    Consumer consumer = (o1) -> {};
    Object obj1 = consumer; // this two line working fine 
Run Code Online (Sandbox Code Playgroud)

2行以上的代码时,我分配预期不抱怨任何事情consumerobj1.

但是,当我尝试将lambda直接分配给对象时,它开始给我一个编译错误.

    Object obj2 = (o1) -> {}; // this line gives compilation error
Run Code Online (Sandbox Code Playgroud)

上面的代码行给出了一个错误:

此表达式的目标类型必须是功能接口.

我的问题是为什么我们不能直接将lambda分配给类型的引用变量Object

编辑:我已经编辑了我的问题,因为有一个类似的问题已经提到但我的问题主要目标是确保为什么Object o1 = "Hello"会工作但不是lambda.

java lambda java-8 functional-interface

-1
推荐指数
1
解决办法
462
查看次数