Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这段代码正在编译吗?我不知道';我不这么认为_Java_Java 14_Java Record - Fatal编程技术网

Java 这段代码正在编译吗?我不知道';我不这么认为

Java 这段代码正在编译吗?我不知道';我不这么认为,java,java-14,java-record,Java,Java 14,Java Record,周末我读了一些关于Java14预览功能记录的文章。我不想问这个问题,因为这似乎是Brian Goetz的一段代码,我们都知道这家伙是谁,以及他代表了Java生态系统,但这一直在我脑海中萦绕,我知道这对我来说是一种学习 链接在这里 是这样的 record PlayerScore(Player player, Score score) { // convenience constructor for use by Stream::map PlayerScore(Player play

周末我读了一些关于Java14预览功能记录的文章。我不想问这个问题,因为这似乎是Brian Goetz的一段代码,我们都知道这家伙是谁,以及他代表了Java生态系统,但这一直在我脑海中萦绕,我知道这对我来说是一种学习

链接在这里

是这样的

record PlayerScore(Player player, Score score) {
    // convenience constructor for use by Stream::map
    PlayerScore(Player player) { this(player, getScore(player)); }
}

List<Player> topN
    = players.stream()
             .map(PlayerScore::new)
             .sorted(Comparator.comparingInt(PlayerScore::score))
             .limit(N)
             .map(PlayerScore::player)
             .collect(toList());
public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
也许在我理解它试图做什么之前,你已经看到了它,但有些事情我不明白。也许我错了

这条线

.sorted(Comparator.comparingInt(PlayerScore::score))
comparingit
的API如下所示

record PlayerScore(Player player, Score score) {
    // convenience constructor for use by Stream::map
    PlayerScore(Player player) { this(player, getScore(player)); }
}

List<Player> topN
    = players.stream()
             .map(PlayerScore::new)
             .sorted(Comparator.comparingInt(PlayerScore::score))
             .limit(N)
             .map(PlayerScore::player)
             .collect(toList());
public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
从记录元组返回分数引用是否正确?不是整数或结果不是整数

或者这会使代码编译,我认为这可能是一个键入错误

record PlayerScore(Player player, int score) {
    // convenience constructor for use by Stream::map
    PlayerScore(Player player) { this(player, getScore(player)); }
}

据我所知,正如我之前所说,这段代码不会编译;可能我错了。

这可能无法编译的原因可能是因为
Score
是一种类型,而不是
整数值。要比较
record PlayerScore
得分,您需要做的是确保两件事-

  • 使用比较器。在

    List<Player> topN  = players.stream()
         .map(PlayerScore::new)
         .sorted(Comparator.comparing(PlayerScore::score)) //here
         .limit(N)
         .map(PlayerScore::player)
         .collect(toList());
    

  • 遗憾的是,我在链接文档中也没有看到您的
    Score
    类的实现,这对于理解作者(或编辑)到底错过了什么至关重要。例如,一个简单的
    记录
    定义更改将使现有代码正常工作:

    record PlayerScore(Player player, Integer score) { 
        // convenience constructor for use by Stream::map
        PlayerScore(Player player) {
            this(player, getScore(player));
        }
    }
    
    List<Player> topN = players.stream()
            .map(PlayerScore::new)
            .sorted(Comparator.comparingInt(PlayerScore::score))
            .limit(N)
            .map(PlayerScore::player)
            .collect(Collectors.toList());
    

    这可能无法编译的原因可能是因为
    Score
    是一种类型,而不是
    整数
    值。要比较
    record PlayerScore
    得分,您需要做的是确保两件事-

  • 使用比较器。在

    List<Player> topN  = players.stream()
         .map(PlayerScore::new)
         .sorted(Comparator.comparing(PlayerScore::score)) //here
         .limit(N)
         .map(PlayerScore::player)
         .collect(toList());
    

  • 遗憾的是,我在链接文档中也没有看到您的
    Score
    类的实现,这对于理解作者(或编辑)到底错过了什么至关重要。例如,一个简单的
    记录
    定义更改将使现有代码正常工作:

    record PlayerScore(Player player, Integer score) { 
        // convenience constructor for use by Stream::map
        PlayerScore(Player player) {
            this(player, getScore(player));
        }
    }
    
    List<Player> topN = players.stream()
            .map(PlayerScore::new)
            .sorted(Comparator.comparingInt(PlayerScore::score))
            .limit(N)
            .map(PlayerScore::player)
            .collect(Collectors.toList());
    

    目前,我很难理解你的问题。如果您创建一个我们可以自己复制/粘贴并运行的应用程序,这将有所帮助。您这里的代码片段不是给定的可执行代码。是的,我理解。我的意思是这一行不会编译。排序(Comparator.comparingit(PlayerScore::score))我的猜测:在文章的某些草稿中,
    PlayerScore.score
    被定义为
    int
    类型,而不是
    score
    ,当它被更改时,比较行被忽略了。我认为同样的事情。据我所知,记录为每个字段创建一个与bean兼容的get方法。因此,
    Player::score
    是一个错误,因为没有
    score()
    方法。但是,有一个生成的
    getScore()
    方法,因此您可能需要
    comparingit(Player::getScore)
    。目前,我很难理解您的要求。如果您创建一个我们可以自己复制/粘贴并运行的应用程序,这将有所帮助。您这里的代码片段不是给定的可执行代码。是的,我理解。我的意思是这一行不会编译。排序(Comparator.comparingit(PlayerScore::score))我的猜测:在文章的某些草稿中,
    PlayerScore.score
    被定义为
    int
    类型,而不是
    score
    ,当它被更改时,比较行被忽略了。我认为同样的事情。据我所知,记录为每个字段创建一个与bean兼容的get方法。因此,
    Player::score
    是一个错误,因为没有
    score()
    方法。但是,有一个生成的
    getScore()
    方法,因此您可能需要
    comparingit(Player::getScore)