public static void main(String... args){
then(bar()); // Compilation Error
}
public static <E extends Exception> E bar() {
return null;
}
public static void then(Throwable actual) { }
public static void then(CharSequence actual) { }
Run Code Online (Sandbox Code Playgroud)
编译结果(来自命令行javac Ambiguous.java)
Ambiguous.java:4: error: reference to then is ambiguous
then(bar());
^
both method then(Throwable) in Ambiguous and method then(CharSequence) in Ambiguous match
1 error
Run Code Online (Sandbox Code Playgroud)
为什么这种方法含糊不清?这段代码在Java 7下成功编译!
将方法栏更改为:
public static <E extends Float> E bar() {
return null;
}
Run Code Online (Sandbox Code Playgroud)
这编译没有任何问题,但在IntelliJ Idea(无法解析方法then(java.lang.FLoat))中报告为错误.
此代码在Java …
我有一个Consumer<T>我想转换成一个Function<T, Void>.
我可以通过使用来实现这一点
public <T> Function<T, Void> consumerToFunction(Consumer<T> consumer)
{
return x -> {
consumer.accept(x);
return null;
};
}
Run Code Online (Sandbox Code Playgroud)
但是我怀疑这样的东西可能已经存在于JDK中,或者可能已经存在于公共库中.
我有一个列表,我需要自定义排序,然后转换为具有其Id与名称映射的地图.
这是我的代码:
Map<Long, String> map = new LinkedHashMap<>();
list.stream().sorted(Comparator.comparing(Building::getName)).forEach(b-> map.put(b.getId(), b.getName()));
Run Code Online (Sandbox Code Playgroud)
我认为这将完成这项工作,但我想知道我是否可以避免LinkedHashMap在这里创建并使用花哨的函数式编程来完成一行中的工作.
我对Java 8上的Guava感到非常满意 - 在向Streams迁移顺序代码时是否有任何性能优势或陷阱?
使用Java 8 lambdas,有效创建新的List<T>给定的List<K>密钥和"a "的"最佳"方法是Map<K,V>什么?在这种情况下,您将获得一些List可能的Map键,并且应该生成一个List<T>where T,这是基于Vmap值类型的某些方面构造的某种类型.
我已经探索了一些并且感到不舒服声称一种方式比另一种方式更好(可能有一个例外 - 参见代码).我将澄清"最佳"作为代码清晰度和运行时效率的组合.这些是我想出来的.我相信有人可以做得更好,这是这个问题的一个方面.我不喜欢filter大多数方面,因为它意味着需要创建中间结构和多次传递名称List.现在,我选择了例6 - 一个简单的'ol循环.(注意:代码注释中有一些神秘的想法,特别是"需要外部引用......"这意味着从lambda外部.)
public class Java8Mapping {
private final Map<String,Wongo> nameToWongoMap = new HashMap<>();
public Java8Mapping(){
List<String> names = Arrays.asList("abbey","normal","hans","delbrook");
List<String> types = Arrays.asList("crazy","boring","shocking","dead");
for(int i=0; i<names.size(); i++){
nameToWongoMap.put(names.get(i),new Wongo(names.get(i),types.get(i)));
}
}
public static void main(String[] args) {
System.out.println("in main");
Java8Mapping j = new Java8Mapping();
List<String> testNames = Arrays.asList("abbey", "froderick","igor");
System.out.println(j.getBongosExample1(testNames).stream().map(Bongo::toString).collect(Collectors.joining(", "))); …Run Code Online (Sandbox Code Playgroud) 我使用的是Java 8.我有工具栏和按钮.
我想实现以下内容:
怎么做通过CSS?
在Java 8中引入Locale.lookup(),基于RFC 4647,允许用户Locale根据优先级列表找到最佳匹配列表LocaleRange.现在我不明白这种方法的每个角落情况.以下公开了一个我想要解释的具体案例:
// Create a collection of Locale objects to search
Collection<Locale> locales = new ArrayList<>();
locales.add(Locale.forLanguageTag("en-GB"));
locales.add(Locale.forLanguageTag("en"));
// Express the user's preferences with a Language Priority List
String ranges = "en-US;q=1.0,en-GB;q=1.0";
List<Locale.LanguageRange> languageRanges = Locale.LanguageRange.parse(ranges);
// Find the BEST match, and return just one result
Locale result = Locale.lookup(languageRanges,locales);
System.out.println(result.toString());
Run Code Online (Sandbox Code Playgroud)
这打印en,我会直观地预期en-GB.
注意:
"en-GB;q=1.0,en-US;q=1.0"(GB和US反转),这将打印en-GB,"en-US;q=0.9,en-GB;q=1.0"(GB的优先级高于美国),这将打印出来en-GB.有人可以解释这种行为背后的理由吗?
我希望将毫秒截断到几天,我可以使用
Instant.now().truncatedTo(ChronoUnit.DAYS).toEpochMilli()
Run Code Online (Sandbox Code Playgroud)
但我无法截断ChronoUnit.MONTH(它抛出异常).我需要使用日历吗?
我想创建一个可以运行方法的类,直到满足返回值的条件.
看起来应该是这样的
methodPoller.poll(pollDurationSec, pollIntervalMillis)
.method(dog.bark())
.until(dog -> dog.bark().equals("Woof"))
.execute();
Run Code Online (Sandbox Code Playgroud)
我的方法poller看起来有点像这个()//跟随GuiSim回答
public class MethodPoller {
Duration pollDurationSec;
int pollIntervalMillis;
public MethodPoller() {
}
public MethodPoller poll(Duration pollDurationSec, int pollIntervalMillis) {
this.pollDurationSec = pollDurationSec;
this.pollIntervalMillis = pollIntervalMillis;
return this;
}
public <T> MethodPoller method(Supplier<T> supplier) {
return this;
}
public <T> MethodPoller until(Predicate<T> predicate) {
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
但是我很难从这里开始.
在满足条件之前,如何实现对常规方法的重试?
谢谢.
以下代码是否需要包含在try-with-resources中以确保底层文件已关闭?
List<String> rows = Files.lines(inputFilePath).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)