【问题标题】:Is there better approach for partitioning list and process in batches for changing source list是否有更好的方法来分区列表和批量处理以更改源列表
【发布时间】:2020-03-07 17:59:51
【问题描述】:

我将有一个基本上是动态的源列表。一些进程会不断地向这个源列表中添加记录。我想将此列表中的记录作为 10 个条目的批次进行处理。一旦条目被处理,它就会从源列表中清除,这样我就不会再次处理它们并确保列表不会增长。

我也不想长时间阻止源列表来添加记录。(假设我们在同步块中做了一些事情)。有人可以帮助以有效的方式解决这个问题吗?

我打算使用Guava列表分区来批量获取记录,但我不明白这是否适用于不断变化的列表。

【问题讨论】:

    标签: java list arraylist collections


    【解决方案1】:

    以下是分区列表和处理大小为 10 的元素的简单示例。 在 10 个元素之后,它将清除列表,您可以再次在同一列表中填充新元素。 在您的批处理过程中,您可以使用此代码在特定大小的列表元素后清除记录。

        public static void main(String arg[]) {
            ArrayList<Integer> arrList = null;
            Scanner scan = null;
            char choice = 'n';
            try {
                arrList = new ArrayList<Integer>(10);
                scan = new Scanner(System.in);
                do {
                    System.out.println("Enter 10 element and add in ArrayList");
                    for (int i = 0; i < 10; i++) {
                        System.out.print("Enter element [" +i+"] :: ");
                        int number = scan.nextInt();
                        arrList.add(number);
                    }
    
                    for (Integer number : arrList) {
                        System.out.println("Elements in list = " + number);
                    }
                    int retval = arrList.size();
                    if (retval == 10) {
                        arrList.clear();
                    }
                    System.out.println("Performing clear operation !!");
                    System.out.println("Do you want to continue !!");
                    String next = scan.next();
                    choice = next.charAt(0);
                } while ((choice == 'y') || (choice == 'Y'));
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                scan.close();
                System.out.println("Exit  !!");
            }
        }
    

    输出:

    Enter 10 element and add in ArrayList
    
    Enter element [0] :: 1
    
    Enter element [1] :: 2
    
    Enter element [2] :: 3
    
    Enter element [3] :: 4
    
    Enter element [4] :: 5
    
    Enter element [5] :: 6
    
    Enter element [6] :: 7
    
    Enter element [7] :: 8
    
    Enter element [8] :: 9
    
    Enter element [9] :: 10
    
    Elements in list = 1
    
    Elements in list = 2
    
    Elements in list = 3
    
    Elements in list = 4
    
    Elements in list = 5
    
    Elements in list = 6
    
    Elements in list = 7
    
    Elements in list = 8
    
    Elements in list = 9
    
    Elements in list = 10
    
    Performing clear operation !!
    
    Do you want to continue !!
    
    N
    
    Exit  !!
    

    您可以根据需要更改此代码。

    希望这个例子对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 2022-09-22
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      相关资源
      最近更新 更多