Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 计算arrayList中相同元素的数量_Java_Arraylist_Hashmap - Fatal编程技术网

Java 计算arrayList中相同元素的数量

Java 计算arrayList中相同元素的数量,java,arraylist,hashmap,Java,Arraylist,Hashmap,因此,我正在创建一个考试评分系统,它有一个getExamCore(学生a,intExamid)方法。此方法检索指定考试的答案键、该考试的学生答题表,并评估学生得到的分数。这是它的大致功能 Exam toIterate = database.get(examId); Map<Integer, Question> map = toIterate.getMap(); for(Map.Entry<Integer, Question> entry : map.ent

因此,我正在创建一个考试评分系统,它有一个getExamCore(学生a,intExamid)方法。此方法检索指定考试的答案键、该考试的学生答题表,并评估学生得到的分数。这是它的大致功能

Exam toIterate = database.get(examId);
    Map<Integer, Question> map = toIterate.getMap();
    for(Map.Entry<Integer, Question> entry : map.entrySet()){
        answerKey.add(entry.getAnswer());
        points.add(entry.getPoints());
    }
System.out.println(answerKey);
System.out.println(points);
//Outputs 
[False, True, False]
[2.0, 4.0, 3.0]

使用您当前的设计

answerKey.entrySet().stream()
    .mapToDouble(e -> e.getValue().equals(answers.get(e.getKey())) ? points.get(e.getKey()) : 0)
    .sum();
使用问题地图

questions.entrySet().stream()
    .mapToDouble(e -> e.getValue().getAnswer().equals(answers.get(e.getKey())) ? e.getValue().getPoints() : 0)
    .sum();
studentAnswers
更改为
Map


如果不考虑代码的细微差别,您的方法似乎很笨拙,例如,
Map
是无用的

记录某个问题的正确答案的最自然的方法是在问题字段中记录正确答案,或者使用
映射来解耦,或者如果多个选项是“正确答案”,则设置
但它不会改变此解决方案:

Map<Question, Answer> correct;
Map<Question, Answer> actual;

int total = actual.entrySet().stream()
    .filter(e -> e.getValue().equals(correct.get(e.getKey()))
    .mapToInt(e -> e.getKey().getScore())
    .sum();
映射正确;
实际地图;
int total=actual.entrySet().stream()
.filter(e->e.getValue().equals(correct.get(e.getKey()))
.mapToInt(e->e.getKey().getScore())
.sum();
questions.entrySet().stream()
    .mapToDouble(e -> e.getValue().getAnswer().equals(answers.get(e.getKey())) ? e.getValue().getPoints() : 0)
    .sum();
 studentAnswers.entrySet().stream()
     .mapToDouble(e -> answerKey.get(e.getKey()).equals(e.getValue()) ? points.get(e.getKey()) : 0)
     .sum();
Map<Question, Answer> correct;
Map<Question, Answer> actual;

int total = actual.entrySet().stream()
    .filter(e -> e.getValue().equals(correct.get(e.getKey()))
    .mapToInt(e -> e.getKey().getScore())
    .sum();