我在服务中有一个方法可以从数据库中检索课程对象。在将其发送到控制器之前,我需要解压缩字段 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
我们应该如何称呼存储该功能接口的实现的对象呢?将其称为匿名类是否正确?
功能接口定义
@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) 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。我不明白为什么不返回重写方法的结果,而是返回默认方法的结果。
我是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) 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 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 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) 有什么具体原因导致它不能像Supplier接口那样使用get()吗?
显然,相同的查询也适用于其他接口(DoubleSupplier、BooleanSupplier 等)。
我正在做一些lambda表达式的实验,看到了一些我无法理解的行为.
Consumer consumer = (o1) -> {};
Object obj1 = consumer; // this two line working fine
Run Code Online (Sandbox Code Playgroud)
2行以上的代码时,我分配预期不抱怨任何事情consumer来 obj1.
但是,当我尝试将lambda直接分配给对象时,它开始给我一个编译错误.
Object obj2 = (o1) -> {}; // this line gives compilation error
Run Code Online (Sandbox Code Playgroud)
上面的代码行给出了一个错误:
此表达式的目标类型必须是功能接口.
我的问题是为什么我们不能直接将lambda分配给类型的引用变量Object?
编辑:我已经编辑了我的问题,因为有一个类似的问题已经提到但我的问题主要目标是确保为什么Object o1 = "Hello"会工作但不是lambda.