我有这样的pojo ..
public class abcObject {
private long id;
private long version;
private DateTime created ;
private String status;
}
及其相应的hbm作为内部数据通过hibernate存储在表中现在请告知状态可以有值如pass或failNow我必须过滤值无论是通过还是失败现在我可以检查它的价值通过检查并依赖于我必须将它们放入单独列表中,我已经通过这种方式完成了
List<abcObject> successful = new ArrayList <abcObject>();
List<abcObject> exception = new ArrayList <abcObject>();
List<abcObject> failure = new ArrayList <abcObject>();
//getting the list from the database into the parameter allabcObjects
List<abcdObject> allabcObjects = abcHome.getabcObjects(fileIdentifier);
if (abcObjects !=null && abcObjects.size() > 0) {
for (abcObject f : allabcObjects) {
}
现在请指教,因为我正在使用for循环遍历每个对象,然后通过检查f.getstatus()方法,如果它失败,那么它应该将失败列表中的所有细节都存储起来,如果它是成功然后它应该存储在成功列表中,请告知如何实现这个
答案 0 :(得分:0)
只需将对象与字符串进行比较
List<abcObject> successful = new ArrayList <abcObject>();
List<abcObject> exception = new ArrayList <abcObject>();
List<abcObject> failure = new ArrayList <abcObject>();
//getting the list from the database into the parameter allabcObjects
List<abcdObject> allabcObjects = abcHome.getabcObjects(fileIdentifier);
if (abcObjects !=null && abcObjects.size()>0)
{
for (abcObject f : allabcObjects) {
if( "pass".equalsIgnoreCase(f.getStatus()) ) {
successful.add( f );
} else if ( "failNow".equalsIgnoreCase(f.getStatus()) {
failure.add( f );
}
}
}
只是指出一些事情。更好的是为你的成功/失败字符串创建static final string
:
private static final String SUCCESS = "success";
private static final String FAIL = "failNow";
....
if( SUCCESS.equalsIgnoreCase(f.getStatus()) ) {
successful.add( f );
} else if ( FAIL.equalsIgnoreCase(f.getStatus()) {
failure.add( f );
}
将SUCCESS
与f.getStatus()
进行比较,而不是反向,可以确定如果某种方式f
为空,则不会出现NullPointerException
答案 1 :(得分:0)
您的for循环将显示为:
for (abcObject f : allabcObjects) {
String status = f.getstatus();
if(status.equals("fail")){
failure.add(f);
}
else{
otherList.add(f);
}
}
您还需要声明otherList
,即
List<abcObject> otherList = new ArrayList<abcObject>();
这假设你的getstatus()方法如果失败则返回一个字符串“fail”。