title: Partition a Set into smaller Subsets and process as batch
body: <p>I have a continuous running thread in my application, which consists of a HashSet to store all the symbols inside the application. As per the design at the time it was written, inside the thread's while true condition it will iterate the HashSet continuously, and update the database for all the symbols contained inside HashSet.</p>
  <p>The maximum number of symbols that might be present inside the HashSet will be around 6000. I don't want to update the DB with all the 6000 symbols at once, but divide this HashSet into different subsets of 500 each (12 sets) and execute each subset individually and have a thread sleep after each subset for 15 minutes, so that I can reduce the pressure on the database.</p>
  <p>This is my code (sample code snippet)</p>
  <p>How can I partition a set into smaller subsets and process (I have seen the examples for partitioning ArrayList, TreeSet, but didn't find any example related to HashSet)</p>
  <pre><code>package com.ubsc.rewji.threads;
  
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  import java.util.concurrent.PriorityBlockingQueue;
  
  public class TaskerThread extends Thread {
      private PriorityBlockingQueue&lt;String&gt; priorityBlocking = new PriorityBlockingQueue&lt;String&gt;();
      String symbols[] = new String[] { &quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot; };
      Set&lt;String&gt; allSymbolsSet = Collections
              .synchronizedSet(new HashSet&lt;String&gt;(Arrays.asList(symbols)));
  
      public void addsymbols(String commaDelimSymbolsList) {
          if (commaDelimSymbolsList != null) {
              String[] symAr = commaDelimSymbolsList.split(&quot;,&quot;);
              for (int i = 0; i &lt; symAr.length; i++) {
                  priorityBlocking.add(symAr[i]);
              }
          }
      }
  
      public void run() {
          while (true) {
              try {
                  while (priorityBlocking.peek() != null) {
                      String symbol = priorityBlocking.poll();
                      allSymbolsSet.add(symbol);
                  }
                  Iterator&lt;String&gt; ite = allSymbolsSet.iterator();
                  System.out.println(&quot;=======================&quot;);
                  while (ite.hasNext()) {
                      String symbol = ite.next();
                      if (symbol != null &amp;&amp; symbol.trim().length() &gt; 0) {
                          try {
                              updateDB(symbol);
  
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }
                  }
                  Thread.sleep(2000);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  
      public void updateDB(String symbol) {
          System.out.println(&quot;THE SYMBOL BEING UPDATED IS&quot; + &quot;  &quot; + symbol);
      }
  
      public static void main(String args[]) {
          TaskerThread taskThread = new TaskerThread();
          taskThread.start();
  
          String commaDelimSymbolsList = &quot;ONVO,HJI,HYU,SD,F,SDF,ASA,TRET,TRE,JHG,RWE,XCX,WQE,KLJK,XCZ&quot;;
          taskThread.addsymbols(commaDelimSymbolsList);
  
      }
  
  }
  </code></pre>