我有一个ABC类,它包含两个整数字段
public class ABC{
private Integer x;
private Integer y;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我有两个列表:xValues和yValues,它们分别包含x和y值的整数列表.
List<Integer> xValues = fetchAllXValues(); //say list xValues contains {1,2,3}
List<Integer> yValues = fetchAllYValues(); //say list yValues contains {7,8,9}
Run Code Online (Sandbox Code Playgroud)
现在我想要的是使用xValues列表的每个值与yValues列表的每个值创建一个ABC对象.我不想使用嵌套for循环.什么是更有效的解决方法?
ABC的示例输出对象是:
ABC(1,7);
ABC(1,8);
ABC(1,9);
ABC(2,7);
ABC(2,8);
ABC(2,9);
ABC(3,7);
ABC(3,8);
ABC(3,9);
Run Code Online (Sandbox Code Playgroud) 如果a是Optional [123],b是Optional [empty].
a.orElse(b.orElseThrow(() -> new UnexpectedInternalException(
"Error")))
Run Code Online (Sandbox Code Playgroud)
它为什么扔?
我在不同的小版本java下从相同的代码片段获得不同的输出.我无法在开放的jdk bug跟踪器上找到相关的票证.
CompletableFuture<String> completableFuture = new CompletableFuture<>();
completableFuture.complete("xxx");
completableFuture.thenCompose(str -> {
CompletableFuture<String> completableFuture1 = new CompletableFuture<>();
completableFuture1.completeExceptionally(new Exception("hello"));
return completableFuture1;
}).exceptionally(ex -> {
System.out.println(ex.getMessage());
return null;
}).get();
Run Code Online (Sandbox Code Playgroud)
JDK 1.8.0_25下的输出:
你好
JDK 1.8.0_102下的输出:
java.lang.Exception:你好
较新的是修复还是回归?什么是相关票?
鉴于:
List<Integer> integers = new ArrayList<>(Arrays.asList(
10, 12
));
List<Optional<Integer>> optionalIntegers = Arrays.asList(
Optional.of(5),
Optional.empty(),
Optional.of(3),
Optional.of(2),
Optional.empty()
);
List<Integer> unwrappedOptionals = optionalIntegers.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
integers.addAll(unwrappedOptionals);
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来展开Optionals,或者将它们合并到一起的其他方式List<Integer>?List在做之前将它们收集到新的中感觉非常浪费addAll().
根据java.time文档,java.time应该能够以纳秒精度呈现LocalDateTime或LocalTime,但是当我运行LocalDateTime.now()并打印出来时,它只显示3位而不是9位.
像这样:
2016-08-11T22:17:35.031
Run Code Online (Sandbox Code Playgroud)
有没有办法获得更高的精度?
使用JAVA8,内部类可以用lambda表达式替换.
Comparator c = (a, b) -> Integer.compare(a.length(), b.length());
Runnable java8Runner = () ->{System.out.println("I am running");};
Run Code Online (Sandbox Code Playgroud)
jvm怎么知道,这个lambda应该覆盖正确的方法?在上面的例子中,它们是run()和compare().
用例:
通过返回ImmutableTable类型的方法处理字符串列表{R,C,V}.例如ImmutableTable of {Integer,String,Boolean} process(String item){...}
收集结果,即合并所有结果并返回ImmutableTable.有没有办法实现它?
目前的实施(波希米亚建议):
如何使用并行流?以下代码中是否存在并发问题?使用并行流我在tableBuilder.build()上得到"索引1800处的NullPointerException",但是对流工作正常.
ImmutableTable<Integer, String, Boolean> buildData() {
// list of 4 AwsS3KeyName
listToProcess.parallelStream()
//Create new instance via Guice dependency injection
.map(s3KeyName -> ProcessorInstanceProvider.get()
.fetchAndBuild(s3KeyName))
.forEach(tableBuilder::putAll);
return tableBuilder.build(); }
Run Code Online (Sandbox Code Playgroud)
虽然下面的代码与流和并行流有关.但是由于row和col的重复输入,ImmutableBuild失败了.什么是在合并表时防止重复的最佳方法?
public static <R, C, V> Collector<ImmutableTable<R, C, V>,
ImmutableTable.Builder<R, C, V>, ImmutableTable<R, C, V>>
toImmutableTable()
{
return Collector.of(ImmutableTable.Builder::new,
ImmutableTable.Builder::putAll, (builder1, builder2) ->
builder1.putAll(builder2.build()), ImmutableTable.Builder::build); }
Run Code Online (Sandbox Code Playgroud)
编辑:如果在合并不同的表时ImmutableTable.Builder中有任何重复的条目,那么它会失败,
试图通过将ImmutableTables放在HashBasedTable中来避免faluire
ImmutableTable.copyOf(itemListToProcess.parallelStream()
.map(itemString ->
ProcessorInstanceProvider.get()
.buildImmutableTable(itemString))
.collect(
Collector.of(
HashBasedTable::create,
HashBasedTable::putAll,
(a, …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码段:
public class JavaApplication4 {
static <T> List<T> functionConcat(List<T> l1, List<T> l2) {
return Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList());
}
// functionConcat in lambda form
static final BinaryOperator<List<? extends Number>> lambdaConcat = (l1, l2)
-> Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList());
public static void main(String[] args) {
// DOES NOT WORK with lambdaConcat
final List<Integer> x = new LinkedList<List<Integer>>()
.stream().reduce(new LinkedList<>(), lambdaConcat);
final List<Double> y = new LinkedList<List<Double>>()
.stream().reduce(new LinkedList<>(), lambdaConcat);
// WORKS with functionConcat
final List<Integer> x2 = new LinkedList<List<Integer>>()
.stream().reduce(new LinkedList<>(), JavaApplication4::functionConcat);
final List<Double> y2 …Run Code Online (Sandbox Code Playgroud) 我有一个方法,如:
private List<Person> findFilteredPersons(List<Person> persons,
Predicate<Person> filter) {
return persons
.stream()
.filter(filter)
.sorted(BY_NAME.thenComparing(BY_AGE))
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
现在我想在几个地方使用这个方法,但在一个地方我不需要过滤列表,我只想对它进行排序.实现这一目标的最佳方法是什么?
假设我有一个SchoolDistrict,有许多Schools,有许多Students有很多Classes.
我想计算给予SchoolDistrict的课程数量.
Java 7的方式是这样的:
Integer classCount = 0;
for (School school : schoolDistrict)
{
for (Student student : school.getStudents())
{
classCount += student.getClasses().size();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道Java 8带来的流应该会使这种事情在眼睛上变得容易一些.
但我无法弄清楚如何去做.
任何接受者?
编辑:有人将此标记为此帖的副本,我不相信这是因为该示例仅深入2级(国家/地区).我的问题涉及一个深入四个层次的问题(SchoolDistricts to Schools to Students to Classes)
java-8 ×10
java ×9
java-stream ×4
guava ×1
java-time ×1
lambda ×1
list ×1
nested-loops ×1
openjdk ×1
optional ×1