代码之家  ›  专栏  ›  技术社区  ›  Geek

处理java集合对象以过滤数据

  •  0
  • Geek  · 技术社区  · 4 年前

    我有一个hibernate查询,它返回一个通知列表。 通知对象有一个Set

    通知:

    private BigDecimal id;
    private BigDecimal createdBy;
    private Date createDate;
    private BigDecimal deletedBy;
    private BigDecimal deletedDate;
    private Set<Boards> notificationBoards = new HashSet<Boards>();
    

    董事会:

    private BigDecimal id;
    private BigDecimal createdBy;
    private Date createDate;
    

    我的查询给了我一个 list of Notifications. 我必须根据板Id过滤通知。因此,我需要迭代板以检查Id。 例如,如果baords id=10,则我添加到新列表中,否则忽略。所以我想我会形成另一个新的通知列表,只有板id=10。 下面是我的循环

     public List<Notifications> getNotificationList(List<Notifications> notification, Users loggedInUserObj){
        List<Notifications> notificationLi = new ArrayList<Notifications>(); 
        Set<Boards> boardLi = new HashSet<Boards>();
        Boards b = new Boards();
        Notifications n = new Notifications();
        if(notification != null && notification.size() > 0){
           for(Notifications notificationObj : notification){ 
               n = notificationObj;
               Set<Boards> notificationBoards = notificationObj.getNotificationBoards();
                for(Boards board: notificationBoards){
                     if(board.getId() == loggedInUserObj.getBoardObj().getId()){
                        b = board;  
                     }
                    boardLi.add(b); 
                }               
               n.setNotificationBoards(boardLi);
           }
           notificationLi.add(n);
        }
        return notificationLi;        
    }
    

    我只收到了一个列表中的一个通知(根据数据,我预计会收到2个通知),并且公告板始终为空。有人能指出问题所在以及更好的解决方法吗?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Eklavya    4 年前

    你需要做一些修复

    • 使用 equals 比较时 boardId BigDecimal类型
    • 迭代通知列表时在for循环中添加通知
    • 如果条件允许,在列表中添加板
    • 在循环内创建Notification和Boards对象
     public List<Notifications> getNotificationList(List<Notifications> notification, Users loggedInUserObj){
        List<Notifications> notificationLi = new ArrayList<Notifications>(); 
        if(notification != null && notification.size() > 0){
           for(Notifications notificationObj : notification){ 
               Notifications n = new Notifications();
               n = notificationObj;
               Set<Boards> boardLi = new HashSet<Boards>();
               Set<Boards> notificationBoards = notificationObj.getNotificationBoards();
                for(Boards board: notificationBoards){
                     if(board.getId().equals(loggedInUserObj.getBoardObj().getId())){
                        boardLi.add(board);  
                     }
                }               
               n.setNotificationBoards(boardLi);
               notificationLi.add(n);
           }
        }
        return notificationLi;        
    }
    

    您可以使用Java流API过滤板

    Set<Boards> boardLi = notificationObj.getNotificationBoards()
                   .stream()
                   .filter(board -> board.getId().equals(loggedInUserObj.getBoardObj().getId()))
                   .collect(Collectors.toSet());
    
        2
  •  0
  •   Thiyagu    4 年前

    你正在比较两个 BigDecimal s使用 == .使用 equals .

    board.getId().equals(loggedInUserObj.getBoardObj().getId())
    

    阅读: What is the difference between == and equals() in Java?