创建一个通用方法来查找对象列表中某个特定属性的最大值 [英] Create a generic method to find largest value of a certain property in a list of objects

查看:432
本文介绍了创建一个通用方法来查找对象列表中某个特定属性的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Groovy / Java创建一个小项目来跟踪足球统计数据。我有一个迭代播放器对象列表的方法,并找到谁获得了最多的目标并保留了最干净的表单。目前这些方法使用相同的代码,我试图做出一个通用的方法,但无法看到如何做到这一点,因为我想要找到最大值的每个属性都是不同的,例如目标和干净的表单。任何人都可以给我一些指针,我是新来的泛型,我试图传入一个通用的对象,但然后无法解决如何然后定位该对象上的特定属性来计算循环中的最高值。



这是我的代码



https://gist.github.com/anonymous/0751dc3eae8f264e12ea4564f0e71dd2

  @Override 
Promise< Map< String,Double>> findPlayerWithMostMotms(List< Player> players){
Map< String,Integer> mostManOfTheMatches = [:]
Integer currentlyMostManOfTheMatches = 0
returnPlayerWithHighestAverageRating(
players,mostManOfTheMatches,currentlyMostManOfTheMatches

return Promise.value(mostManOfTheMatches)
}

私人
静态列表< Player> returnPlayerWithHighestAverageRating(List< Player> players,
Map< String,Integer> mostManOfTheMatches,Integer currentlyMostManOfTheMatches){
players.each {
if(it.manOfTheMatches> currentlyMostManOfTheMatches){
mostManOfTheMatches.clear()
mostManOfTheMatches.put(it.name,it.manOfTheMatches)
}
}
}

@Override
Promise< Map< String,Integer>> findMostCleanSheets(List< Player> players){
Map< String,Integer> mostCleanSheets = [:]
Integer CurrentHighestValue = 0
returnPlayerWithMostCleanSheets(players,mostCleanSheets,CurrentHighestValue)
return Promise.value(mostCleanSheets)
}

private
静态列表< Player> returnPlayerWithMostCleanSheets(List< Player> players,
Map< String,Integer> mostCleanSheets,Integer CurrentHighestValue){
players.each {
if(it.cleanSheets> CurrentHighestValue){
mostCleanSheets.clear()
mostCleanSheets.put(it.name,it.cleanSheets)
}
}
}


解决方案

你可以看看演示文稿去年在德国斯图加特举行的Java论坛上发表。

只有少数幻灯片使用德语,99%的内容是基于英文的Java源代码;像

  collection .sort(
Comparator
.comparing(Person :: getName)
.thenComparing(Person :: getId)
);

其中比较器是一个示例实现,它执行您正在寻找的任务 - 允许调用选择器方法(比如getName())。



如果你使用java8,你会发现很多材料让你开始。

I am using Groovy/Java to create a little side project to track footballers stats. I have a method that iterates over a list of Player objects and finds who has scored the most goals and kept the most clean sheets. At the moment the methods use the same code, I am trying to make one generic method but can't see how to do this as each property I want to find the maximum of will be different eg goals and clean sheets. Can anyone give me some pointers, I am new to generics, I tried to pass in a generic object but then couldn't work out how to then target a specific property on that object to count the highest value in the loop.

Here is my code

https://gist.github.com/anonymous/0751dc3eae8f264e12ea4564f0e71dd2

@Override
Promise<Map<String, Double>> findPlayerWithMostMotms(List<Player> players) {
    Map<String, Integer> mostManOfTheMatches = [:]
    Integer currentlyMostManOfTheMatches = 0
    returnPlayerWithHighestAverageRating(
        players, mostManOfTheMatches, currentlyMostManOfTheMatches
    )
    return Promise.value(mostManOfTheMatches)
}

private
static List<Player> returnPlayerWithHighestAverageRating(List<Player> players, 
    Map<String, Integer> mostManOfTheMatches, Integer currentlyMostManOfTheMatches) {
    players.each {
        if (it.manOfTheMatches > currentlyMostManOfTheMatches) {
            mostManOfTheMatches.clear()
            mostManOfTheMatches.put(it.name, it.manOfTheMatches)
        }
    }
}

@Override
Promise<Map<String, Integer>> findMostCleanSheets(List<Player> players) {
    Map<String, Integer> mostCleanSheets = [:]
    Integer CurrentHighestValue = 0
    returnPlayerWithMostCleanSheets(players, mostCleanSheets, CurrentHighestValue)
    return Promise.value(mostCleanSheets)
}

private
static List<Player> returnPlayerWithMostCleanSheets(List<Player> players, 
    Map<String, Integer> mostCleanSheets, Integer CurrentHighestValue) {
    players.each {
        if (it.cleanSheets > CurrentHighestValue) {
            mostCleanSheets.clear()
            mostCleanSheets.put(it.name, it.cleanSheets)
        }
    }
}

解决方案

You can have a look into this presentation hold at the Java Forum in Stuttgart Germany last year.

Only a few slides use German language, 99% of the content is "English based" Java source code; like

collection .sort(
  Comparator
    .comparing(Person::getName)
    .thenComparing(Person::getId)
);

where "Comparator" is an example implementation that does what you are looking for - allowing to call a "selector" method (such as getName()) on some "data" class.

If you are into java8, you find a lot of material there to get you started.

这篇关于创建一个通用方法来查找对象列表中某个特定属性的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆