悠悠隐于市 2010-05-18 23:16
浏览 374
已采纳

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

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

  • 写回答

3条回答

  • weixin_42518082 2010-05-19 08:48
    关注

    [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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog