简体   繁体   English

生成唯一的序列号java

[英]Generate unique sequence numbers java

I generate lists based on Map,我根据地图生成列表,

       Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();

complete code is here: https://repl.it/L3cv/0完整代码在这里: https : //repl.it/L3cv/0

Result of the code:代码结果:

      list1: [account_number, account_number, account_number, account_number, Amount, Amount, Amount, Amount]
      list2: [1, 2, 3, 4, 100, 400, 500, 700]

Desired result: (generating unique sequence/id based on list2)期望结果:(根据 list2 生成唯一的序列/id)

      list3: [1, 2, 3, 4, 1, 2, 3, 4]

In the code, I tried to add 'sequence_number', but it just gives the incremental value [1,2,3,4,5,6,7,8]在代码中,我尝试添加“sequence_number”,但它只给出了增量值 [1,2,3,4,5,6,7,8]

        sequence_number = 0;
        for (final Integer val : value) {
            sequence_number++;
            list3.add(key);
            list4.add(val);
        }

but this does not give me the desired result.但这并没有给我想要的结果。 How to do it?怎么做?

There are two possible solutions, based on your answer to this question:根据您对此问题的回答,有两种可能的解决方案:

"Does the first list always contain pairs of items? Means, it always has an even number of entries, so if there are 12 entries, you expect 1,2,3,4,5,6,1,2,3,4,5,6?" “第一个列表是否总是包含成对的项目?意味着,它总是有偶数个条目,所以如果有 12 个条目,你期望 1,2,3,4,5,6,1,2,3,4 ,5,6?”

If yes, your solution goes like this:如果是,您的解决方案是这样的:

 int halflist = list1.size() / 2;
 int sequencenumber = 0;
 for (...) {
    sequencenumber++;
    // add your stuff
    if (sequencenumber == halflist) sequencenumber = 0;
 }

This will raise the value 1,2,3,4,1,2,3,4.这将提高值 1,2,3,4,1,2,3,4。 It could be done with modulo and more fancy code too, but the simple increment here should keep it understandable.它也可以用模数和更花哨的代码来完成,但这里的简单增量应该让它易于理解。

  • If no, please provide more details, what you want to achieve.如果不是,请提供更多详细信息,您想要实现的目标。

I am not 100% sure what problem you are trying to solve.我不是 100% 确定您要解决什么问题。 This is what I think you try to achieve.这就是我认为你试图实现的目标。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

class Main {
  public static void main(String[] args) {
    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(1);
    list1.add(2);
    list1.add(3);
    list1.add(4);
    List<Integer> list2 = new ArrayList<Integer>();
    list2.add(100);
    list2.add(400);
    list2.add(500);
    list2.add(700);

    Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
    map.put ("account_number", list1);
    map.put ("Amount", list2);

    System.out.println(map);

    List<String> list3 = new ArrayList<String>();
    List<Integer> list4 = new ArrayList<Integer>();

    String lastkey = null;
    int sequence = 0;
    for (final Entry<String, List<Integer>> entry : map.entrySet()) {
        final String key = entry.getKey();
        final List<Integer> value = entry.getValue();

        for (final Integer val : value) {
            list3.add(key);
            if(lastkey == null||lastkey!=key){
              sequence = 1;
              lastkey = key;
            } 
            list4.add(sequence++);
        }
    }

    System.out.println(list3);
    System.out.println(list4);
  }
}

Desired result: (generating unique sequence/id based on list2)期望结果:(根据 list2 生成唯一的序列/id)

Did you mean list1 ?你是说list1吗? Because the example you have given - [1, 2, 3, 4, 1, 2, 3, 4] , is of list1 .因为您给出的示例 - [1, 2, 3, 4, 1, 2, 3, 4]list1

Then, if you want to restart the sequence ID-Counter over the size of list1 , you could use % -operator:然后,如果您想在list1的大小上重新启动序列 ID-Counter,您可以使用% -operator:

    for (final Entry<String, List<Integer>> entry : map.entrySet()) {
        final String key = entry.getKey();
        final List<Integer> value = entry.getValue();
        for (int i = 0; i < value.size(); i++) { //<<< We need an index.
            list3.add(key);
            list4.add(list1.get(i % list1.size()));  //<<< Change this!
        }
    }

Your code-link, edited with changes.您的代码链接,经过更改编辑。

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

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