List集合的過濾之lambda表達式


下面代碼 是過濾List中的對象,->后面為條件,滿足條件的才會放入List中

java 8 中lambda表達式寫法
public static void filter(List<SQLStatement> stmtList) {
    stmtList = stmtList.stream().filter(stmt -> !(stmt instanceof OracleInsertStatement) && stmt.getParent() == null).collect(Collectors.toList());
    stmtList = stmtList.stream().filter(stmt -> !(stmt instanceof OracleUpdateStatement) && stmt.getParent() == null).collect(Collectors.toList());
    stmtList = stmtList.stream().filter(stmt -> !(stmt instanceof OracleDeleteStatement) && stmt.getParent() == null).collect(Collectors.toList());
    stmtList = stmtList.stream().filter(stmt -> !(stmt instanceof SQLSelectStatement) && stmt.getParent() == null).collect(Collectors.toList());
    stmtList = stmtList.stream().filter(stmt -> !(stmt instanceof SQLUpdateStatement) && stmt.getParent() == null).collect(Collectors.toList());
    stmtList = stmtList.stream().filter(stmt -> !(stmt instanceof SQLInsertStatement) && stmt.getParent() == null).collect(Collectors.toList());
    stmtList = stmtList.stream().filter(stmt -> !(stmt instanceof SQLDeleteStatement) && stmt.getParent() == null).collect(Collectors.toList());
}

 java 7 以前寫法

   stmtList.stream().filter(new Predicate<SQLStatement>() {
   @Override
   public boolean test(SQLStatement stmt) {
   return !(stmt instanceof OracleInsertStatement);
   }
   }).collect(Collectors.toList());


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM