First of all, I know that the database is set up incorrectly, but I have no chance to change it. I keep the messages sent to the companies and the reply messages in the same table. Namely, if a company has answered the question sent to it, it is stored in my database via firstMessageId. firstMessage id is equal to id in my table. Previously, there was no problem when there were 500-1000 messages, but now there are 40000 messages and map.put, Collection.sort etc. It takes 2 minutes to open the message box with methods. I think I need to use stream api to fix this problem, but I couldn't generate the necessary code. I'll go back to a list, and within that list there will be a separate list of reply messages. I will also link them with getID==getFirstMessageID.
MessageDTO.java:
private int id;
private int firstMessageID;
private List<MessageDTO> answers = new ArrayList<MessageDTO>();
My old code:
List<MessageDTO> list = null;
List<MessageDTO> messageList = new ArrayList<MessageDTO>();
Map<Integer, MessageDTO> messageMap = new HashMap<Integer, MessageDTO>();
try{
list = MessageDTO.instance.getCompanyMessages(conn, User); // a JDBC connection
for (MessageDTO messageDTO: list) {
if(MessageDTO.getfirstMessageID() == 0){
messageMap.put(MessageDTO.getId(), messageDTO);
}else{
MessageDTO tmp = messageMap.get(messageDTO.getfirstMessageID());
tmp.getAnswers().add(messageDTO );
}
}
for (Iterator<MessageDTO> iterator = messageMap.values().iterator(); iterator.hasNext();) {
MessageDTO messageDTO = (MessageDTO ) iterator.next();
Collections.sort(messageDTO.getAnswers());
messageList.add(messageDTO);
}
Collections.sort(messageList);
} catch etc.
return messageList;
thanks all