如何在Java中使用过滤器时替换表达式?

huangapple 未分类评论45阅读模式
英文:

How to replace a expression when using a filter in Java?

问题

我正在进行一项关于Android的课程作业,它会在屏幕上返回一份外卖列表,列出它们的地址、距离我的位置的距离以及卫生评分。我在设置筛选器以过滤掉不符合条件的评分(即-1)方面遇到了困难。

因此,在这些情况下,我想在屏幕上返回 "exempt"。这是我目前的代码:

for (int i = 0; i < ja.length(); i++) {
    JSONObject JO = (JSONObject) ja.get(i);
    // 如果餐厅的评分是-1,则显示 "exempt"
    if (ratingValue.get(i).equals("-1")) {
        System.out.println("Exempt");
    }
    ConcatenateSearchResults();

    // 其他功能的输出
    // ...
}

对于"ifPresent",在Java中它是存在的,但在Android中似乎不存在,我尝试过的教程也没有帮助。

doInBackground 方法中:

// 遍历外卖数据数组
for (int i = 0; i < ja.length(); i++) {
    JSONObject JO = (JSONObject) ja.get(i);
    // 如果餐厅的评分是-1,则显示 "exempt"
    if (ratingValue.get(i).equals("-1")) {
        System.out.println("Exempt");
    }
    ConcatenateSearchResults();

    // 输出满足基本功能要求的数据
    // ...
}

希望对你有帮助!

英文:

I am working on a piece of coursework in android, it returns a list of takeaways to the screen, listing their address, the distance from my location, and a hygiene rating and I struggling to set up the filter to filter out ratings that don't meet the criteria i.e -1

Thus in these cases I want to return "exempt" to the screen. This is what I have so far

 for (int i = 0; i &lt; ja.length(); i++) {
                        JSONObject JO = (JSONObject) ja.get(i);
                        // if the rating of the restaurant is -1, exempt
                        // should be displayed
                            ratingValue.stream()
                                .filter(x-&gt;x.equals(&quot;-1&quot;))
                               &lt;&lt; ifPresent.&gt;&gt;  (System.out.println(&quot;Exempt&quot;));

                        {
                            ConcatenateSearchResults();
                        }

The ifPresent that seems to exist in Java, doesn't seem to exist in Android and the tutorials I tried don't assist me.

protected JSONArray doInBackground(Void... voids) {
        // if the searchUrl conversion is empty
        if (!searchUrl.toString().isEmpty()) {
            // do this instead
            try {
                URL url = new URL(searchUrl.toString());
                URLConnection tc = url.openConnection();
                InputStreamReader isr = new InputStreamReader(tc.getInputStream());
                BufferedReader in = new BufferedReader(isr);

                String line;  // variable

                // while line in (read in) is not equal to null
                // create a new object and output as line in JSON
                while ((line = in.readLine()) != null) {
                    ja = new JSONArray(line);

                    // run through the length of the array

                    for (int i = 0; i &lt; ja.length(); i++) {
                        JSONObject JO = (JSONObject) ja.get(i);
                        // if the rating of the restaurant is -1, exempt
                        // should be displayed
                        ratingValue.stream()
                                .filter(x -&gt; x.equals(&quot;-1&quot;))
                                .findFirst()
                                .ifPresent(v -&gt; System.out.println(&quot;Exempt&quot;));
                        ConcatenateSearchResults();

                        // output to meet Basic Functionality requirement
                        businessNames.add(JO.getString(&quot;BusinessName&quot;));
                        postCodes.add(JO.getString(&quot;PostCode&quot;));
                        addressList1.add(JO.getString(&quot;AddressLine1&quot;));
                        addressList2.add(JO.getString(&quot;AddressLine2&quot;));
                        addressList3.add(JO.getString(&quot;AddressLine3&quot;));
                        ratingValue.add(JO.getString(&quot;RatingValue&quot;));


                        calcDistance.add(JO.getString(&quot;DistanceKM&quot;));
                    }
                }
                isr.close();
                in.close();
                //return ja;
            } catch (Exception e) {
                this.exception = e;

                return ja;
            } finally {
                //is.close();
            }
        }
        return ja;
    }

Cheers

答案1

得分: 0

ifPresent()在Android平台中没有关联,它是Java的一部分。
在您的情况下,.filter()方法会返回另一个经过筛选的值流,不是单个值,而是一个流

所以,您不能在流上使用ifPresent(),您需要获取单个值。为了使您的代码正确,您可以在ifPresent()之前简单地添加.findFirst(),这将从ratingValue中返回第一个经过筛选的值:

ratingValue.stream()
    .filter(x -> x.equals("-1"))
    .findFirst()
    .ifPresent(v -> System.out.println("Exempt"));
英文:

ifPresent() doesn't relate to the Android platform, its a part of Java.
In your case, .filter() the method returns another stream of filtered values, not the single value, but a stream.

So you can't use ifPresent() on streams, you need to obtain a single value. To make you code correctly, you can simply add .findFirst() before ifPresent(), this will return the first filtered value from ratingValue:

ratingValue.stream()
        .filter(x -&gt; x.equals(&quot;-1&quot;))
        .findFirst()
        .ifPresent(v -&gt; System.out.println(&quot;Exempt&quot;)

huangapple
  • 本文由 发表于 2020年4月9日 22:48:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/61123900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定