Iterate over a list that has an object type

I have a method that is sleeping through which I get a list as shown below.

List<abcObject> aaList= session.createCriteria(abcObject.class)

now when checking, I found that the list type has an object type, since api criteria returns us a list of object types

Now the list I get is the following:

  t_id   value
    11      3
    12      20
    14      60
    15      17    ------->(3+20+60+17 =100),here min =11 & max =15
    18      40  
    22      20
    33      40
    45      20   ---------->(40+20+40+20 =100),here min =18 & max =45

Now, please consult, since I need to iterate over the list so that if the number of values ​​reaches 100, then a separate map must be created, which will be min and the value will be max, now the map has a row type

min     max

11      15
18      45

and also be careful that this parameter, on which division equal to 100 was performed, can sometimes be set to 60, therefore this separation parameter needs to be set

, , , , min max , .

min     max

11      15
18      45

, , java, .

-, .

 final int BARRIER = 100;

    //I am going to assume there are getter methods in you abcObject and 
    //that all values are greater than 0
    List<abcObject> minMaxs = new List<abcObject>();
    int sum = 0; 
    int min = 0;
    for (abcObject obj: aaList) {
        if (sum == 0) {
            //start over, remember min
            min = obj.getT_id();
        }
        //add value to sum
        sum += obj.getValue();
        if (sum >= BARRIER) {
            //now we need to start again, remember what we got
            minMaxs.add(new abcObject(min, obj.getT_id()));
            //reset
            min = 0;
            sum = 0;
        }
    }
+4
1

, BARRIER min max . , a knapsack.

int sum = 0; 
int min = 0;
int max = 0;
for (abcObject obj: aaList) {
    int tId = obj.getT_id();
    if (sum == 0) {
       //start over, remember min
       min = tId;
       max = min;
    }
    //add value to sum
    sum += obj.getValue();
    min = tId < min ? tId : min; //check if tId is lesser than min
    max = tId > max ? tId : max; //check if tId is greater than max
    if (sum >= BARRIER) {
       //now we need to start again, remember what we got
       minMaxs.add(new abcObject(min, max));
       //reset
       min = 0;
       max = min;
       sum = 0;
    }
}

. , , , min , max .

0

Source: https://habr.com/ru/post/1524014/


All Articles