小编Agu*_*uid的帖子

如何在Comparator地图中使用泛型来避免警告

我想创建一个比较器的映射如下,这个映射将用于为每种类提供比较器.

如何更换Generic?在我的地图的声明中,以确保我的地图(比较器)的键和值始终具有相同的类类型?

我也希望减少警告的数量

 private static final Map<Class<?>, Comparator<?>> comparators = new HashMap<>();

static {
    comparators.put(Identifiable.class, new Comparator<Identifiable>() {
        @Override
        public int compare(Identifiable o1, Identifiable o2) {
            return o1.getId().compareTo(o2.getId());
        }
    });
    comparators.put(MyClass.class, new Comparator<MyClass>() {
        @Override
        public int compare(EIntersection o1, EIntersection o2) {
            return o1.getRef().compareTo(o2.getRef());
        }
    });
    ...
}
Run Code Online (Sandbox Code Playgroud)

java generics

6
推荐指数
1
解决办法
85
查看次数

迁移到 java17 问题:模块 java.base 不会向未命名模块“打开 java.io”

当我从 java8 迁移到 java 17 时,我的单元测试失败。以下是我得到的异常示例:

无法使 java.io.OptionalDataException(boolean) 可访问:模块 java.base 不会向未命名模块“打开 java.io”

当我用 google 搜索时,我发现我必须添加“--add-opens java.base/java.io=ALL-UNNAMED”选项作为 JVM 的参数。

这是我的做法:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${surefire.version}</version>
            <configuration>
                <argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
                <argLine>--add-opens java.base/java.util=ALL-UNNAMED</argLine>
                <argLine>--add-opens java.base/java.io=ALL-UNNAMED</argLine>
            </configuration>

        </plugin>
Run Code Online (Sandbox Code Playgroud)

但我仍然遇到同样的问题:(有什么帮助吗?

migration module openjdk-17

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

如何在NetBeans中使用Angular 4?

我使用netbeans作为我的angular 4应用程序的编辑器.我使用angular CLI创建了我的第一个应用程序然后用netbeans 8.2打开了.问题是我得到了一些错误编译(例如:) <input type="text" [(ngModel)] ="name" >.这就是为什么我想知道netbeans是否支持角4?如果是,我在哪里可以找到所需的库?

谢谢

netbeans angular

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

使用try-with-resources语句声明Stream之间的区别是什么?

在Java 8中,Stream(AutoCloseable)无法重用,一旦消耗或使用它,流将被关闭.那么使用try-with-resources语句声明的实用程序是什么?

使用try-with-resources语句的示例:

    public static void main(String[] args) throws IOException {

    try (Stream<Path> entries
            = Files.walk(Paths.get("."), 4, FileVisitOption.FOLLOW_LINKS)) {
        entries.forEach(x -> System.out.println(x.toAbsolutePath()));// the entries stream will be automatically closed at this point
        //..
        System.out.println("Still in the Try Block");
    } //The entries will be closed again because it is declared in the try-with-resources statement
}
Run Code Online (Sandbox Code Playgroud)

这里是没有try catch块的相同示例

public static void main(String[] args) throws IOException {
        Stream<Path> entries = Files.walk(Paths.get("."), 4, FileVisitOption.FOLLOW_LINKS);
        entries.forEach(x -> System.out.println(x.toAbsolutePath()));// the entries stream will be automatically closed …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

3
推荐指数
2
解决办法
787
查看次数

Java8 Stream:为什么Stream.skip(long n)方法需要long而不是int作为参数?

这里是Stream的skip方法的定义:

skip(long n)在丢弃流的前n个元素后,返回由该流的其余元素组成的流.

我的问题是为什么参数long代替int

这是一个例子:

import java.util.Arrays;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<String> stringList = Arrays.asList("1","1","2","3","4");

    stringList.stream()
           .skip(2)
           .forEach(System.out::print);// will prints 234
  }
} 
Run Code Online (Sandbox Code Playgroud)

java java-stream

3
推荐指数
1
解决办法
76
查看次数

使用Python的Spark Redshift

我正在尝试将Spark与亚马逊Redshift连接,但我收到此错误:

在此输入图像描述

我的代码如下:

from pyspark.sql import SQLContext
from pyspark import SparkContext

sc = SparkContext(appName="Connect Spark with Redshift")
sql_context = SQLContext(sc)
sc._jsc.hadoopConfiguration().set("fs.s3n.awsAccessKeyId", <ACCESSID>)
sc._jsc.hadoopConfiguration().set("fs.s3n.awsSecretAccessKey", <ACCESSKEY>)

df = sql_context.read \
    .option("url", "jdbc:redshift://example.coyf2i236wts.eu-central-    1.redshift.amazonaws.com:5439/agcdb?user=user&password=pwd") \
    .option("dbtable", "table_name") \
    .option("tempdir", "bucket") \
    .load()
Run Code Online (Sandbox Code Playgroud)

amazon-redshift apache-spark databricks

2
推荐指数
2
解决办法
8731
查看次数

我们如何在UML 2中绘制内部类的组合?

如何呈现匿名类的关联组成在UML 2中的类本身中定义?

谢谢

uml anonymous-inner-class composition

2
推荐指数
1
解决办法
904
查看次数

Java8:如何用Predicate函数替换按位包含和(&)?

我想使用Predicate函数而不是&运算符.这意味着我想要"第二次测试通过!!" 在执行我的代码后打印.

我该怎么做 ?

public class BitwiseInclusiveAND{

    public static void main(String[] args) {
        final Predicate<String> condition1 = (Predicate<String>) (arg -> arg != null);
        final Predicate<String> condition2 = (Predicate<String>) (arg -> {
            System.out.println("Second Test is passed !!");
            return arg.equals("Hello");
        });
        Predicate<String> equalsStrings
                = condition1.and(condition2); // Here I want to execute the condition2 even if condition 1 = true 
        System.out.println(equalsStrings.test("Hello"));
    }
}
Run Code Online (Sandbox Code Playgroud)

java function java-8

2
推荐指数
1
解决办法
80
查看次数