简体   繁体   中英

Compare elements in Array of ArrayList - Java

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;
}
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.

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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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