0

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

Ethem
  • 13
  • 5
  • 3
    Using the Stream api won't magically solve your performance problems. Instead, attach debugging tools and *measure* where the problem is. Then fix that. – Jorn Jul 27 '23 at 14:00
  • Read the the description 3 time, really didn't understand what your requirement is. – Lunatic Jul 27 '23 at 14:10
  • 2
    I would try and solve this in the database query instead, do you think that would be possible? If you can closeout what you want to achieve, and include your DB query, maybe you can get help here to solve it by the query in the DB domain instead of in the application domain. – Andreas Lundgren Jul 27 '23 at 14:46

0 Answers0