一个list要把相同的元素的某个值累加起来,怎么办
我有一个list,保存了学生数据,但他们有重复,现在根据学生学号(studentNo),姓名(name),性别(sex),年龄(age),来判断他们是不是一样的,如果一样...
我有一个list,保存了学生数据,但他们有重复,现在根据学生学号(studentNo),姓名(name),性别(sex),年龄(age),来判断他们是不是一样的,如果一样择他们的成绩相加。有可能有三个一样的,比如成绩是30,20,50,那么最后这个学生只有一条数据,成绩(score)栏显示100,(不要问我为什么要这么做,不要说不和逻辑,解决问题就OK)
展开
2个回答
展开全部
常规做法就是遍历list,然后取出studentNo&name进行比对,放在map中,like this:
for( student in studentList){
if(map.contains(student[(name+studentNo)])){
map.get(name+studentNo)+student.score
}else{
// put score into the map with name&studentNo key
}
}
this map object is the final result you want
//或者你可以写一个子类继承hashmap,重写put方法,把这个方法包含了你需要的逻辑,然后一个个直接put进去即可
for( student in studentList){
if(map.contains(student[(name+studentNo)])){
map.get(name+studentNo)+student.score
}else{
// put score into the map with name&studentNo key
}
}
this map object is the final result you want
//或者你可以写一个子类继承hashmap,重写put方法,把这个方法包含了你需要的逻辑,然后一个个直接put进去即可
展开全部
import java.util.Collections;
import java.util.List;
public class Program {
/**
* @param args
*/
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
Student stu1 = new Student("20140101", "张三", 1, 20, 39);
Student stu2 = new Student("20140102", "里斯", 1, 23, 88);
Student stu3 = new Student("20140101", "张三", 1, 20, 26);
Student stu4 = new Student("20140103", "王五", 1, 17, 57);
Student stu5 = new Student("20140101", "张三", 1, 20, 99);
students.add(stu1);
students.add(stu2);
students.add(stu3);
students.add(stu4);
students.add(stu5);
Collections.sort(students);
Student stu = students.get(0);
for (int i=1;i<students.size();i++) {
Student s = students.get(i);
if (s.equals(stu)) {
stu.setScore((stu.getScore() + s.getScore()));
if (i == students.size() - 1) {
System.out.println(stu.getName() + ",score:" + stu.getScore());
}
}
else {
System.out.println(stu.getName() + ",score:" + stu.getScore());
stu = s;
}
}
}
}
class Student implements Comparable<Student>,Cloneable{
private String studentNo;
private String name;
private int sex;
private int age;
private double score;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String studentNo, String name, int sex, int age, double score) {
super();
this.studentNo = studentNo;
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student [studentNo=" + studentNo + ", name=" + name + ", sex="
+ sex + ", age=" + age + "]";
}
@Override
public int compareTo(Student o) {
if (o == null) {
return 1;
}
if (o.getAge() == this.age && o.getName().equals(this.name)
&& o.getSex() == this.sex
&& o.getStudentNo().equals(this.studentNo)) {
return 0;
}
return 1;
}
public boolean equals(Student o) {
if (o == null) {
return false;
}
if (this.compareTo(o) == 0) {
return true;
}
return false;
}
public Student clone() {
return this.clone();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询