2
java version "1.7.0_45"

I am trying to create mutliple lists that will contain the same messages sent from the JID.

For example offlineMessageHeaderList will contain many messages from different and the same JID's. I want to group all the ones that have the same JIDs in to there own lists.

messageList1 -> all messages from John
messageList2 -> all messages from Peter
etc.

Then at the end check which ones are empty and add them to the groupList.

However, my solution is not good as I declare everything first and I could have more or less messages with the same JID.

code snippet for checking and adding

    List<List<MessageHeader>> groupList = new ArrayList<>();
    List<MessageHeader> messageList1 = new ArrayList<>();
    List<MessageHeader> messageList2 = new ArrayList<>();
    List<MessageHeader> messageList3 = new ArrayList<>();
    List<MessageHeader> messageList4 = new ArrayList<>();

    if(offlineMessageHeaderList.size() > 0) {
        for(OfflineMessageHeader header : offlineMessageHeaderList) {
            log.log(Level.INFO, "JID: " + header.getJid());
            log.log(Level.INFO, "Stamp: " + header.getStamp());
            log.log(Level.INFO, "User: " + header.getUser());

            MessageHeader messageHeader = new MessageHeader(header.getJid(), header.getStamp(), header.getUser());

            /* Check do we have message list already for this JID */
            /* Does this messageList1 already have this JID message */
            if(messageList1.get(0).getJid().compareTo(messageHeader.getJid()) == 0) {
                /* then we add all the same Jid to this list */
                messageList1.add(messageHeader);
            }
            else {
                /* The JID was different so need to add it to new list */
                if(messageList2.get(0).getJid().compareTo(messageHeader.getJid()) == 0) {
                    messageList2.add(messageHeader);
                }
            }
    /* Do the same for the others */
        }

        /* Add all the non-empty lists to the groupList */
        if(!messageList1.isEmpty()) {
            groupList.add(messageList1);
        }

        if(!messageList2.isEmpty()) {
            groupList.add(messageList2);
        }

        if(!messageList3.isEmpty()) {
            groupList.add(messageList3);
        }

        if(!messageList4.isEmpty()) {
            groupList.add(messageList4);
        }

    }

class of the MessageHeader
    public class MessageHeader {
        private String mJid;
        private String mStamp;
        private String mUser;

        public MessageHeader(String jid, String stamp, String user) {
            mJid = jid;
            mStamp = stamp;
            mUser = user;
        }

        public String getJid() {
            return mJid;
        }

        public String getStamp() {
            return mStamp;
        }

        public String getUser() {
            return mUser;
        }
    }

Is there any way I can do this in a more dynamic way?

0

2 Answers 2

1

If I understood correctly, you can use the below approach to minimize number of objects and proper use of data structure.

1) Create a Map<String, List<MessageHeader>> to store all the message for each JID. In the Map the key is JID and value is the list of the messages for the JID.

2) When a message comes, check if a key with JID is present:

if yes - fetch the value from the Map for the JID, which would return a List<MessageHeader> object and add your new message into the list.

if No - Create a new entry within Map, with JID as key and a new ArrayList<MessageHeader>

Later you could iterate through the map to get eligible JID for inserting into group list

Sign up to request clarification or add additional context in comments.

Comments

1

Don't create an object like that, create a reference and when you think you have data to add in the list than create an Object i.e.

List<MessageHeader> messageList1;// Creating the reference or pass null to it
List<MessageHeader> messageList2;;// Creating the reference or pass null to it

and when your check it true create an object i.e.

if(messageList1.get(0).getJid().compareTo(messageHeader.getJid()) == 0) {
                /* then we add all the same Jid to this list */
                messageList1 = new ArrayList<>(); // Create an Object
                messageList1.add(messageHeader);
            }
            else {
                /* The JID was different so need to add it to new list */
                if(messageList2.get(0).getJid().compareTo(messageHeader.getJid()) == 0) {
                    messageList2 = new ArrayList<>(); // Create an Object
                    messageList2.add(messageHeader);
                }
            }

In that way you are avoiding unnecessary Object creation, ArrayList Object.

Note: In your code you are running a FOR loop so make sure you create an ArrayList Object 1 single time otherwise it will create a new Object always, recommend to enhance your if Condition.

1 Comment

Thanks for the answer. But not quite what I was looking for. As I don't know how many type messageList I will need. It all depends on the JIDs in the offlineMessageHeaderList. I am looking to group all the JIDs with the same name into there separate lists. However, I could have up to 10 different ones. So I could have up to 10 lists. I hope I am more clear. I am working on another solution now to sort the offlineMessageHeaderList first to see how many different JIDs there are first. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.