📌  相关文章
📜  在Java中查找集合的最小和最大元素

📅  最后修改于: 2022-05-13 01:54:24.746000             🧑  作者: Mango

在Java中查找集合的最小和最大元素

集合是一组表示为单个单元的单个对象。 Java提供了集合框架,它定义了几个类和接口来将一组对象表示为一个单元。这些都是:

使用 Collections.min() 和 Collections.max() 方法可以轻松找到 Collection 的最小和最大元素。这些是Java中 Collections 类的静态方法。因此,它们可以在类名的帮助下直接访问,如下所示。

Object ob = Collections.min(Collection c);
Object ob = Collections.max(Collection c);

如上面的语法所示,Collections.min() 和 Collections.max() 方法将 Collection 作为参数。这是要找到最大/最小元素的集合。因此,任何实现 Collection 接口的类都可以作为这些函数的参数传递,例如 Arrays、LinkedList、ArrayList、Set 等。因此可以轻松找到任何集合的最大和最小元素。

下面的示例演示了查找某些集合的最小和最大元素的过程。

示例 1:在列表中

// Java program to find the minimum and
// maximum element in a List
  
import java.util.*;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
  
        // Get the ArrayList
        List list = new ArrayList();
  
        // populate the list
        list.add(12);
        list.add(53);
        list.add(30);
        list.add(8);
  
        // printing the List
        System.out.println("List: " + list);
  
        // getting minimum value
        // using min() method
        int minList = Collections.min(list);
  
        // getting maximum value
        // using max() method
        int maxList = Collections.max(list);
  
        // printing the minimum value
        System.out.println("Minimum value of list is: "
                           + minList);
  
        // printing the maximum value
        System.out.println("Maximum value of list is: "
                           + maxList);
    }
}
输出:
List: [12, 53, 30, 8]
Minimum value of list is: 8
Maximum value of list is: 53

示例 2:在集合中

// Java program to find the minimum and
// maximum element in a Set
  
import java.util.*;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
  
        // Get the HashSet
        Set set = new HashSet();
  
        // fill the hashSet
        set.add(3);
        set.add(6);
        set.add(2);
        set.add(9);
  
        // printing the Set
        System.out.println("Set: " + set);
  
        // getting minimum value
        // using min() method
        int minSet = Collections.min(set);
  
        // getting maximum value
        // using max() method
        int maxSet = Collections.max(set);
  
        // printing the minimum value
        System.out.println("Minimum value of set is: "
                           + minSet);
  
        // printing the maximum value
        System.out.println("Maximum value of set is: "
                           + maxSet);
    }
}
输出:
Set: [2, 3, 6, 9]
Minimum value of set is: 2
Maximum value of set is: 9

示例 3:在数组中

// Java program to find the minimum and
// maximum element in an Array
  
import java.util.*;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
  
        // Get the Array
        Integer arr[] = { 2, 5, 1, 8, 34, 20, 4 };
  
        // printing the Array
        System.out.println("Array: " + Arrays.toString(arr));
  
        // getting minimum value
        // using min() method
        int minArray = Collections.min(Arrays.asList(arr));
  
        // getting maximum value
        // using max() method
        int maxArray
            = Collections.max(Arrays.asList(arr));
  
        // printing the minimum value
        System.out.println("Minimum value of Array is: "
                           + minArray);
  
        // printing the maximum value
        System.out.println("Maximum value of Array is: "
                           + maxArray);
    }
}
输出:
Array: [2, 5, 1, 8, 34, 20, 4]
Minimum value of Array is: 1
Maximum value of Array is: 34

如果集合是 Map 会发生什么?

Map 是Java集合框架中的一种不同类型的实体,其中元素被视为对而不是直接值。所以地图集合类不实现集合接口。他们实现了地图接口。因此 Collection.min() 和 Collection.max() 不适用于 Maps 并且会抛出Compilation Error

例子:

// Java program to find the minimum and
// maximum element in a Map
  
import java.util.*;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
  
        try {
  
            // Creating hashMap
            Map map
                = new HashMap();
  
            // Putting key-value pairs in map
            map.put("A", 10);
            map.put("B", 15);
            map.put("C", 20);
            map.put("D", 25);
  
            // Print the map
            System.out.println("Map: " + map);
  
            // getting minimum value using min()
            int minMap = Collections.min(map);
  
            // getting maximum value using max()
            int maxMap = Collections.max(map);
  
            // printing the minimum value
            System.out.println("Minimum value of Map is: "
                               + minMap);
  
            // printing the maximum value
            System.out.println("Maximum value of Map is: "
                               + maxMap);
        }
  
        catch (NoSuchElementException e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Compile Errors:
prog.java:25: error: 
no suitable method found for min(Map)
            int minMap = Collections.min(map);
                                    ^
method Collections.min(Collection) 
is not applicable

如何找到地图的最小和最大元素?

虽然 Map 没有实现 Collection 接口,但是 Map 的元素类型,即KeyValue分别实现了 Collection 接口。因此,可以根据它们的键或值在 Map 中找到最小值和最大值元素。

示例 1:根据 Key Values 查找 Map 的最小和最大元素。

// Java program to find the minimum and
// maximum element in a Map
  
import java.util.*;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
  
        // Creating hashMap
        Map map
            = new HashMap();
  
        // Putting key-value pairs in map
        map.put("A", 10);
        map.put("B", 15);
        map.put("C", 20);
        map.put("D", 25);
  
        // Print the map
        System.out.println("Map: " + map);
  
        // getting minimum value using min()
        String minKey = Collections.min(map.keySet());
  
        // getting maximum value using max()
        String maxKey = Collections.max(map.keySet());
  
        // printing the minimum value
        System.out.println("Minimum Key of Map is: "
                           + minKey);
        System.out.println("Value corresponding to "
                           + "minimum Key of Map is: "
                           + map.get(minKey));
  
        // printing the maximum value
        System.out.println("Maximum Key of Map is: " + maxKey);
        System.out.println("Value corresponding to "
                           + "maximum Key of Map is: "
                           + map.get(maxKey));
    }
}
输出:
Map: {A=10, B=15, C=20, D=25}
Minimum Key of Map is: A
Value corresponding to minimum Key of Map is: 10
Maximum Key of Map is: D
Value corresponding to maximum Key of Map is: 25

示例 2:根据 Value Keys 查找 Map 的最小和最大元素。

// Java program to find the minimum and
// maximum element in a Map
  
import java.util.*;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
  
        // Creating hashMap
        Map map
            = new HashMap();
  
        // Putting key-value pairs in map
        map.put("A", 10);
        map.put("B", 15);
        map.put("C", 20);
        map.put("D", 25);
  
        // Print the map
        System.out.println("Map: " + map);
  
        // getting minimum value using min()
        int minValue = Collections.min(map.values());
  
        // getting maximum value using max()
        int maxValue = Collections.max(map.values());
  
        // printing the minimum value
        System.out.println("Minimum Value of Map is: "
                           + minValue);
  
        // printing the maximum value
        System.out.println("Maximum Value of Map is: "
                           + maxValue);
    }
}
输出:
Map: {A=10, B=15, C=20, D=25}
Minimum Value of Map is: 10
Maximum Value of Map is: 25