什么是一个方法的Java 8功能接口什么都不带,什么都不返回?
即,相当于Action带void返回类型的C#参数?
我看到java.util.function.BiFunction,所以我可以这样做:
BiFunction<Integer, Integer, Integer> f = (x, y) -> { return 0; };
Run Code Online (Sandbox Code Playgroud)
如果那还不够好我需要TriFunction怎么办?它不存在!
TriFunction<Integer, Integer, Integer, Integer> f = (x, y, z) -> { return 0; };
Run Code Online (Sandbox Code Playgroud)
我想我应该补充一点,我知道我可以定义自己的TriFunction,我只是想了解不在标准库中包含它的原理.
我非常想使用Map.computeIfAbsent,但是因为lambdas在本科课程中已经太长了.
几乎直接来自文档:它给出了一个旧方法的例子:
Map<String, Boolean> whoLetDogsOut = new ConcurrentHashMap<>();
String key = "snoop";
if (whoLetDogsOut.get(key) == null) {
Boolean isLetOut = tryToLetOut(key);
if (isLetOut != null)
map.putIfAbsent(key, isLetOut);
}
Run Code Online (Sandbox Code Playgroud)
而新的方式:
map.computeIfAbsent(key, k -> new Value(f(k)));
Run Code Online (Sandbox Code Playgroud)
但在他们的例子中,我认为我并没有"得到它".我如何转换代码以使用新的lambda表达方式?
如何获得与流中的条件匹配的第一个元素?我试过这个但是没用
this.stops.stream().filter(Stop s-> s.getStation().getName().equals(name));
Run Code Online (Sandbox Code Playgroud)
该条件不起作用,filter方法在Stop之外的其他类中调用.
public class Train {
private final String name;
private final SortedSet<Stop> stops;
public Train(String name) {
this.name = name;
this.stops = new TreeSet<Stop>();
}
public void addStop(Stop stop) {
this.stops.add(stop);
}
public Stop getFirstStation() {
return this.getStops().first();
}
public Stop getLastStation() {
return this.getStops().last();
}
public SortedSet<Stop> getStops() {
return stops;
}
public SortedSet<Stop> getStopsAfter(String name) {
// return this.stops.subSet(, toElement);
return null;
}
}
import java.util.ArrayList;
import java.util.List;
public class Station {
private final …Run Code Online (Sandbox Code Playgroud) 我在Spring Boot Application中格式化Java 8 LocalDateTime时遇到了一个小问题.使用"正常"日期我没有问题,但LocalDateTime字段转换为以下内容:
"startDate" : {
"year" : 2010,
"month" : "JANUARY",
"dayOfMonth" : 1,
"dayOfWeek" : "FRIDAY",
"dayOfYear" : 1,
"monthValue" : 1,
"hour" : 2,
"minute" : 2,
"second" : 0,
"nano" : 0,
"chronology" : {
"id" : "ISO",
"calendarType" : "iso8601"
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我想将其转换为:
"startDate": "2015-01-01"
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
@JsonFormat(pattern="yyyy-MM-dd")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
public LocalDateTime getStartDate() {
return startDate;
}
Run Code Online (Sandbox Code Playgroud)
但是上述任何一个注释都不起作用,日期会像上面那样被格式化.建议欢迎!
我有下面的代码,对于多次空检查有点难看.
String s = null;
if (str1 != null) {
s = str1;
} else if (str2 != null) {
s = str2;
} else if (str3 != null) {
s = str3;
} else {
s = str4;
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试使用Optional.ofNullable如下所示,但如果有人读取我的代码仍然很难理解.在Java 8中这样做的最佳方法是什么.
String s = Optional.ofNullable(str1)
.orElse(Optional.ofNullable(str2)
.orElse(Optional.ofNullable(str3)
.orElse(str4)));
Run Code Online (Sandbox Code Playgroud)
在Java中9,我们可以使用Optional.ofNullable同OR,但在Java8还有没有其他办法?
我在Java中遇到Date类问题.Date类返回本地机器日期,但我需要UTC-0.
我用Google搜索并找到了很好的JavaScript解决方案,但对Java来说没用.
如何在Java 8中获得UTC + 0日期?
当我注意到你现在可以在界面中定义静态和默认方法时,我正在通过接口学习.
public interface interfacesample2 {
public static void method() {
System.out.println("hello world");
}
public default void menthod3() {
System.out.println("default print");
}
}
Run Code Online (Sandbox Code Playgroud)
请解释两者的区别,如果有一个例子,我们什么时候使用它会很好.在接口上有点困惑.
鉴于如下的流{ 0, 1, 2, 3, 4 },
我怎样才能最优雅地将其转换为给定的形式:
{ new Pair(0, 1), new Pair(1, 2), new Pair(2, 3), new Pair(3, 4) }
(假设,当然,我已经定义了类对)?
编辑:这不是严格关于整数或原始流.对于任何类型的流,答案应该是通用的.
在初始化类之前,必须初始化其直接超类,但不会初始化类实现的接口.同样,在初始化接口之前,不会初始化接口的超接口.
为了我自己的好奇心,我尝试了它,正如预期的那样,界面InterfaceType没有被初始化.
public class Example {
public static void main(String[] args) throws Exception {
InterfaceType foo = new InterfaceTypeImpl();
foo.method();
}
}
class InterfaceTypeImpl implements InterfaceType {
@Override
public void method() {
System.out.println("implemented method");
}
}
class ClassInitializer {
static {
System.out.println("static initializer");
}
}
interface InterfaceType {
public static final ClassInitializer init = new ClassInitializer();
public void method();
}
Run Code Online (Sandbox Code Playgroud)
这个程序打印
implemented method
Run Code Online (Sandbox Code Playgroud)
但是,如果接口声明了default方法,则会发生初始化.考虑InterfaceType给出的接口为
interface InterfaceType {
public …Run Code Online (Sandbox Code Playgroud) java ×10
java-8 ×10
java-stream ×4
lambda ×3
java-time ×2
datetime ×1
dictionary ×1
interface ×1
json ×1
optional ×1
spring-boot ×1