Java中关于List集合输出问题?

我一个List类型的集合中有以下数据:
Student类中的属性有:
Company ,No1,NO2,NO3,NO4;
省略getter。setter.

输出的结果是:
Company No1 No2 No3 No4

WTCCNDEPT1 0 0 2 1
WTCCNDEPT1 0 0 3 1
WTCCNDEPT1 0 0 2 1
WTCCNDEPT2 0 0 3 2
WTCCNDEPT2 0 0 3 2

这种输出来.有重复的数据..我想要的结果是:
//如果重复;则把重复的行变成一行;然后把NO1, NO2,NO3,NO4 的列的值累加.

还有一种情况就是;两行的值,完全相通,则移除一行.;

Company No1 No2 No3 No4
WTCCNDEPT1 0 0 7 3
WTCCNDEPT2 0 0 3 2

我自己没有做出来.. 求大家帮下忙..能不能帮我想想办法..
解决这个问题..谢谢你们拉..

[code="java"]
public static void main(String[] args) throws Exception {
List students = new ArrayList();
students.add(new Student("WTCCNDEPT1", 0, 0, 2, 1));
students.add(new Student("WTCCNDEPT1", 0, 0, 3, 1));
students.add(new Student("WTCCNDEPT1", 0, 0, 2, 1));
students.add(new Student("WTCCNDEPT2", 0, 0, 3, 2));
students.add(new Student("WTCCNDEPT2", 0, 0, 3, 2));

Set studsSet = new HashSet(students);

List newStuds = new ArrayList();

for (Student student : studsSet) {
String company = student.getCompany();
boolean found = false;
for (Student stud : newStuds) {
if (company.equals(stud.getCompany())) {
stud.setNo1(stud.getNo1() + student.getNo1());
stud.setNo2(stud.getNo2() + student.getNo2());
stud.setNo3(stud.getNo3() + student.getNo3());
stud.setNo4(stud.getNo4() + student.getNo4());
found = true;
break;
}
}
if (!found) {
newStuds.add(student);
}
}
for (Student student : newStuds) {
System.out.println(student);
}
}
[/code]
[code="java"]

public class Student{
private String company;
private int no1;
private int no2;
private int no3;
private int no4;

public Student(String company, int no1, int no2, int no3, int no4) {
this.company = company;
this.no1 = no1;
this.no2 = no2;
this.no3 = no3;
this.no4 = no4;
}

// 省略 getter和setter

@Override
public int hashCode() {
int hash = company.hashCode();
hash = hash * 37 + no1;
hash = hash * 37 + no2;
hash = hash * 37 + no3;
hash = hash * 37 + no4;
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Student) {
Student other = (Student) obj;
return company.equals(other.getCompany()) && no1 == other.getNo1() && no2 == other.getNo2()
&& no3 == other.getNo3() && no4 == other.getNo4();
}
return false;
}

@Override
public String toString() {
return String.format("%s\t%d\t%d\t%d\t%d", company, no1, no2, no3, no4);
}
}
[/code]

你这个应该在数据放入List之前,就是从数据库中查出数据进行过滤应该可以做出来

[color=blue][b]让Student类继承java.util.Comparator接口,实现比较的方法。在该比较方法中,将会把这个2个Student对象认为是同一个对象。[/b][/color]
[quote]
WTCCNDEPT2 0 0 3 2
WTCCNDEPT2 0 0 3 2 [/quote]
[color=blue]
[b]然后,就该List中的所有元素放入到一个Set中,这个重复的元素就不会被放进去了。

最后,输出Set中的所有内容,即是你想要的结果了。[/b][/color]