0

I have an int element and I would like to know if this int is higher than all the integers of an Arraylist ?

Example :

int a ; // Contain an int (i.e 51 or 54 or 989...etc)

ArrayList<Integer> list = new ArrayList<Integer>(); // contain a list of Integers

My purpose is to know if a is higher than any number in the arraylist.

Thanks in advance

5
  • Either loop through that list and check each element, or sort it by using Collections.sort() and check the last index. Commented Dec 3, 2014 at 17:26
  • How can I know if a if higher than any other integer in the list ? Commented Dec 3, 2014 at 17:27
  • the order is important, so I couldn't use sort Commented Dec 3, 2014 at 17:28
  • Say you have a basket of apples and you want to find the apple which has the brightest red color. What do you do? Commented Dec 3, 2014 at 17:28
  • you can create a temporary array list and sort it and find the index(s) of the highest numbers, without changing the first arraylist Commented Dec 3, 2014 at 17:29

5 Answers 5

5

Sorting is complete overkill. You just need to compare your value to the highest value in the list. Here's a way to do this with existing library functions:

if (a > Collections.max(list)) {
   //a is higher than anything in list
}

Note:

This carries a caveat of always going through the whole list even if the very first element is larger than a. But it's a tradeoff you're usually willing to make because the code reads so nice. If you really want the early exit, you can roll your own approach like in Austin's answer, or in Java 8, it would look something like this:

if ( list.stream().allMatch(element -> a > element) ) {
  //...a is higher than anything in list
}
Sign up to request clarification or add additional context in comments.

Comments

2

you can just iterate the array to see if any other value is higher..

int a = _whateverInt ; // Contain an int (i.e 51 or 54 or 989...etc)

ArrayList<Integer> list = new ArrayList<Integer>();
boolean isHigher = true;
for(int i = 0; i < list.size() && isHigher; i ++)
{
    isHigher = a > list.get(i);

}

5 Comments

How is it not breaking from the loop?
@MarkPeters how it this not a "nice" way to break out of a loop?
if it is the same number, then it is not greater @AngelAngel
@austinwernli: Well, simply based on a sample of two (Sotirios and I) we both had to do a double take to see that you were breaking. Ideally this would be in a utility function where you could just return false, but barring that I usually prefer an explicit break rather than burying the early exit in the condition. Just a readability preference, and I don't seem to be the only one who thinks so :).
@MarkPeters, very understandable, i'll have to keep that in mind in the future. Thanks for the input!
0

Solution 1:

public class Test {
    public static void main(String[] args) {
        int a = 150;

        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(50);
        arrayList.add(100);
        arrayList.add(30);

        System.out.println(a > Collections.max(arrayList));
    }
}

Solution 2:

public class Test {
    public static void main(String[] args) {
        int a = 150;

        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(50);
        arrayList.add(100);
        arrayList.add(30);

        Collections.sort(arrayList);
        System.out.println(a > arrayList.get(arrayList.size() - 1));
    }
}

Comments

0
int a;

ArrayList<Integer> list = new ArrayList<Integer>();

int max = Collections.max(list);

if(a>max)
{
//this is what you want
}

Hope you find this useful... edit: oops someone already answered the same trick :(

Comments

-2
private boolean isLargest(int a, ArrayList<Integer> list)
{
   ArrayList<Integer> sortedList = Collections.sort(list);
   if(a > sortedList.get(0))
      return true;
   return false;
}

It is not efficient, but this approach leaves the ordering in the original list in-tact.

1 Comment

This code cannot work, because Collections#sort doesn't return anything.

Your Answer

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