如何遍历对象的ArrayList

时间:2014-12-02 15:45:09

标签: java object arraylist

我有ArrayList自定义Users.该列表已按manager.排序我的目标是按manager浏览列表并将每个用户添加到电子邮件的正文取决于其到期日期。

User基本上是从数据库中构建的。所有必要的访问者/变异者都存在:

id|fName|lName|...|manager

如果用户即将过期,请浏览用户并通知经理:

To: Manager

Expiring in 10 days
<User>
<User>
Expiring in 30 days
<User>

StringBuilder body = new StringBuilder();
ArrayList<Users> contractors;
Date today = cal.getTime();
...
if(contractors != null && contractors.size() > 0){

 for(int i = 0; i < contractors.size(); i++){

  if(i+1 > contractors.size()){
   //do something to avoid outOfBounds and still access last item in the list
  }else{

   if (contractors.get(i+1).getManager() != null){ 
    if(manager.equals(contractors.get(i+1).getManager())){
     if(today.compareTo(contractor.getExpiration()){
       //build body of email
     }
    }
  }
  sendEmail(manager, body.toString());
 }else{
  //new manager
  body.equals(""); // reset email for next run
}
}

发送电子邮件后,我想继续根据manager.转到下一组用户我的问题是我遇到了管理器遍历数组后面的逻辑,然后每次都重置新manager.我在想我需要另一个for loop

最好的方法是什么?感谢

修改

以这种方式实施:

4 个答案:

答案 0 :(得分:0)

迭代Users列表,并将其添加到由经理键入的Map,每位经理有Set名员工。像,

if (contractors != null && contractors.size() > 0) {
    Map<String, Set<Users>> map = new HashMap<>();
    for (Users contractor : contractors) {
        String manager = contractor.getManager();
        if (manager == null) {
            manager = contractor.getName();
        }
        Set<Users> employees = map.get(manager);
        if (employees == null) {
            employees = new HashSet<>();
            map.put(manager, employees);
        }
        employees.add(contractor);
    } // now you can iterate the keySet and then each manager's employees like
    for (String manager : map.keySet()) {
        Set<Users> employees = map.get(manager);
        for (Users u : employees) {
            // ...  
        }
    }
}

答案 1 :(得分:0)

你应该选择&#34; foreach&#34;在承包商上循环,更多信息here

另一个例子here

答案 2 :(得分:0)

我会这样做:

if (contractors != null) {
    String currentManager = null;

    for (User contractor : contractors) {
        String contractorManager = contractor.getManager();

        if (!contractorManager.equals(currentManager) {
            // a new manager
            if (currentManager != null) {
                sendEmail(body.toString());
                body.setLength(0);
            }
            currentManager = contractorManager;
        }
        //build body of email ...
    }

    // send the email for the last manager
    sendEmail(body.toString());
}

答案 3 :(得分:0)

如果它们已经排序,您可以采用这种方法(我使用&#34; ManagerObject&#34;来表示Users.getManager()的返回类型 - 替换为实际的类名称):< / p>

StringBuilder body = new StringBuilder();
ManagerObject currentManager = null;
for (Users contractor : contractors) {
  if (currentManager != null && !(contractor.getManager().equals(currentManager)) {
    sendEmail(body.toString());
    body.equals("");
  }
  currentManager = contractor.getManager();
  // Add stuff to the body for this contractor
}

如果由于某种原因对Users.getManager()的调用计算成本很高,则可以重新调整以仅在值更改时设置currentManager