0

I have a stream of strings. I want to provide a method parameter x which then streams the strings in groups of x which can then be collected in a set.

So if x=4 then and the stream size was 8, then I would have two sets containing 4 elements. The for each of those sets of 4 elements I can call ForEach and do other operations on, in the above example forEach would be called twice 1 for each 4 element set.

TheDon2019
  • 19
  • 3

1 Answers1

0

I'm not very sure if it is what you are asking.

So you have some data (say, integer), and you want to put it into multiple collections, each set have X elements.

Maybe you can use a 2D array? I don't have a JDK on hand so i just writing some pseudo code. For example:

int[][] MyArray = new int[MAX_BUFFER][X]

Then you use a while loop

int a = 0, b = 0;
While(!instream.empty){
  int t = instream.next;
  MyArray[a][b]=t;
  b++;
  if(b>=X){
    a++;
    b=0;
  }
}

I suddenly understand that arraylist of arrays may work better for you.

ArrayList<int[]> MyArray = new ArrayList<int[]>()

    int a = 0;
    int temparray= new int[X];
    While(!instream.empty){
      int t = instream.next;
      temparray[a]=t;
      a++;
      if(a>=X){
        a=0;
        MyArray.add(temparray);
      }
    }
    if(a>0){//So there are some data left in the temp array;
      MyArray.add(temparray);
    }
Ice.Rain
  • 113
  • 2
  • 4
  • 12