简体   繁体   English

根据数组列表中的百分比计算记录数

[英]count number of records based on percentages which are in arraylist

I have an Arraylist which contains a list of records and in each single record I have percentage field. 我有一个Arraylist ,其中包含记录列表,并且在每条记录中,我都有一个百分比字段。

I am trying to show the count of students who have percentage in between 我试图显示介于两者之间的学生人数

40% to 50% , 51% to 60% , 61% to 70% , 71% to 80% , 81% to 90% , 91% to 100% . 40%至50%,51%至60%,61%至70%,71%至80%,81%至90%,91%至100%。

For Example, Suppose there are 5 students in between 61% to 70% I want to get the count as 5. How can I get count of all students with specified percentages dynamically . 例如,假设有5位学生在61%到70%之间,我希望得到的计数为5。我如何动态地获得所有具有指定百分比的学生的计数。 Can anyone please suggest me the approach to follow in this regard. 任何人都可以建议我在这方面遵循的方法。

Don't count Students, count their percentages, like this: 不要算学生,算他们的百分比,像这样:

int count = list.stream()
    .map(Student::getPercentage)
    .filter(n -> n >= low && n <= high)
    .count();

Being a Collection, an ArrayList lets us stream the Student objects, which are then converted to a stream of percentages, which then is filtered to keep only those percentages in the desired range, which are then counted. 作为集合,ArrayList让我们流传输Student对象,然后将其转换为百分比流,然后对其进行过滤以仅将那些百分比保持在所需范围内,然后对其进行计数。


To collect and iterate though ranges of 10 point brackets: 收集并迭代10个尖括号的范围:

new TreeMap<>(list.stream()
    .collect(Collectors.groupingBy(s -> s.getPercentage() / 10, Collectors.counting()))
.forEach((r, n) -> System.out.print(r + "0% to " + ++r + "0% had " + n));

This groups by the result of integer division by 10 (add a cast to the percentage to int if it's not an integer), adds a downstream counting operation, then processes each one. 这将整数除以10的结果进行分组(如果不是整数,则将百分比强制转换为int ),添加下游计数操作,然后处理每个整数。

The TreeMap wrapper puts the percentage ranges in order. TreeMap包装器按顺序排列百分比范围。

I would take the stream-approach a bit further: 我将进一步采用流方法:

Map<PercentageRange, Integer> counts =
    list.stream()
        .collect(Collectors.groupingBy( 
             student -> rangeOf(student.getPercentage())), 
             Collectors.counting()));

You should use a lambda expression. 您应该使用lambda表达式。 This will get you a map with all specified ranges and the students. 这将为您提供所有指定范围和学生的地图。

ArrayList<Student> students = new ArrayList<>();

//Each element will have the range as key and all students who fit in the range.
Map<MinMax, List<Student>> map = new HashMap();

//Add all ranges you want to filter your students-list by.
ArrayList<MinMax> minMaxes = new ArrayList<>();
minMaxes.add(new MinMax(20,40);
minMaxes.add(new MinMax(40,60);
minMaxes.add(new MinMax(60,80);

for (MinMax minMax : minMaxes) {
  map.put(minMax, students.stream().filter(u -> u.percentage < minMax.min && u.percentage > minMax.max).collect(Collectors.toList()));
}

class MinMax {
  private int min;
  private int max;


  public MinMax(int min, int max) {
    this.min = min;
    this.max = max;
  }

  public int getMin() {
    return min;
  }
  public void setMin(int min) {
    this.min = min;
  }
  public int getMax() {
    return max;
  }
  public void setMax(int max) {
    this.max = max;
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM