How compare int[] elements in arraylist?

Viewed 265
public company(String a, int[] b)

I created a class, company.

ArrayList<company> c1;
int temp;
for(company c:c1) {
    if (temp > c.getb()) {
        temp = c.getb();
    }

There is a list of companies, and a company has a number of integers stored in it as an int[].

I am trying to compare and get the largest value from int[] b.

But my code is showing an error since c.getb() is int[] array so can't be compared by < or >

So, I am not sure how to compare int[] values in the ArrayList.

4 Answers

I am trying to compare and get the largest value from int[] b.

Your code has several issues:

ArrayList<company> c1 = ...
int temp;
for(company c : c1) {
    if (temp > c.getb()) {
        temp = c.getb();
    }
}

First, you need to initialize temp to a value, which value ? Integer.MIN_VALUE;, because you are trying to get the largest value. Moreover, this:

for(company c : c1) {
    if (temp > c.getb()) {
        temp = c.getb();
    }
}

is not getting the largest it is getting the smallest. So what you actually want to do is the following:

List<company> c1 = ....
int max = Integer.MIN_VALUE;
for(company c : c1) {
    int temp = Arrays.stream(c.getb()).max().getAsInt();
    max = Math.max(temp, max);
}

If you don't want to use the Arrays.stream you can create the function yourself:

public static int maxArray(int [] array){
    int max = Integer.MIN_VALUE;
    for(int e :  array) {
        max = Math.max(e, max);
    }
    return max;
}

Which as you can see it is very similar to what you are trying to do in the first place.

With Java streams you can do it as follows:

ArrayList<company> c1 = ...
int max = c1.stream()
            .map(c -> Arrays.stream(c.getb()).max().getAsInt())
            .max(Comparator.naturalOrder())
            .orElse(Integer.MIN_VALUE);

You can get the max value of an array with primitives with native java methods:

Collections.max(Arrays.asList(array));

To save performance we are going to save the list. Also you can't compare temp to something because you didn't initialize temp.

If you want to get bigger numbers you would want to do temp < max instead of temp > max so then every iteration you would check if the company element's b array has a greater number than its predecessor.

List<company> c1 = ....
int temp = Integer.MIN_VALUE;
for(company c:c1) {
    int max = Collections.max(Arrays.asList(c.getb()));
    if (temp < max) {
        temp = max;
    }
}

Try this.

ArrayList<company> c1 = new ArrayList<>();
// ....
int temp = c1.stream()
    .flatMapToInt(c -> Arrays.stream(c.getB()))
    .max().getAsInt();

When the list is empty, there is no maximum value, so NoSuchElementException is thrown.


import java.util.ArrayList;

public class Runner {

    public static void main(String[] args) {

        // The arraylist of company
        ArrayList<Company> c1 = new ArrayList<>();
        
        // First set of data creating the int[] for Apple company and inserting in c1 List
        int[] i = { 9, 4, 7, 5, 10, 8 };
        c1.add(new Company("Apple", i));
        
        // Second set of data creating the int[] for Samsung company and inserting in c1 List
        i = new int[] { 0, 2, -3, 4, 5 };
        c1.add(new Company("Samsung", i));

        // The arraylist has now 2 companies
        
        
        // First we store the largest from the array of the first company in temp
        int temp = Runner.findLargestFromArray(c1.get(0).getB());
        
        // We iterate the arraylist of the companies
        for (Company c : c1) {
            // For each company we find the largest of int[] B 
            int l = Runner.findLargestFromArray(c.getB());
            
            // If temp is less than the largest it will be updated
            if (temp < l) {
                temp = l;
            }

        }
        // the largest of arrays of both the companies is stored here i.e 10
        System.out.println(temp);
    }

    // Method which takes array as an input and returns the highest number stored in it
    public static int findLargestFromArray(int[] arr) {

        int largest = arr[0];
        int index = 1;
        while (index < arr.length) {
            // check if largest is smaller than element
            if (largest < arr[index]) {
                // update largest
                largest = arr[index];
            }
            index++;
        }
        return largest;
    }

}

Main driver code

package mpackage;

import java.util.Arrays;

public class Company {
    String a;
    int[] b;
    
    
    public Company() {
    }


    public Company(String a, int[] b) {
        this.a = a;
        this.b = b;
    }


    public String getA() {
        return a;
    }


    public void setA(String a) {
        this.a = a;
    }


    public int[] getB() {
        return b;
    }


    public void setB(int[] b) {
        this.b = b;
    }


    @Override
    public String toString() {
        return "Company [a=" + a + ", b=" + Arrays.toString(b) + "]";
    }

    
}

Company Class

Related