java list 输出_Java中关于List集合输出问题?

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);

}

}

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值