-1

I have a collection class (a java.util.ArrayList). When I go through the collection and print the lines, the output looks like this:

x = 1997 y = 1700
x = 1996 y = 1800  
x = 1992 y = 1150
x = 1994 y = 1300 
x = 1993 y = 1000
x = 1995 y = 1650 

How do I get max x, min x, max y, and min y from this collection?

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
user2481390
  • 31
  • 2
  • 7
  • possible duplicate of [How to get max() element from List in Guava](http://stackoverflow.com/questions/11758982/how-to-get-max-element-from-list-in-guava) – rzymek Dec 17 '13 at 12:14
  • Iterate over the collection. Store the first value in a variable and check with and if condition if the next value is bigger/smaller if true, store this value in the variable. After the iteration you have the min or max. – kai Dec 17 '13 at 12:17

1 Answers1

0
int minX = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;

int minY = Integer.MAX_VALUE;
int maxY = Integer.MIN_VALUE;

for (A a: collection) {
    if (a.x < minX)
       minX = a.x;
    if (a.x > maxX)
       maxX = a.x;

    if (a.y < minY)
       minY = a.y;
    if (a.y > maxY)
       maxY = a.y;

}
Tim B
  • 40,716
  • 16
  • 83
  • 128