标签: java-8

Java 8 标识符/关键字列表

我最近一直在查看 Java 8 文档,并惊讶地看到这么多新标识符(例如消费者)。有人可以给我一份这些标识符的列表以及它们的含义吗?谢谢一堆!

java java-8

-4
推荐指数
1
解决办法
1907
查看次数

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)

java sorting comparator java-8

-4
推荐指数
1
解决办法
953
查看次数

Python不像字数统计那样方便

我只是在研究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)

python java-8

-4
推荐指数
1
解决办法
111
查看次数

Java 8映射列表按键映射列表

我有以下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和流?

java java-8 java-stream

-4
推荐指数
1
解决办法
2871
查看次数

使用stream和parallelStream减少相同数据数组时的结果不同?

我在具有相同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)

java java-8

-4
推荐指数
1
解决办法
291
查看次数

Java 8流 - 每个映射键的数字和列表

是否有一种简单/优雅的方法可以在列表中为地图中的每个键求和数字,例如Map<String, List<BigDecimal>>我希望得到的Map<String, BigDecimal>?我找不到/想出来......

java reduce java-8 java-stream

-4
推荐指数
1
解决办法
699
查看次数

对于JAVA中具有Lambda表达式的循环

为什么我使用下面的代码 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)

java foreach lambda java-8

-5
推荐指数
3
解决办法
3480
查看次数

Java 8 --- Lambda Expression

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

java lambda expression java-8

-5
推荐指数
1
解决办法
189
查看次数

如何在java中将空字符串转换为LocalDate?

我需要将空字符串转换为 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)

有人可以帮我吗。

java exception java-8

-5
推荐指数
1
解决办法
2万
查看次数

Why stream().map() can accept parameters like map(Student::getName)

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 java-8 java-stream

-5
推荐指数
1
解决办法
67
查看次数