如何从java中的多列集合中获取max和min值

时间:2013-12-17 12:13:48

标签: java collections arraylist

我有一个集合类(java.util.ArrayList)。当我浏览集合并打印行时,输出如下所示:

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

如何从此系列中获取 max x min x max y min y

1 个答案:

答案 0 :(得分: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;

}