0

I have a hibernate query that returns a list of notifications. Notifications object has a Set

Notifications:

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

Boards:

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

My query gives me a list of Notifications. I have to filter the Notifications based on board Id. So i need to iterate boards to check for id. If baords id=10 for example then i add to new list else ignore. So i thought i will form another new list of Notifications with only board id = 10. Below is my loop

 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;        
}

I am getting only one Notifications instaed of a list(I am expecting 2 Notifications as per data) and boards are always null. Can someone point out whats wrong and a better way of doing the same?

Geek
  • 3,187
  • 15
  • 70
  • 115

2 Answers2

1

You need to do some fix

  • Use equals when comparing boardId of BigDecimal type
  • Add notification inside for loop when iterate list of notification
  • Add board in list inside if condition
  • Create object of Notification and Boards inside loop
 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;        
}

You can use Java stream API for filtering Boards

Set<Boards> boardLi = notificationObj.getNotificationBoards()
               .stream()
               .filter(board -> board.getId().equals(loggedInUserObj.getBoardObj().getId()))
               .collect(Collectors.toSet());
Eklavya
  • 17,618
  • 4
  • 28
  • 57
0

You are comparing two BigDecimals using ==. Use equals.

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

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

Thiyagu
  • 17,362
  • 5
  • 42
  • 79