我最近一直在查看 Java 8 文档,并惊讶地看到这么多新标识符(例如消费者)。有人可以给我一份这些标识符的列表以及它们的含义吗?谢谢一堆!
虽然我使用下面的比较器来对我正在获得的对象进行排序比较方法违反了比较器中的一般合同问题.
final Set<Span> set = new TreeSet<Span>(new Comparator<Span>() {
public int compare(final Span firstSpan, final Span secSpan) {
BigInteger s1X0 = firstSpan.getCoordinates().getX0();
BigInteger s1X1 = firstSpan.getCoordinates().getX1();
BigInteger s2X0 = secSpan.getCoordinates().getX0();
BigInteger s2X1 = secSpan.getCoordinates().getX1();
BigInteger s1Y0 = firstSpan.getCoordinates().getY0();
final BigInteger s2Y0 = secSpan.getCoordinates().getY0();
if(s1X0.intValue() == s2X0.intValue() && s1X1.intValue() == s2X1.intValue() && s1Y0.intValue() == s2Y0.intValue()){
return 0;
}
if ((s1Y0.intValue() - s2Y0.intValue() <= 5) && (s1Y0.intValue() - s2Y0.intValue() >= -5)) {
return (s1X0.intValue()>s2X0.intValue()) ? 1 : -1;
} else {
if …Run Code Online (Sandbox Code Playgroud) 我只是在研究Python,发现一些地方甚至不如Java8那么方便,例如字数
起初我认为它可能很容易实现
>>> {x : x**2 for x in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
Run Code Online (Sandbox Code Playgroud)
但实际上我发现它有点麻烦
>>> sent3
['In', 'the', 'beginning', 'God', 'created', 'the', 'heaven', 'and', 'the', 'earth', '.']
>>> for w in sent3:
... if w in word_count:
... word_count[w] += 1
... else:
... word_count[w] = 1
...
Run Code Online (Sandbox Code Playgroud)
但是在Java8中实现它非常方便,
List<String> strings = asList("In", "the", "beginning", "God", "created", "the", "heaven", "and", "the", "earth");
Map<String, …Run Code Online (Sandbox Code Playgroud) 我有以下Java代码:
List<BaseQuery> queries;
Map<Long, List<BaseQuery>> map = new HashMap<>();
for (BaseQuery query : queries) {
List<BaseQuery> queryList = map.get(query.getCharacteristicId());
if(queryList == null) {
queryList = new ArrayList<>();
map.put(query.getCharacteristicId(), queryList);
}
queryList.add(query);
}
Run Code Online (Sandbox Code Playgroud)
能否请您展示如何将其转换为Java 8和流?
我在具有相同lambda表达式的相同数组上使用了reduce with stream和parallelStream,我期望相同的结果,但输出是不同的.但是,结果不同,我不知道为什么.
代码:
System.out.println("Reduce me...");
Integer[] arrx = {1,2,3,4};
// Reducing the Array
Arrays
.stream(arrx)
.reduce((s1, s2) -> (int)Math.pow(s1,2) + (int)Math.pow(s2,2))
.ifPresent(System.out::println);
Arrays.asList(arrx)
.parallelStream()
.reduce((s1, s2) -> (int)Math.pow(s1,2) + (int)Math.pow(s2,2))
.ifPresent(System.out::println);
Run Code Online (Sandbox Code Playgroud)
输出:
1172
650
Run Code Online (Sandbox Code Playgroud) 是否有一种简单/优雅的方法可以在列表中为地图中的每个键求和数字,例如Map<String, List<BigDecimal>>我希望得到的Map<String, BigDecimal>?我找不到/想出来......
为什么我使用下面的代码 IndexOutOfBoundsException
码:
List<Integer> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList());
System.out.print("the list: ");
ints.forEach((i) -> {
System.out.print(ints.get(i-1) + " ");
});
Run Code Online (Sandbox Code Playgroud)
我的错误堆栈:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 5
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at Agent.lambda$main$1(Agent.java:33)
at Agent$$Lambda$8/980546781.accept(Unknown Source)
at java.util.ArrayList.forEach(ArrayList.java:1234)
at Agent.main(Agent.java:32)
the list: Java Result: 1
Run Code Online (Sandbox Code Playgroud)
但是当我将列表更改为一位数时,一切都很好
码:
List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());
System.out.print("the list: ");
ints.forEach((i) -> {
System.out.print(ints.get(i-1) + " ");
});
Run Code Online (Sandbox Code Playgroud)
输出:
2 8 7 4 3
Run Code Online (Sandbox Code Playgroud) import java.util.function.Function;
public class LambdaExpression {
@SuppressWarnings("rawtypes")
public static Function create(int addTo){
int n = 1;
Function<Integer, Integer> f = addT -> addTo + n;
return f;
}
public static void main(String[] args){
System.out.println(LambdaExpression.create(5));
}
}
Run Code Online (Sandbox Code Playgroud)
在运行时给我这个错误:
LambdaExpression $$ LAMBDA $424058530分之1@ 1c20c684
我需要将空字符串转换为 LocalDate 对象。我尝试使用
LocalDate.parse("");
Run Code Online (Sandbox Code Playgroud)
但有一个例外说:
java.time.format.DateTimeParseException: Text '' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗。
This is the student class definition.
public class Student {
public Student(String name) {
this.name = name;
}
private String name;
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
The map function should accept like a Function interface
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Run Code Online (Sandbox Code Playgroud)
That means the parameters should be like that
int func(int a){
return b;
}
Run Code Online (Sandbox Code Playgroud)
We need to make sure have one method parameter.
So why getName() can work?
The method actually change to …
java-8 ×10
java ×9
java-stream ×3
lambda ×2
comparator ×1
exception ×1
expression ×1
foreach ×1
python ×1
reduce ×1
sorting ×1