比较Java中的两个对象列表

时间:2015-07-28 05:08:21

标签: java performance

我想比较两个不同对象列表中的两个字段,并创建另一个列表以将其返回给用户。我使用了两个for循环来执行此操作。

无论如何我可以改进这段代码吗?如果我可以,怎么样?

我在下面添加我的代码:

public List<UserTestBean> getUsersWithRoleAndAssignedStatus(String roleBuyer, String testId) {
        List<UserInfo> userInfoWithRole = rolesDAO.getUserInfoWithRole(roleBuyer);
        CriteriaBean bean = new CriteriaBean(null, "testId", Long.parseLong(testId));
        @SuppressWarnings("unchecked")
        List<AssignedTestRelation> relations = (List<AssignedTestRelation>) simpleQueryDAO
                .getObjectListWithCriteria(bean, AssignedTestRelation.class);
        List<UserTestBean> userTestBeanList = new ArrayList<UserTestBean>();
        for (UserInfo u : userInfoWithRole) {
            UserTestBean userTestBean = new UserTestBean();
            userTestBean.setEmailId(u.getEmail());
            userTestBean.setUserName(u.getUsername());
            userTestBean.setAssigned(false);

            if (relations.size() > 0) {
                for(AssignedTestRelation b:relations){
                    if(u.getEmail().equals(b.getUser())){
                        userTestBean.setAssigned(true);
                    }
                }
            }
            userTestBeanList.add(userTestBean);
        }
        return userTestBeanList;
    }

0 个答案:

没有答案