寻找钥匙的最大值

时间:2018-08-07 12:00:23

标签: java

public class Task1 {

    public static void main(String[] args) {

        Map<String, List<String>> map = new HashMap<String, List<String>>();

        List<String> valSetOne = new ArrayList<String>();
        valSetOne.add("0.00");
        valSetOne.add("0.01");
        valSetOne.add("1.00");

        List<String> valSetTwo = new ArrayList<String>();
        valSetTwo.add("0.00");
        valSetTwo.add("0.01");

        List<String> valSetThree = new ArrayList<String>();
        valSetThree.add("0.01");
        valSetThree.add("1.00");

        List<String> valSetFour = new ArrayList<String>();
        valSetFour.add("0.01");

        map.put("HR3-A1234", valSetOne);
        map.put("HR3-A2345", valSetTwo);
        map.put("HR3-A3456", valSetThree);
        map.put("HR3-A4567", valSetFour);

        for (Map.Entry<String, List<String>> entry : map.entrySet()) {

            String key = entry.getKey();

            List<String> values = entry.getValue();

            Object obj = Collections.max(values);
            System.out.println("Value of " + key + " is " + obj);

        }

    }
}

这是我的代码,工作正常。对于键HR3-A2345,我想找到最小值,对于其他记录,我需要最大值。有人可以建议我解决方案吗?

2 个答案:

答案 0 :(得分:1)

如果您不想使用if...else,那么我建议先对集合进行排序,然后再将其放入地图中以首先放置所需的元素。例如:

final BiFunction<List<String>, Comparator<String>, List<String>> sort = (values, comparator) -> {
    values.sort(comparator);
    return values;
};

Map<String, List<String>> map = new HashMap<>();
map.put("HR3-A1234", sort.apply(valSetOne(), null)); // first element is min
map.put("HR3-A2345", sort.apply(valSetTwo(), Collections.reverseOrder()));  // first element is max
map.put("HR3-A3456", sort.apply(valSetThree(), null)); // first element is min
map.put("HR3-A4567", sort.apply(valSetFour(), null)); // first element is min

map.forEach((key, values) ->
        System.out.println("Value of " + key + " is " + (values != null && !values.isEmpty() ? values.iterator().next() : null)));

注释:

  1. 我可以看到,您想比较双精度值,但是在您的示例中,您比较了Strings!这不一样。在这种情况下,您必须在代码中将String替换为Double
  2. 如果值不能重复,则可以将List与必需的比较器一起使用,而不是使用TreeSet

例如;

public static void main(String... args) {
    Map<String, Set<Double>> map = new HashMap<>();
    map.put("HR3-A1234", valSetOne(null)); // first element is min
    map.put("HR3-A2345", valSetTwo(Collections.reverseOrder()));  // first element is max
    map.put("HR3-A3456", valSetThree(null)); // first element is min
    map.put("HR3-A4567", valSetFour(null)); // first element is min

    map.forEach((key, values) -> {
        if (values != null && !values.isEmpty())
            System.out.format("Value of %s is %.2f\n", key, values.iterator().next());
    });
}

private static Set<Double> valSetOne(Comparator<Double> comparator) {
    Set<Double> values = new TreeSet<>(comparator);
    values.add(.0);
    values.add(.01);
    values.add(1.);
    return values;
}

private static Set<Double> valSetTwo(Comparator<Double> comparator) {
    Set<Double> values = new TreeSet<>(comparator);
    values.add(.0);
    values.add(.01);
    return values;
}

private static Set<Double> valSetThree(Comparator<Double> comparator) {
    Set<Double> values = new TreeSet<>(comparator);
    values.add(.01);
    values.add(1.);
    return values;
}

private static Set<Double> valSetFour(Comparator<Double> comparator) {
    Set<Double> values = new TreeSet<>(comparator);
    values.add(.01);
    return values;
}

答案 1 :(得分:0)

    for (Map.Entry<String, List<String>> entry : map.entrySet()) {

        String key = entry.getKey();

        List<String> values = entry.getValue();

        Object obj = key.equals("HR3-A2345") ? Collections.min(values) : Collections.max(values);
        System.out.println("Value of " + key + " is " + obj);

    }

    for (Map.Entry<String, List<String>> entry : map.entrySet()) {

        String key = entry.getKey();

        List<String> values = entry.getValue();

        Object obj;
        if(key.equals("HR3-A2345"))
            obj = Collections.min(values);
        else
            obj = Collections.max(values);
        System.out.println("Value of " + key + " is " + obj);

    }

我可能误解了您的问题,但似乎您所需要的只是将密钥与您要查找的密钥进行比较。