简体   繁体   English

java中HashMap中一个键的多个值

[英]Multiple value for a key in HashMap in java

Problem问题

I am struggling to solve a problem where user inputs student name with marks ie [(John,50),(Allex,70),(Tim,80),(Allex,90)].我正在努力解决用户输入带有标记的学生姓名的问题,即 [(John,50),(Allex,70),(Tim,80),(Allex,90)]。

Now I want average of marks of each student ie [(John,50),(Allex,80),(Tim,80)].现在我想要每个学生的平均分数,即 [(John,50),(Allex,80),(Tim,80)]。

As I was planning to use HashMap in this case but I realized that a single key cannot have multiple values so how can I approach the question using map or another data structure.由于我计划在这种情况下使用 HashMap,但我意识到单个键不能有多个值,所以我如何使用 map 或其他数据结构来解决这个问题。

Single Key cannot have multiple values but single key can have a single object capable of holding multiple value!单键不能有多个值,但单键可以有一个能够保存多个值的对象! Use

HashMap<String, List<Integer>> 

Or you can also use或者你也可以使用

HashMap<String,List<subject>>

where subject class can have subject name,subject id,etc...其中主题类可以有主题名称、主题 ID 等...

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

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