Find the Biggest number in HashSet/HashMap java

后端 未结 8 2177
我在风中等你
我在风中等你 2020-12-29 04:48

I would like to find the biggest number in HashSet and HashMap. Say I have the number [22,6763,32,42,33] in my HashSet and I want to find the largest number in my current Ha

8条回答
  •  再見小時候
    2020-12-29 05:25

    Something like this:

    Set values = new HashSet() {{
        add(22);
        add(6763);
        add(32);
        add(42);
        add(33);
    }};
    int maxValue = Integer.MIN_VALUE;
    for (int value : values) {
        if (value > maxValue) {
            maxValue = value;
        }
    }
    

    And this:

    Map values = new HashMap() {{
        put("0", 22);
        put("1", 6763);
        put("2", 32);
        put("3", 42);
        put("4", 33);
    }};
    int maxValue = Integer.MIN_VALUE;
    for (int value : values.values()) {
        if (value > maxValue) {
            maxValue = value;
        }
    }
    

提交回复
热议问题