根据特定条件分割地图 [英] Splitting a map based on certain condition

查看:60
本文介绍了根据特定条件分割地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张地图,如下所示:

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  

如您所见,将有一个名为split的最终常量,其值为40

As you can see, there will be final constant named split whose value is 40

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

我必须实现逻辑,例如,如果地图的值达到40,然后,如上所述,将地图从计算开始的第一个键和精确到40的键选择为最小值和最大值.

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,则需要格外小心.如果是,我们必须忽略它,并且在min和max相等的情况下,将先前的值本身作为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.

请建议我如何使用Java和 Map 实现此目标.乡亲请指教

Please suggest me how can I achieve this with Java and 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

我从Hibernate标准中获取了一个列表,如下所示...

I am getting an list from Hibernate criteria as shown below ...

  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

推荐答案

您可以创建一个包含密钥和值的 Pair 类,而不是使用Map.

Instead of using a Map, you could create a Pair class that will hold the key and the value.

class Pair {
    public int key;
    public int value;

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

然后创建一个配对列表并对其进行迭代.如果总和为0,则初始化最小值和最大值.然后,对于每个迭代的对,将其值加到总和上.如果总和小于,请继续循环并更新最大密钥,否则可能出现两种情况:

Then create a list of pair and iterate through it. If the sum is 0, initialize the min and the max. 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:

  1. 总和等于上限,因此请更新最大密钥
  2. 总和不等于限制(因此更好),减少索引并且不更新最大密钥


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;
        }
    }
}

哪些印刷品:

23_32
43_43
45_56

我向您展示了如何通过地图创建一个配对列表(使用 LinkedHashMap 保留插入顺序)(显然,您需要对 Pair 类):

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):

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

这篇关于根据特定条件分割地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆