首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统计List<Integer>中出现的次数

统计List<Integer>中出现的次数
EN

Stack Overflow用户
提问于 2013-11-21 04:40:02
回答 3查看 480关注 0票数 1

我正在创建一个计算学生成绩的小应用程序。它在一个方法中需要两个List<Integer>

如果我的list包含8个项目,例如;{52,62,65,65,72,72,75,75},我想要找到一种方法,通过该list计数,并找到相同的第十个项目(例如,30,40,50,60,70)。因此,在这种情况下,4个项目将在70年代内,然后能够存储它(称为上限)。这样做的目的是,如果4个项目在上限内,则总体学生成绩会更高。

我的问题是,我如何才能做到这一点?我已经看到我可以使用某种map,但我不确定这是否是实现它的最佳方式。

EN

回答 3

Stack Overflow用户

发布于 2013-11-21 04:49:06

遍历列表并将条目放入映射中,其中key是一个数字,value是一个计数。

在前面的答案a的基础上,增加了一点伪代码...

代码语言:javascript
复制
Map<Integer, Integer> gradeMap = new HashMap<Integer, Integer>();
int roundedGrade = (grade / 10) * 10;
// Where "incrementMap" is a function you define...
// The purpose of the method is to increment the value (counter) in the map for a particular key
// If the key doesn't exist yet, you want to add it with an initial count of "1"
// Jérémy Dutheil's answer is a good example of this method, but I can include the actual ones that I use since it is a generic library method (see below)
incrementMap(gradeMap, roundedGrade);

// To find upper boundary
int upperBoundary = 0;
for(Map.entry<Integer,Integer> entry : gradeMaps.getEntries())
{
  int count = entry.getValue();
  int tenth = entry.getKey();
  // determine if this is the upper boundary
}

下面是incrementMap的一个实现示例:

代码语言:javascript
复制
public static <T> void incrementMap(Map<T, Integer> map, T key)
{
    incrementMap(map, key, 1);
}

public static <T> void incrementMap(Map<T, Integer> map, T key, int amountToIncrement)
{
    assert map != null;
    int currentValue = map.containsKey(key) ? map.get(key) : 0;
    map.put(key, currentValue + amountToIncrement);
}

我可能把我们拉到一个切线上..。我包含泛型的唯一原因是因为我想给您提供我在我的库中发现有用的实际代码,而不仅仅是伪代码。如果你想让它更容易理解,把所有出现的"T“替换成"Integer”。的目的是允许您将相同的方法用于具有任何类型的“键”的Map,只要"values“是Integer类型。它不一定要是"T“,这只是你会从大多数教程中得到的例子。(例如:http://www.javacodegeeks.com/2011/04/java-generics-quick-tutorial.html)

例如,查看使用两种不同泛型的实用程序方法:

代码语言:javascript
复制
/**
 * Convenience method to create a map containing a single entry.
 * @param pKey the entry key
 * @param pValue the entry value
 * @param <K> the key type
 * @param <V> the value type
 * @return a map with a single entry
 */
public static <K, V> Map<K, V> mapOfOne(K pKey, V pValue)
{
    Map<K, V> target = new HashMap<K, V>();
    target.put(pKey, pValue);
    return target;
}
票数 2
EN

Stack Overflow用户

发布于 2013-11-21 04:42:16

迭代列表并将条目放入Map<Integer, Integer>,其中key是一个数字,value是一个计数。

票数 1
EN

Stack Overflow用户

发布于 2013-11-21 04:48:14

我刚刚意识到,事实上,回答你的问题的人从来没有真正理解过你的问题;这不仅仅是计算一个事件,而是找到第十个事件,然后计算每十个事件的数量。

因此,首先,我们需要一个函数来确定给定整数的第十位

代码语言:javascript
复制
int getTenth( int number ) {
    return Math.floor( number / 10 );
}

在这里,我们只需将number除以10 (因为您在示例中只给出了小于100的数字),然后将其舍入为最接近的较低整数。

然后,您必须存储列表中每十个键的计数;为此,我们将使用一个Map,它允许您存储键列表和相关的(有关更多信息,请参阅the documentation。在您的问题中,我们将使用第十个作为键,并将计数作为值。

代码语言:javascript
复制
function Map< Integer, Integer > getTenthCount( List< Integer > list ) {
    Map< Integer, Integer> tenthCount = new HashMap< Integer, Integer >();

    for( Integer i : list ) {
        int tenth = getTenth( i );
        if( !tenthCount.containsKey( tenth ) ) {
            tenthCount.put( tenth, 1 );
        } else {
            // Value exists, add to count
            int count = tenthCount.get( tenth );
            count++;
            tenthCount.put( tenth, count );
        }
    }

    return tenthCount;
}

然后,您可以轻松地使用这两个函数来解析列表中的值并使用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20106541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档