0

I am using the following code to traverse through a array of arraylist:

for(List<Long> innerList : arr) {
    for(Long number : innerList) {
       System.out.println(number);
    }
}

This code returns the number in each array of the arraylist.I want to compare the first element of the first array of the arraylist with all other elements of all other array of the arraylist. How to do this?

Complete program:

static long getWays(long sum, long[] changes) {
    int count=0;
    ArrayList<ArrayList<Long>> arr = new ArrayList<ArrayList<Long>>();
    for(int i=0;i<changes.length;i++){
        ArrayList<Long> arr1 = new ArrayList<Long>();
        long change = changes[i];
        long value = changes[i];
        arr1.add(change);
        for(int j=0;j<sum;j++){
            change = change + value;
            if(change>sum){
                break;
            }
            arr1.add(change);
        }
        arr.add(arr1);
    }
    for(List<Long> innerList : arr) {
        for(Long number : innerList) {
            System.out.println(number);
        }
    }
    return count;
}
1
  • Need more info, like the other arrays in question. Commented Jun 12, 2018 at 5:00

2 Answers 2

1

I would go for a Stream.flatMap usage.

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.

data.stream() //stream a list of list
    .flatMap( List::stream ) //stream each list
    // At this point, we have a Stream reading every `Long` values in those Collections
    .filter(l -> l.equals(value)) //filter only the equivalent value
    .count() - 1; //-1 to remove the first one

This would give you the number of occurence of the first value where :

final Long value = data.get(0).get(0);

Example :

List<List<Long>> data = new ArrayList<List<Long>>();
    
for(int i = 0; i < 5; ++i){
    data.add(new ArrayList<>());
    for(int j = 0; j < 10; ++j){
        data.get(i).add(1L * i * j);
    }
}
    
final Long value = data.get(0).get(0);
long count = data.stream()
                 .flatMap( List::stream )
                 .filter(l -> l.equals(value))
                 .count() - 1;
System.out.println(count);

13

You were not clear about the comparison you where doing so I let you adapt the filter. I simply count the occurrence of the first value here.

Note that you still have some validation to prevent exception (check if the list aren't null, if there is at list one List with one Long...

Sign up to request clarification or add additional context in comments.

Comments

1
Long first = arr.get(0).get(0);
for (List<Long> innerList: arr) {
  for (Long number: innerList) {
    System.out.println(number);
    // Here compare first and number
  }
}

You can keep the first element in a var.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.