简体   繁体   English

根据特定条件分割地图

[英]Splitting a map based on certain condition

I have a map as shown below: 我有一张地图,如下所示:

Key        Value
    23      20
    32      20      (20+20  =40 , min=23 max=32)
    43      18
    45      24      (24+18 =42 , since 42 >40 so here min and max will be same that is 43
    47      10    
    56      6       (24 +10 +6 =40) so here min =45 and max = 56
    49       2  
    47      12  

As you can see, there will be final constant named split whose value is 40 如您所见,将有一个名为split的最终常量,其值为40

final int SPLIT = 40;  //this will be configurable as it value can be changed.

I have to implement the logic such as if the value of the map reaches 40, then the first key of the map from where the calculation started and the key where exactly it reaches to 40 will be chosen as min and max also, as explained above. 我必须实现逻辑,例如,如果映射的值达到40,那么从计算开始的映射的第一个键以及恰好达到40的键也将被选择为min和max,如上所述。 。

Besides this, care needs to be taken if sum reaches more than 40. If so, we have to ignore it and take the previous value itself as min and max in the case where min and max would be equal. 除此之外,如果总和超过40,则需要格外小心。如果是,我们必须忽略它,并且在最小值和最大值相等的情况下,将先前的值本身作为最小值和最大值。

Please suggest me how can I achieve this with Java and Map . 请建议我如何使用Java和Map folks please advise 乡亲们请指教

The data that I am geeting is not from database I am geeting it from hibernate criteria in an object list 我要存储的数据不是来自数据库的,而是我从对象列表中的休眠条件中获取的

I am getting an list from Hibernate criteria as shown below ... 我从Hibernate标准中获取列表,如下所示...

  List<Object[]> abcObjectsList= session.createCriteria(dddObject.class) 

upon inspecting while I am getting data in this format 在我以这种格式获取数据时进行检查

abcObjectsList= ArrayList<E>
     elementData =Object[3]
        [0] = Long  ----------> value 23
        [1] = Integer -------> value 20
        [0] = Long  ----------> value 32
        [1] =Integer -------> value 20
        [0] =Long  ----------> value 43
        [1] =Integer -------> value 18

I have stored it in a map in such a way as I was requiring it in same fashion 我将其以相同的方式存储在地图中

  Map<Long, Integer> result = new HashMap<Long, Integer>();
            for (Object[] arr : list) {
                result.put((Long) arr[0], (Integer) arr[1]);
            }

so finally map will contain.. 所以最终地图将包含..

  Key      Value
        23      20
        32      20  (20+20  =40 , min=23 max=32)
        43      18

Instead of using a Map, you could create a Pair class that will hold the key and the value. 除了使用Map之外,您还可以创建一个Pair类,该类将保存键和值。

class Pair {
    public int key;
    public int value;

    public Pair(int key, int value){
        this.key = key;
        this.value = value;
    }
}

Then create a list of pair and iterate through it. 然后创建一个配对列表并对其进行迭代。 If the sum is 0, initialize the min and the max. 如果总和为0,则初始化最小值和最大值。 Then for each pair iterated, add its value to the sum. 然后对于每个迭代的对,将其值加到总和上。 If the sum is inferior continue the loop and update the max key, else you have two cases possible: 如果总和小于,请继续循环并更新max键,否则可能出现两种情况:

  1. The sum is equals to the limit so update the max key 总和等于限制,因此更新最大密钥
  2. The sum is not equals to the limit (so it's superior), decrement the index and don't update the max key 总和不等于限制(因此更好),减少索引并且不更新最大密钥

public static void main(String[] arg) {
    List<Integer> indexList = Arrays.asList(23,32,43,45,47,56,49,47); // get this from database
    List<Integer> valueList = Arrays.asList(20,20,18,24,10,6,2,12); // get this from database
    List<Pair> pairList = new ArrayList<>();
    for(int i = 0; i < indexList.size();i++){
        pairList.add(new Pair(indexList.get(i), valueList.get(i)));
    }
    int sum = 0;
    int min = -1;
    int max = -1;

    for(int i = 0; i < pairList.size(); i++){
        Pair p = pairList.get(i);
        if(sum == 0){
            min = p.key;
            max = p.key;
        }
        sum += p.value;
        if(sum < LIMIT){
            max = p.key;
        } else {
            if(sum > LIMIT){
                i--;
            } else {
                max = p.key;
            }
            System.out.println(min+"_"+max);
            sum = 0;
        }
    }
}

Which prints: 哪些打印:

23_32
43_43
45_56

I show you how to create a list of pair through your map (use a LinkedHashMap to preserve insertion order) (obviously, you'll need to modify a little bit the Pair class): 我将向您展示如何通过地图创建一个配对列表(使用LinkedHashMap保留插入顺序)(显然,您需要Pair类进行一些修改):

Map<Long, Integer> m = new LinkedHashMap<>();
//fill your map here
List<Pair> l = new ArrayList<>();
for(Map.Entry<Long, Integer> entries : m.entrySet()){
    l.add(new Pair(entries.getKey(), entries.getValue()));
}
//Now you have a list of Pair

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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