简体   繁体   中英

How to Iterate over too big ArrayList<String>?

I have two ArrayList sourceMessageList and TargetMessageList. I need to compare both the message list data.

Now lets say List1 - 100 Records. List2 - 1000 records From List1- 1st record is compared with each record in list2 and then List1- 2nd record is compared with each record in list2.

But list2 is getting the value hasNext() to true for 1st source data in list1.

private void compareMessageList(ArrayList<String> source_messageList, ArrayList<String> target_messageList)
            throws Exception {
        // TODO Auto-generated method stub
        Iterator<String> sourceMessageIterator = source_messageList.iterator();
        Iterator<String> targetMessageIterator = null;

        while (sourceMessageIterator.hasNext()) {
            String sourceMessage = (String) sourceMessageIterator.next();

            targetMessageIterator = target_messageList.iterator();
            while (targetMessageIterator.hasNext()) {
                String targetMessage = (String) targetMessageIterator.next();
                if (getCorpValue(sourceMessage).equalsIgnoreCase(getCorpValue(targetMessage))) {
                    assertXMLEquals(convertSwiftMessageToXML(sourceMessage), convertSwiftMessageToXML(targetMessage));
                }
            }
        }

        if (buffer.toString().length() > 0) {

            writeDifferenceTofile(buffer.toString());
            buffer.delete(0, buffer.length());
            throw new CatsException("There are some differences in the files.");
        }

        System.out.println("Exiting now ...");
    } 

The above code is taking too much time to execute.

To speed things up:

HashMap<String, String> lowers = new HashMap<String, String>();
for (String source : source_messageList) {
    lowers.put(getCorpValue(source).toLowerCase(), source);
}
for (String target : target_messageList) {
    final String corpTarget = getCorpValue(target).toLowerCase();
    if(lowers.containsKey(corpTarget)) {
        assertXMLEquals(
            convertSwiftMessageToXML(lowers.get(corpTarget)),
            convertSwiftMessageToXML(target)
        );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM