简体   繁体   中英

How to compare 2 arraylist in one of value object

I have two arraylist. How to match a value of String just running the proccess?

Coding:

ArrayList<TimeSeriesObject> arrayDayMinutes = new ArrayList<TimeSeriesObject>();
List<GetTimeSeriesResponseType.Row> rows = (List<Row>) el.getObjectValue();

for (i = iStart; i < arrayDayMinutes.size(); i++)
{
    for (int j = n; j < rows.size(); j++)
    {
        nLoop++;
        if (arrayDayMinutes.get(i).getTimeStamp().equals(ConvertDateTime(rows.get(j).getTIMESTAMP().toGregorianCalendar())))
        {
           // khteo 20140211
           if (arrayDayMinutes.get(i).getTimeStamp().equals(ConvertDateTime(rows.get(j).getTIMESTAMP().toGregorianCalendar(), marketInfo.getTimezone())))
           {
               arrayDayMinutes.get(i).setOpen(rows.get(j).getOPEN());
               arrayDayMinutes.get(i).setHigh(rows.get(j).getHIGH());
               arrayDayMinutes.get(i).setLow(rows.get(j).getLOW());     
               arrayDayMinutes.get(i).setClose(rows.get(j).getCLOSE());
               try
               {
                   arrayDayMinutes.get(i).setVolume(rows.get(j).getVOLUME());
               }
               catch (NullPointerException npe)
               {
                   arrayDayMinutes.get(i).setVolume(0);
               }
               n = j + 1;
               long executeReques_end = System.currentTimeMillis();
               // System.out.println("After 2nd loop:" + executeReques_end +"|total timing " + (executeReques_end-executeRequest_2ndbeforeLoop));
               break;
           }
        }
    }
}

If I'm using steps as above,

if (arrayDayMinutes.get(i).getTimeStamp().equals(ConvertDateTime(rows.get(j).getTIMESTAMP().toGregorianCalendar())))

The code above has a bad performance... How to improve performance? When the ArrayList not match, it will keep loading.. can be skip the unmatching value?

I don't get the second if: Isn't there a more effective way to compute:

arrayDayMinutes.get(i).getTimeStamp().equals(ConvertDateTime(rows.get(j).getTIMESTAMP().toGregorianCalendar(), marketInfo.getTimezone()))

when you know that:

arrayDayMinutes.get(i).getTimeStamp().equals(ConvertDateTime(rows.get(j).getTIMESTAMP().toGregorianCalendar()))

is true?

for instance:

    Calendar cal = rows.get(j).getTIMESTAMP().toGregorianCalendar();
    if (cal.getTimeZone().equals(marketInfo.getTimeZone())
            && arrayDayMinutes.get(i).getTimeStamp().equals(ConvertDateTime(cal)))
    {

In any case, if ConvertDateTime is costly, it could be worthwhile to keep it in a cache to avoid computing is several times:

    // assuming that TimeSeriesObject.getTimeStamp() returns a Date
    Date[] cache = new Date[rows.size()];
    for (i = iStart; i < arrayDayMinutes.size(); i++)
    {
        for (int j = n; j < rows.size(); j++)
        {
            Date timestamp = cache[j];
            if (timestamp == null) {
                timestamp = ConvertDateTime(rows.get(j).getTIMESTAMP().toGregorianCalendar());
                cache[j] = timestamp;
            }
            nLoop++;
            if (arrayDayMinutes.get(i).getTimeStamp().equals(timestamp))
            {
...

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