Reputation: 1314
I'm creating a small application that calculates a grade for a student. It takes two List<Integer>
within a method.
If my list
contains 8 items, for example; {52,62,65,65,72,72,75,75}
I want to find a way of counting through that list
and finding the items that are within the same tenth (eg; 30, 40, 50, 60, 70). So this in case, 4 items will be within 70's and then be able to store it (known as a upper boundary). The aim of this is so that if 4 items are in a upper boundary, the overall student grade will be greater.
My question is, how could I achieve this? I have seen that I could use some sort of map
but I'm not sure if that will be the best way of achieve it.
Upvotes: 1
Views: 528
Reputation: 6137
I've just realized that in fact, no one who answered never really understood your question ; it is not just about counting an occurence, but finding the occurence tenth' and then counting the number for each tenth.
So first, we need a function that will determine the tenth for a given integer
int getTenth( int number ) {
return Math.floor( number / 10 );
}
Here we simply divide number by 10 (as you give only number < 100 in your example), and we round it as the lower integer nearest.
Then you will have to store the count for each of tenth that are in your lists ; for that, we'll use a Map
that allow you to store a list of keys with associated values (please see the documentation for more information. In your problem, we will use the tenth as keys, and the count as values.
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;
}
Then you can easily use those two functions for parsing values from your list and use it.
Upvotes: 0
Reputation: 197
Iterate over your list and put entries into Map where key is a number and value is a count.
Expanding on this previous answer a with a little psuedocode...
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
}
Here is an example implementation for incrementMap:
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);
}
I may be pulling us onto a tangent... the only reason I included the generics was because I wanted to give you actual code that I find useful in my library, rather than only psuedo-code. If you want to make this simpler to understand, replace every occurence of "T" with "Integer". The purpose of the is to allow you to use this same method for a Map with any type of "key", as long as the "values" are of type Integer. It doesn't have to be "T" specifically, that's just the example you will get from most tutorials. (for example: http://www.javacodegeeks.com/2011/04/java-generics-quick-tutorial.html)
For example, check out this utility method that uses two different generic types:
/**
* 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;
}
Upvotes: 2
Reputation: 115328
Iterate over your list and put entries into Map<Integer, Integer>
where key is a number and value is a count.
Upvotes: 1