简体   繁体   English

如何比较列表元素以获得最大值?

[英]how to Compare list element in order to get the maximum value?

I want to compare a list element and get the max value but without the value of 65533 which represent 0 in my program.我想比较一个列表元素并获得最大值,但没有 65533 的值,它在我的程序中代表 0。 With this code I always have the same max value which is not normal.使用此代码,我始终具有相同的最大值,这是不正常的。 can someone correct my errors or advise me.有人可以纠正我的错误或建议我。

int a = text.charAt(39), b = text.charAt(40), c = text.charAt(41), d = text.charAt(42);

            List<Integer> list = Arrays.asList(a, b, c, d);

            Integer max = Collections.max(list);

            if ((a >= 5 & a!=65533) || (b >= 5 & b!=65533) || (c >= 5 & c!=65533) || (d >= 5 & d!=65533)) {

                if (max == a) {
                    Defauts_detecteur.setText(R.string.Défaut_récurrent_1);
                } else if (max == b) {
                    Defauts_detecteur.setText(R.string.Défaut_récurrent_2);
                } else if (max == c) {
                    Defauts_detecteur.setText(R.string.Défaut_récurrent_3);
                } else if (max == d) {
                    Defauts_detecteur.setText(R.string.Défaut_récurrent_4);
                }

                Conseil_detecteur.setText("Par mesure de sécurité, nous vous conseillons\nvivement de vérifier que le détecteur est bien\n collé au produit à sécuriser.\n\nPour une adhésion optimale,\nremplacez l’adhésif. Vérifiez que le détecteur est bien connecté à la centrale et qu’il est en bon état.");
            } else {
                Conseil_detecteur.setText("Oops ! Il semblerait que vous ayez rencontré quelques alarmes intempestives. \n" +
                        "\nPour une maintenance\n préventive de votre système, \n" +
                        "\ncontactez notre hotliner au 02 37 33 69 66\n");
                Defauts_detecteur.setVisibility(View.GONE);
            }
        }

The Collections.max(Collection) method gives you the largest element according to the collection elements' Comparable.compareTo method. Collections.max(Collection)方法根据集合元素的Comparable.compareTo方法为您提供最大的元素。

But your element type is Integer which uses the natural ordering of integers for compareTo .但是您的元素类型是Integer ,它使用整数的自然顺序进行compareTo

So what you need to do is:所以你需要做的是:

  1. Define a Comparator that implements your special ordering where 65535 means the same thing as 0 .定义一个Comparator来实现您的特殊排序,其中65535的含义与0相同。 (Assuming that is what you mean...) (假设这就是你的意思......)
  2. Use Collections.max(Collection, Comparable) to find the maximum of your list.使用Collections.max(Collection, Comparable)查找列表的最大值。

Customer a comparator and and sorted by desc, get the first element is the max value that you want.客户一个比较器并按 desc 排序,得到第一个元素是您想要的最大值。

    List<Integer> arr = Arrays.asList(5,3,2,4,1, 65533);
    int v = arr.stream()
            .sorted((a, b) -> {
              int a_ = a == 65533 ? 0 : a;
              int b_ = b == 65533 ? 0 : b;
              return b_ - a_;
            })
            .findFirst().get(); // v = 5

I suggest using Optional interface of Stream API, the following statement demonstrates how you use it.我建议使用 Stream API 的可选接口,下面的语句演示了如何使用它。 {OptionalInt max = Arrays.stream(strings).mapToInt(String::length).max();} {OptionalInt max = Arrays.stream(strings).mapToInt(String::length).max();}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM