Java two list data are merged according to a certain field

Keywords: less

Previously, I met a requirement that the field is hourly (2018101208). Then the service gave me less data. Then I need to figure it out myself. Then fill in the data without those hours. It also involves that the data list designed by ourselves is comprehensive. All hours. Then get the server list and compare it according to this, and fill in the number of hours that the server does not have. Two lists are merged based on a field.

1. Obtain the hours of the past 15 days

initialize variable

    public String start_ts = "1537812000000";
    //Analyze historical data
    private List<HistoryData> historyDataList = new ArrayList<>();
    //Self calculated historical data
    private List<String> calculateDataList = new ArrayList<>();

Get the current time of the system, calculate the time 15 days ago, and store it in list < string > every hour. This is also a small algorithm.

    public void SetContext(Context context) {
        if (mContext != null) mContext.clear();
        mContext = new WeakReference<Context>(context);

        long time = System.currentTimeMillis();

        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH");
        String strNow = df.format(time);

        time = dateConvertMillionSec(strNow);
        //Gets the current time, minus 24 hours. It's about getting hourly data from the past day
        long tempTime = 1000 * 60 * 60 * 24 * 15L;

        start_ts = String.valueOf(time - tempTime);

        for (int j = 1; j < 15; j++) {
            for (int i = 1; i < 25; i++) {
                calculateDataList.add(df.format(time - 1000 * 60 * 60 * j * i));
            }
        }
    }

    private long dateConvertMillionSec(String str) {
        long millionSeconds = 0;
        try {
            millionSeconds = new SimpleDateFormat("yyyyMMddHH").parse(str).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return millionSeconds;
    }

2. Merge the two list s according to the time field

Method 1: compare the fields with the contains method in the list. Find out that there is no data in list < HistoryData > and put it in the field of HistoryData in list < HistoryData >.

            for (String temp : calculateDataList) {
                if (!tempList.contains(temp)) {
                    HistoryData historyData = new HistoryData();
                    historyData.setDatetime(temp);
                    historyDataList.add(historyData);
                }
            }

Method 2, two for loop traversal can also be done.

            for (String temp : calculateDataList) {
                boolean flag = false;
                for (HistoryData historyData : historyDataList) {
                    if (temp.equals(historyData.getDatetime())) {
                        flag = true;
                        break;
                    }
                }

                if (!flag) {
                    HistoryData historyData = new HistoryData();
                    historyData.setDatetime(temp);
                    historyDataList.add(historyData);
                }
            }

3. A list sorting method is attached at the end, sorting by time

        Collections.sort(historyDataList, new Comparator<HistoryData>() {
            @Override
            public int compare(HistoryData temp1, HistoryData temp2) {
                //You can sort by other properties of the User object, as long as the property supports the compareTo method
                Integer it1 = Integer.valueOf(temp1.getDatetime());
                Integer it2 = Integer.valueOf(temp2.getDatetime());

                return it1.compareTo(it2);
            }
        });

 

Posted by electrix on Mon, 16 Dec 2019 13:56:02 -0800